Exemple #1
0
def UnsupervisedClassification(DiskPath,filelist):
    # Variables
    #imdir = r'C:\CK\welldata-images\Disk1'
    targetdir = os.getcwd()+"/"+  "Cluster/"+dirName
    number_clusters = 4
    featurelist = []
    for i, imagepath in enumerate(filelist):
        print("    Status: %s / %s" %(i, len(filelist)), end="\r")
        img = image.load_img(imagepath,target_size=(224, 224))
        img_data = image.img_to_array(img)
        img_data = np.expand_dims(img_data, axis=0)
        img_data = preprocess_input(img_data)
        features = np.array(model.predict(img_data))
        #features_flat = features.reshape(( -1,2))
        featurelist.append(features.flatten())

    # Clustering
    kmeans = KMeans(n_clusters=number_clusters, random_state=0).fit(np.array(featurelist))

    print('------------------------K-Means Clustering Completed------------------------')
    try:
        os.makedirs(targetdir)
    except OSError:
        pass
    #Copy  Image
    print("\n")
    for i, m in enumerate(kmeans.labels_):
        print("    Copy: %s / %s" %(i, len(kmeans.labels_)), end="\r")
        shutil.copy(filelist[i], targetdir + str(m) + "_" + str(i) + ".png") 
        #print('Before Upload Files')
        uploadFile.UploadFiles('converge-clusterimages',DiskPath,str(m) + "_" + str(i) + ".png",targetdir + str(m) + "_" + str(i) + ".png")
def ImageProcess(path, DiskPath):
    try:

        if ("Splits" in path):
            outputDir = path.rsplit('/', 1)[0] + "\\Splits-Enhanced\\"
            if (os.path.isdir(outputDir) == False):
                os.makedirs(outputDir, exist_ok=True)
                print('Created Enhanced images directory for splits')
        else:
            print(path)
            outputDir = path.rsplit('\\', 1)[0] + "\\Enhanced\\"
            if (os.path.isdir(outputDir) == False):
                os.makedirs(outputDir, exist_ok=True)
                print('Created Enhanced images directory')

            file_name = path.rsplit('/', 1)[-1]
            #print (os.path.join(root,filename) )

            #file_name_withoutext=Constants.FileName
            file_name_withoutext = path.rsplit('\\', 1)[1].split('.')[0]

            im_new = process_image_for_ocr(path)
            kernel = np.ones((1, 1), np.uint8)
            closing = cv2.morphologyEx(im_new, cv2.MORPH_CLOSE, kernel)
            #kernel = np.ones((1,1),np.uint8)
            #erosion = cv2.erode(im_new,kernel,iterations = 50)

            EnhancedImagePath = os.path.join(
                outputDir, file_name_withoutext + '_Enhanced.png')
            cv2.imwrite(EnhancedImagePath, closing)
            #cv2.waitKey(0)

            #fetch ContainerName From StorageContainer For ProcessedImages
            #StorageAcountName=retrieveTableStorage.FetchValueFromTableStorage('ProcessedStorageAccountNameWest')
            #import Constants as cons
            #OriginLoc=cons.OriginLocation
            #if(OriginLoc=='WH'):
            #ProcessedSContainerName=retrieveTableStorage.FetchValueFromTableStorage('ProcessedStorageAccountNameWest')
            #if(OriginLoc=='EH'):
            #ProcessedSContainerName=retrieveTableStorage.FetchValueFromTableStorage('ProcessedStorageAccountNameEast')

            #Comment this
            #ProcessedSContainerName=Constants.ProcessedSContainerName
            #StorageAcountName=retrieveTableStorage.FetchValueFromTableStorage('StorageAccountNameEast')

            #uploadFiles.UploadFiles(ProcessedSContainerName,file_name_withoutext+'_Enhanced.png',os.path.join(outputDir,file_name_withoutext+'_Enhanced.png'))
            uploadFile.UploadFiles('converge-enhancedimages', DiskPath,
                                   file_name_withoutext + '_Enhanced.png',
                                   EnhancedImagePath)
            print('----------------Image Processing Completed for ' +
                  file_name_withoutext + '----------------')
            #print('upload Done!')

            #import Constants as cons
            #OriginLoc=cons.OriginLocation

            #UnComment Production
            #FETCH STOAREGE ACCOUNT NAME
            #if(OriginLoc=='WH'):
            #StorageAcountName=retrieveTableStorage.FetchValueFromTableStorage('StorageAccountNameWest')
            #if(OriginLoc=='EH'):
            #StorageAcountName=retrieveTableStorage.FetchValueFromTableStorage('StorageAccountNameEast')
            #UnComment Production
            #StorageAcountName='029ze1b1storslbpcom'
            # print('upload Done!!!')
            #print('path:::'+path)
            #path=path.replace("/", "\\")
            #absPath=path.rsplit('\\', 1)[0]
            #cwd=os.getcwd()
            #fullpath="".join(absPath.rsplit(cwd))
            #fullpath=fullpath.lstrip("\\")
            #print('fullpath:::'+fullpath)
            #fullpath=fullpath.replace("\\", "/")
            #print('After fullpath:::'+fullpath)
            #StorageAcountName = retrieveTableStorage.FetchValueFromTableStorage(Constants.UploadStorageAccountName)
            #StorageAcountName='stgactssabackupdev'
            #blobpath='https://'+StorageAcountName+'.blob.core.windows.net/'+ ProcessedSContainerName+'/'+fullpath+'/'+file_name_withoutext+'_Enhanced.png'

            #insertintoCosmos.updateDocumentsinCosmos(file_name_withoutext+'.TIF','EnhancedImage_FilePath',blobpath)

            return EnhancedImagePath

    except Exception as e:
        print('Error occurred in ImageProcess :::.', e)