def search_SIFT_BF(returnCount=100, mydataSIFT=mydataSIFT,SIFT_FEATURES_LIMIT=SIFT_FEATURES_LIMIT): imagepredictions , searchtimesift = ImageSearch_Algo_SIFT.SIFT_SEARCH_BF(mydataSIFT, q_path, sift_features_limit=SIFT_FEATURES_LIMIT, lowe_ratio=LOWE_RATIO, predictions_count=returnCount) a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagepredictions, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_sift_BF'] = a row_dict['index_sift_BF'] = ind row_dict['Count_sift_BF'] = cnt row_dict['quality_sift_BF'] = d row_dict['time_sift_BF'] = searchtimesift return imagepredictions, searchtimesift
def search_ORB_BOVW (returnCount=100, write=False) : imagematches, searchtime = ImageSearch_Algo_ORB.ORB_SEARCH_TREE(q_path, myORBmodel, myORBtree, mydataORB, returnCount=100, kp=ORB_FEATURES_LIMIT) if write: a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagematches, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_orb_tree'] = a row_dict['index_orb_tree'] = ind row_dict['Count_orb_tree'] = cnt row_dict['quality_orb_tree'] = d row_dict['time_orb_tree'] = searchtime return imagematches, searchtime
def search_SIFT_BOVW(returnCount=100, write=False): imagematches, searchtime = ImageSearch_Algo_SIFT.SIFT_SEARCH_TREE(q_path, mySIFTmodel, mySIFTtree, mydataSIFT, returnCount=returnCount, kp=100) if write: a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagematches, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_sift_tree'] = a row_dict['index_sift_tree'] = ind row_dict['Count_sift_tree'] = cnt row_dict['quality_sift_tree'] = d row_dict['time_sift_tree'] = searchtime return imagematches, searchtime
def search_ORB_BF2(returnCount=100, mydataORB=mydataORB, write=False) : imagematches, searchtime = ImageSearch_Algo_ORB.ORB_SEARCH_MODBF(mydataORB, q_path, ORB_FEATURES_LIMIT , lowe_ratio=LOWE_RATIO, predictions_count=returnCount ) if write: a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagematches, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_orb_BF2'] = a row_dict['index_orb_BF2'] = ind row_dict['Count_orb_BF2'] = cnt row_dict['quality_orb_BF2'] = d row_dict['time_orb_BF2'] = searchtime return imagematches, searchtime
def search_HASH_All(returnCount=100, write=False): # AlgoGenList = ['whash', 'phash', 'dhash', 'ahash'] for algo in AlgoGenList : imagematches, searchtime = ImageSearch_Algo_Hash.HASH_SEARCH_TREE(myHASH_Trees[algo], mydataHASH, q_path, hashAlgo=algo, hashsize=16, returnCount=returnCount) if write: a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagematches, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_HASH_'+str(algo)] = a row_dict['index_HASH_'+str(algo)] = ind row_dict['Count_HASH_'+str(algo)] = cnt row_dict['quality_HASH_'+str(algo)] = d row_dict['time_HASH_'+str(algo)] = searchtime
def search_HASH_HYBRID (returnCount=100, write=False): # HybridAlgoList = ['whash', 'ahash'] imagematches, searchtime = ImageSearch_Algo_Hash.HASH_SEARCH_HYBRIDTREE( myHybridtree, mydataHASH, q_path,hashAlgoList=HybridAlgoList, hashsize=16, returnCount=returnCount) if write: a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagematches, 20 ) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) row_dict['acc_HASH_Hybrid'] = a row_dict['index_HASH_Hybrid'] = ind row_dict['Count_HASH_Hybrid'] = cnt row_dict['quality_HASH_Hybrid'] = d row_dict['time_HASH_Hybrid'] = searchtime return imagematches, searchtime
def search_RGB_Corr(returnCount=100, mydataRGB=mydataRGB, write=False): imagematchesrgb , searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH(mydataRGB, q_path, correl_threshold=RGB_PARAMETERCORRELATIONTHRESHOLD) # y= autothreshold (imagematchesrgb) # toplist = toplist + y # print (y) if write: a, d, ind, cnt = accuracy.accuracy_matches(q_path, imagematchesrgb, 20) row_dict['acc_rgb_corr'] = a row_dict['index_rgb_corr'] = ind row_dict['Count_rgb_corr'] = cnt row_dict['quality_rgb_corr'] = d row_dict['time_rgb_corr'] = searchtimergb # print ('RGB Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) return imagematchesrgb , searchtimergb
def search_RGB(returnCount=100, mydataRGB=mydataRGB, write=False) : imagematchesrgb , searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH_TREE (myRGBtree, mydataRGB, q_path, returnCount=returnCount) # y= autothreshold (imagematchesrgb) # toplist = toplist + y # print (y) if write: a, d, ind, cnt = accuracy.accuracy_matches(q_path, imagematchesrgb, 20) row_dict['acc_rgb'] = a row_dict['index_rgb'] = ind row_dict['Count_rgb'] = cnt row_dict['quality_rgb'] = d row_dict['time_rgb'] = searchtimergb # print ('RGB Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', ind) return imagematchesrgb , searchtimergb
def search_HSV(returnCount=100, write=False): imagematcheshsv , searchtimehsv = ImageSearch_Algo_HSV.HSV_SEARCH_TREE ( myHSVtree, mydataHSV, q_path, returnCount=returnCount) if write: a, d, i_hsv, cnt = accuracy.accuracy_matches(q_path, imagematcheshsv, 20) row_dict['acc_hsv'] = a row_dict['index_hsv'] = i_hsv row_dict['Count_hsv'] = cnt row_dict['quality_hsv'] = d row_dict['time_hsv'] = searchtimehsv # print ('HSV Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', i_hsv) # x = autothreshold (imagematcheshsv) # toplist = toplist + x # # print (x) return imagematcheshsv , searchtimehsv
scores, ind = HSVtree.query(F, k=100) t = time.time() - start print (ind) print (scores) # get the index of searchimage print (mydataHSV.index[mydataHSV['file'] == q_path]) print (q_path) print ( "Search took ", t, ' secs') # Zip results into a list of tuples (score , file) & calculate score flist = list (mydataHSV.iloc[ ind[0].tolist()]['file']) slist = list (scores[0]) result = tuple(zip( slist, flist)) # create a list of tuples from 2 lists a , q, pos, cnt = accuracy.accuracy_matches(q_path, result, 100) print ('Accuracy =', a, '%', '| Quality:', q ) print ('Count', cnt, ' | position', pos) # -------------------- KD Tree HASH (phash, ahash, dhash, whash) --------------------- # Avg. Time per search: 0.033 s from sklearn.neighbors import KDTree import imagehash import numpy as np import time # YD = np.array(mydataHASH['phash'].apply(imagehash.ImageHash.__hash__)) YD = list(mydataHASH['ahash'])
# ------------------ SEARCH TEST # q_path = random.sample(fileterd_file, 1)[0] # q_path = './imagesbooks/ukbench00481.jpg' # imagepredictions , searchtime = SIFT_SEARCH(mydata1, q_path, 50 ,0.75, 20) imagepredictions, searchtime = ImageSearch_Algo_SIFT(mydatasift, q_path, 100, 0.75, 20) # to reload module: uncomment use the following # %load_ext autoreload # %autoreload 2 import Accuracy as accuracy a, b, c, d = accuracy.accuracy_matches(q_path, imagepredictions, 50) print('Accuracy =', a, '%', d, searchtime) # import ImageSearch_Plots as myplots # myplots.plot_predictions(imagepredictions[:20], q_path) #---------------- Compile data and plot results # q_paths = random.sample(imagepaths, 50) # random sample 100 items in list # q_paths = ['./imagesbooks/ukbench05960.jpg', './imagesbooks/ukbench00459.jpg', './imagesbooks/ukbench06010.jpg', './imagesbooks/ukbench06104.jpg', './imagesbooks/ukbench00458.jpg'] q_paths = [ './imagesbooks/ukbench05960.jpg', './imagesbooks/ukbench00459.jpg', './imagesbooks/ukbench06010.jpg', './imagesbooks/ukbench06104.jpg', './imagesbooks/ukbench00458.jpg', './imagesbooks/ukbench00248.jpg', './imagesbooks/ukbench06408.jpg', './imagesbooks/ukbench00303.jpg', './imagesbooks/ukbench03124.jpg', './imagesbooks/ukbench05776.jpg',
def algomixerFunnel (algos, return_count, finalalgo, finalalgoDataframe, algoname='NewAlgo', write=False) : ''' algos [list]: list of candidare algos return_count (int) : number of candidates to be generated finalalgo (str) : list of candidare algos finalalgoDataframe (pd.Dataframe): dataframe of the finalAlgo to be filtered and used algoname (str) : 'column name of datframe for the final reported accuracy, time etc. write (bool): True / False -> whether to report funnel accurracy, time before merge with thresholded candidates ''' # myAlgos = [ search_RGB, search_SIFT_BF ] # algomixerFunnel (myAlgos) start = time.time() algoResults = [] algoTimes = [] for algo in algos: algoResult, algoTime = algo_selector (algo, return_count=return_count) algoResults.append(algoResult) algoTimes.append(algoTime) # generate candidates (short listing) filteredFeatureData = Thresholding.filter_candidates( algoResults, finalalgoDataframe) # run Final Algo (detection) imagepredictions,searchtimesift = algo_selector_final(finalalgo,filteredFeatureData,return_count=return_count) if write: # find accuracy and append to dict a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagepredictions, 20) t = time.time() - start row_dict['acc_'+ algoname + '_BM'] = a row_dict['index_'+ algoname + '_BM'] = ind row_dict['Count_'+ algoname + '_BM'] = cnt row_dict['quality_'+ algoname + '_BM'] = d row_dict['time_'+ algoname + '_BM'] = t # generate algo uniques: apply threshold for each Result (imagematches) unique_final_list = [] for Result in algoResults : unique_final_list.append(Thresholding.autothreshold_knee(Result)) # print (unique_final_list) # MERGE OPEARATION FROM ALL RESULTS # algo uniques + final algo detections results final_algo_List = Thresholding.imagepredictions_to_list(imagepredictions) toplist = unique_final_list.copy() # copy all commons from candidates threshold list to front (2 lists) toplist = [Thresholding.merge_results( toplist, False)] # Add final algo derivatives to toplist toplist.append(final_algo_List) # merge lists of algo results: HSV Thresh, RGB Thresh, SIFT toplist = Thresholding.merge_results( toplist, False) t = time.time() - start # find accuracy and append to dict a ,d, ind, cnt = accuracy.accuracy_from_list(q_path, toplist, 20 ) print ('index F_'+algoname+': ', ind) row_dict['acc_'+ algoname] = a row_dict['index_'+ algoname] = ind row_dict['Count_'+ algoname] = cnt row_dict['quality_'+ algoname] = d row_dict['time_'+ algoname] = t
def search_AlgoA ( candidates=100, verbose=False ): toplist = [] start = time.time() # run RGB imagematchesrgb , searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH_TREE (myRGBtree, mydataRGB, q_path, returnCount=candidates) # run HSV imagematcheshsv , searchtimehsv = ImageSearch_Algo_HSV.HSV_SEARCH_TREE ( myHSVtree, mydataHSV, q_path, returnCount=candidates) # create shortlist for SIFT filteredSIFTData = Thresholding.filter_sift_candidates( [imagematcheshsv, imagematchesrgb], mydataSIFT) # run SIFT imagepredictions , searchtimesift = ImageSearch_Algo_SIFT.SIFT_SEARCH_BF(filteredSIFTData, q_path, sift_features_limit=100 , lowe_ratio=LOWE_RATIO, predictions_count=SIFT_PREDICTIONS_COUNT) # threshold RGB final_RGB_List = Thresholding.autothreshold_knee(imagematchesrgb) # thresold HSV final_HSV_List = Thresholding.autothreshold_knee(imagematcheshsv) # MERGE OPEARATION FROM ALL RESULTS # SIFT LIST final_SIFT_List = Thresholding.imagepredictions_to_list(imagepredictions) # merge lists of algo results: HSV Thresh, RGB Thresh, SIFT toplist = Thresholding.merge_results([final_HSV_List, final_RGB_List, final_SIFT_List], False) # find accuracy and append to dict a ,d, ind, cnt = accuracy.accuracy_from_list(q_path, toplist, 20 ) t = time.time() - start row_dict['acc_algo_A'] = a row_dict['index_algo_A'] = ind row_dict['Count_algo_A'] = cnt row_dict['quality_algo_A'] = d row_dict['time_algo_A'] = t # run if verbose enabled; DEBUGGING if verbose: print ('index FINAL AlgoA: ', ind) # append SIFT Results a ,d, ind, cnt = accuracy.accuracy_matches(q_path, imagepredictions, 20 ) print ('SIFT-A Accuracy =', a, '%', '| Quality:', d ) print ('SIFT-A Count', cnt, ' | position', ind) row_dict['acc_Algo_A_SIFT'] = a row_dict['index_Algo_A_SIFT'] = ind row_dict['Count_Algo_A_SIFT'] = cnt row_dict['quality_Algo_A_SIFT'] = d # row_dict['time_Algo_A_SIFT'] = searchtime # get current accurracy of RGB a, d, ind, cnt = accuracy.accuracy_matches(q_path, imagematchesrgb, 20) print ('index RGB : ', ind) # get thresholded accurracy of RGB a ,d, ind, cnt = accuracy.accuracy_from_list(q_path, final_RGB_List, 20 ) print ('index RGB Th:', ind) # update candidates RGB row_dict['index_Algo_A_cRGB'] = ind # get current accurracy for HSV a, d, ind, cnt = accuracy.accuracy_matches(q_path, imagematcheshsv, 20) print ('index HSV : ', ind) # get thresholded accurracy for HSV a ,d, ind, cnt = accuracy.accuracy_from_list(q_path, final_HSV_List, 20 ) print ('index HSV Th: ', ind) # update candidates row_dict['index_Algo_A_cHSV'] = ind return imagepredictions, t
matcheshsv = [] matchesrgb = [] for q_path in q_paths: toplist = [] start = time.time() row_dict = {'file':q_path } imagematcheshsv , searchtimehsv = ImageSearch_Algo_HSV.HSV_SEARCH_TREE (mytreeHSV, mydataHSV, q_path, 100) x = autothreshold (imagematcheshsv) toplist = toplist + x # print (x) a, d, i_hsv, cnt = accuracy.accuracy_matches(q_path, imagematcheshsv, 20) row_dict['hsv_acc'] = a row_dict['hsv_matchindex'] = i_hsv row_dict['hsv_Count'] = cnt row_dict['hsv_time'] = searchtimehsv print ('HSV Accuracy =', a, '%', '| Quality:', d ) print ('Count', cnt, ' | position', i_hsv) imagematchesrgb , searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH_TREE (mytreeRGB, mydataRGB, q_path, 100) y= autothreshold (imagematchesrgb) toplist = toplist + y # print (y) a, d, i_rgb, cnt = accuracy.accuracy_matches(q_path, imagematchesrgb, 20) row_dict['rgb_acc'] = a row_dict['rgb_matchindex'] = i_rgb
start = time.time() # get the feature for this image q_kp, q_des = ImageSearch_Algo_SIFT.FEATURE (q_path) # get bow cluster q_clustered_words = cluster_model.predict(q_des) # get FV histogram q_bow_hist = np.array([np.bincount(q_clustered_words, minlength=n_clusters)]) # search the KDTree for nearest match dist, result = SIFTtree2.query(q_bow_hist, k=100) t= time.time() - start print (result) print ('SIFT Search Tree: ', t , ' secs') flist = list (mydataSIFT.iloc[ result[0].tolist()]['file']) slist = list (dist[0]) matches = tuple(zip( slist, flist)) # create a list of tuples frm 2 lists a, q, pos, cnt = accuracy.accuracy_matches(q_path, matches, 20) print('Accuracy =', a, '%', '| Quality:', q) print('Count', cnt, ' | position', pos) ################# Unsupervised Clustering ################### # --------------------------------------------------------- # from sklearn.cluster import KMeans from sklearn.mixture import GaussianMixture # --------- find unsupervised cluster ID with KMEANS X = img_bow_hist km = KMeans(n_clusters=200) km.fit(X) km.predict(X) labels = km.labels_ print (labels) # # update labels to original dataframe
q_paths = random.sample(imagepaths, 20) # random sample 100 items in list # q_paths = ["./imagesbooks/ukbench00196.jpg", "./imagesbooks/ukbench00199.jpg", "./imagesbooks/ukbench00296.jpg", "./imagesbooks/ukbench00298.jpg", "./imagesbooks/ukbench00299.jpg", "./imagesbooks/ukbench00300.jpg", "./imagesbooks/ukbench00302.jpg", "./imagesbooks/ukbench00303.jpg", "./imagesbooks/ukbench02730.jpg", "./imagesbooks/ukbench02740.jpg", "./imagesbooks/ukbench02743.jpg", "./imagesbooks/ukbench05608.jpg", "./imagesbooks/ukbench05932.jpg", "./imagesbooks/ukbench05933.jpg", "./imagesbooks/ukbench05934.jpg", "./imagesbooks/ukbench05935.jpg", "./imagesbooks/ukbench05952.jpg", "./imagesbooks/ukbench05953.jpg", "./imagesbooks/ukbench05954.jpg", "./imagesbooks/ukbench05955.jpg", "./imagesbooks/ukbench05956.jpg", "./imagesbooks/ukbench05957.jpg", "./imagesbooks/ukbench05958.jpg", "./imagesbooks/ukbench05959.jpg", "./imagesbooks/ukbench06148.jpg", "./imagesbooks/ukbench06149.jpg", "./imagesbooks/ukbench06150.jpg", "./imagesbooks/ukbench06151.jpg", "./imagesbooks/ukbench06558.jpg", "./imagesbooks/ukbench06559.jpg", "./imagesbooks/ukbench07285.jpg", "./imagesbooks/ukbench07588.jpg", "./imagesbooks/ukbench07589.jpg", "./imagesbooks/ukbench07590.jpg", "./imagesbooks/ukbench08540.jpg", "./imagesbooks/ukbench08542.jpg", "./imagesbooks/ukbench08592.jpg"] counter = 1 for q_path in q_paths: imagematchesrgb, searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH_TREE( myRGBtree, mydataRGB, q_path, 100) # imagematchesrgb , searchtimergb = ImageSearch_Algo_RGB.RGB_SEARCH(mydataRGB, q_path, 0.7) # to reload module: uncomment use the following # %load_ext autoreload # %autoreload 2 import Accuracy as accuracy a, d, i_rgb, cnt = accuracy.accuracy_matches(q_path, imagematchesrgb, 20) # print ('Accuracy =', a, '%', '| Quality:', d ) # print ('Count', cnt, ' | position', i_rgb) # import ImageSearch_Plots as myplots # myplots.plot_predictions(imagematchesrgb, q_path) score = [] matchesposition = [0] * len(imagematchesrgb) for i in i_rgb: matchesposition[i] = 1 # score, file = item for item in imagematchesrgb: x, y = item score.append(x)
mydataHSV, mytime = ImageSearch_Algo_HSV.HSV_GEN(imagepaths) print('HSV Feature Generation time', mytime) #------------ HSV SEARCH TEST------------------------------# q_path = random.sample(imagepaths, 1)[0] # imagematches , searchtime = ImageSearch_Algo_HSV.HSV_SEARCH(mydataHSV, q_path) imagematches, searchtime = ImageSearch_Algo_HSV.HSV_SEARCH(mydataHSV, q_path) print('HSV Search time', searchtime) # to reload module: uncomment use the following # %load_ext autoreload # %autoreload 2 a, m, pos, cnt = accuracy.accuracy_matches(q_path, imagematches, 20) print('Accuracy =', a, '%', ' | Quality: ', m) # ----- Alternative tree search code [Optimized search time ] # test TREE SEARCH code # to create a new tree from dataframe features 'mydataHSV' mytree = ImageSearch_Algo_HSV.HSV_Create_Tree(mydataHSV, savefile='HSV_Tree') # to load an existing tree thistree = ImageSearch_Algo_HSV.HSV_Load_Tree('HSV_Tree') # sample 1 image q_path = random.sample(imagepaths, 1)[0]