def saveUrl(url, img_dir): try: file = urllib.URLopener() directory = ImgManagement.getTimeDir(img_dir) if not os.path.exists(directory): os.makedirs(directory) path = os.path.join(directory, str(randint(0, 100000000))) while os.path.isfile(path): path = os.path.join(directory, str(randint(0, 100000000))) file.retrieve(url, path) md5 = ImgManagement.getMD5(path) if ImageDB.getItem({"md5": md5}) is not None: ImgManagement.deleteFile(path) else: image = Image.open(path) cd = ColorDescriptor((8, 12, 3)) features = cd.describe(image) ImageDB.insert(md5, features, path) ImageDB.getList(True) except: print("*** ImageManagement saveUrl takes error ***") print(sys.exc_info()[0]) traceback.print_exc() print("*** ImageManagement saveUrl takes error ***")
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io query = io.imread(image_url) features = cd.describe(query) # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append( {"image": str(resultID), "score": str(score)}) # return success return jsonify(results=(RESULTS_ARRAY[::-1][:3])) except: # return error jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def Disease_Detection(image_path): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it # query = cv2.imread(args["query"]) filenames = listdir(image_path) a = [filename for filename in filenames if filename.endswith('.png')] path = image_path + '/' + a[0] print(path) query = cv2.imread(path) print("query==", query) features = cd.describe(query) # perform the search #searcher = Searcher(args["index"]) searcher = Searcher("../index.csv") results = searcher.search(features) results.pop(0) print("==============", results) print() # disease_name = results[1][1].split("_")[0].split("\\")[1] #.... extracting disease name # print("**********",disease_name) return (results[1][1].split("_")[0]) # Disease_Detection('/home/vishal/Dropbox/ML/Plant_Disease_Detection/queries')
def browse_query_img(self): self.query_img_frame = Frame(self.master) self.query_img_frame.pack() from tkFileDialog import askopenfilename self.filename = tkFileDialog.askopenfile( title='Choose an Image File').name # process query image to feature vector # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) classifierFilePath = './pyimagesearch/VWClassifier.dat' fe = FeatureEvaluator(classifierFilePath) # load the query image and describe it query = cv.imread(self.filename) self.queryfeatures = cd.describe(query) # print len(self.queryfeatures) = 1440 self.vmfeatures = fe.predictFromImage(query, k=1469) self.trfeatures = TRS.TRSolver(self.filename).result self.sffeatures = TFSFS.SemanticFeatureSolver(self.filename).result # show query image image_file = Image.open(self.filename) resized = image_file.resize((100, 100), Image.ANTIALIAS) im = ImageTk.PhotoImage(resized) image_label = Label(self.query_img_frame, image=im) image_label.pack() self.query_img_frame.mainloop()
def indexer(): args={'index': 'index.csv', 'dataset': 'dataset'} cd = ColorDescriptor((8, 12, 3)) output = open(args["index"], "w") i=0 paths =[os.path.join(dirpath,f) for dirpath,dirnames,files in os.walk(args["dataset"]) for f in files if (f.endswith('.jpg') | (f.endswith('.JPG')))] for imagePath in paths: if(imagePath.count('/')>1): choosenfile = imagePath.split('/') choosenfile = choosenfile[::-1] imageID = choosenfile[1]+'/'+choosenfile[0] else: imageID=imagePath[imagePath.rfind("/")+1:] image = cv2.imread(imagePath) # describe the image features = cd.describe(image) # write the features to file features = [str(f) for f in features] output.write("%s,%s\n" % (imageID, ",".join(features))) i+=1 # close the index file output.close() return i
def find_similar(query): """ query (str): path to the img ex : queries2\img_231.jpg return list_img (list) : 3 paths of img similar """ # initialize list list_img = [] # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread(query) features = cd.describe(query) # perform the search searcher = Searcher("index2.csv") results = searcher.search(features) # display the query # cv2.imshow("Query", query) # loop over the results for (score, resultID) in results: # add img to list list_img.append(str(resultID).replace("\\", "/")) return list_img
def check(limit): args = {"query": "queries/a.jpg", "index": "whpu.csv"} #初始化图像描述符 cd = ColorDescriptor((8, 12, 3)) #从磁盘读取待搜索图像 query = cv2.imread(args["query"]) #提取该图像的特征 features = cd.describe(query) #执行搜索 #使用提取到的特征进行搜索,返回经过排序后的结果列表 searcher = Searcher(args["index"]) results = searcher.search(features, limit) #显示出待搜索的图像 #cv2.imshow("Query", query) result_files_data = [] #遍历搜索结果,将相应的图像显示在屏幕上 for (score, resultID) in results: result = "dataset/" + resultID result_files_data.append([score, result]) return result_files_data
def preprocess_image(self): # process query image to feature vector # initialize the image descriptor #cd = ColorDescriptor((8, 12, 3)) cd = ColorDescriptor((3, 3, 2)) sr = SemanticsReader() # load the query image and describe it query = cv2.imread(self.filename) self.queryfeatures = cd.describe(query) # convert query image to list of texts (if any) reader = csv.reader(open("dataset\\dataset\\combined_text_tags.txt"), delimiter=" ") # find the line belonging to query image, req_line = None if no tags found img_name = self.filename.split("/")[-1] req_line = None for line in reader: if (line[0] == img_name): req_line = line[6:] break self.querytext = req_line # process query image to semantics vector # generate temp.txt for exe to run on it. tempfile = open("semanticFeature\\temp.txt", "w") tempfile.write(self.filename) tempfile.close() # generate the txt file with 1000D for query FNULL = open(os.devnull, 'w') #suppress output to stdout os.chdir("semanticFeature") # Check if txt file with 1000D already exists. reqfile = self.filename base, ext = os.path.splitext(reqfile) req_text_file = base + ".txt" if (not os.path.isfile(req_text_file)): args = "./image_classification.exe temp.txt" #subprocess.call(args, stdout=FNULL, stderr=FNULL) subprocess.call(args) os.chdir("../") # read 1000D vector for semantics self.querysemantics = sr.read(base + ".txt") # convert query image to grayscale convertQueryToGray(self.filename) # generate key file for query #with open("sift/temp/query.pgm","rb") as ip, open("sift/temp/query.key","wb") as op: # subprocess.call("sift/siftWin32.exe",stdin=ip,stdout=op) # Do deep learning # self.querycategory = deepSearch(self.filename) # show query image '''image_file = Image.open(self.filename)
def search(): if request.method == "POST": '''file = request.files['file'] filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) RESULTS_ARRAY = [] print(filename)''' RESULTS_ARRAY = [] # get url image_url = request.form.get('img') print(image_url) try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 #query = io.imread("/home/kene/Documents/PyImageSearch/3D Histogram Descriptor Method With Web Interface/app/"+image_url) query = cv2.imread("/home/kene/Documents/Projects/Image-Search-Engine/app/static/queries/"+image_url); #query = io.imread(image_url) #query = (query * 255).astype("uint8") #(r, g, b) = cv2.split(query) #query = cv2.merge([b, g, r]) #query = cv2.cvtColor(query, cv2.COLOR_BGR2RGB) features = cd.describe(query) # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append( {"image": str(resultID), "score": str(score)}) # return success return jsonify(results=(RESULTS_ARRAY[::-1][:101]), preview="queries/"+image_url) #resultSet=jsonify(results=(RESULTS_ARRAY[::-1][:5])) #return render_template('index.html', preview = "static/queries/"+filename, resultSet = jsonify(results=(RESULTS_ARRAY[::-1][:5]))) except Exception, e: print(str(e)) # return error return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 # query = io.imread(image_url) # query = (query * 255).astype("uint8") # (r, g, b) = cv2.split(query) # query = cv2.merge([b, g, r]) image_url = "app/" + image_url[1:] # print "图像url路径:", image_url # print os.getcwd() # print sys.path[0] query = cv2.imread(image_url) # print "读取成功!" features = cd.describe(query) # print "描述子生成成功" # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append({ "image": str(resultID), "score": str(score) }) # return success return jsonify(results=(RESULTS_ARRAY[:5])) except: # return error jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 # query = io.imread(image_url) # query = (query * 255).astype("uint8") # (r, g, b) = cv2.split(query) # query = cv2.merge([b, g, r]) image_url = "app/" + image_url[1:] # print "图像url路径:", image_url # print os.getcwd() # print sys.path[0] query = cv2.imread(image_url) # print "读取成功!" features = cd.describe(query) # print "描述子生成成功" # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append( {"image": str(resultID), "score": str(score)}) # return success return jsonify(results=(RESULTS_ARRAY[:5])) except: # return error jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def predict(self,location): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) print(location) query = cv2.imread(location) features = cd.describe(query) searcher = Searcher("index.csv") results = searcher.search(features) final_res = [] for (score, resultID) in results: final_res.append(resultID) print(score,resultID) return final_res[0]
def upload(): file = request.files['file'] print('upload', file) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if request.method == "POST": RESULTS_ARRAY = [] # get url try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io filename = os.path.join(app.config['UPLOAD_FOLDER'], filename) print '###' + filename query = io.imread(filename) print 'shape' features = cd.describe(query) # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append({ "image": str(resultID), "score": str(score) }) # return success return jsonify(results=(RESULTS_ARRAY[::-1][:3]), file=file.filename) except: # return error return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 query = io.imread(image_url) # query = (query * 255).astype("uint8") # (r, g, b) = cv2.split(query) # query = cv2.merge([b, g, r]) # cv2.imshow("Image", query) query = cv2.cvtColor(query, cv2.COLOR_BGR2RGB) features = cd.describe(query) # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append({ "image": str(resultID), "score": str(score) }) # return success return jsonify(results=(RESULTS_ARRAY[::-1])) # return 'ok' except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print(message) return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 print("image_url ", image_url) path_img = os.path.join(os.getcwd(), image_url[1:]) query = cv2.imread(path_img, cv2.COLOR_BGR2RGB) print("describe") features = cd.describe(query) print("Searcher") # perform the search searcher = Searcher(INDEX) print("search") results = searcher.search(features) print(INDEX) print("image") # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append({ "image": str(resultID), "score": str(score) }) print("Before success") # return success return jsonify(results=(RESULTS_ARRAY[::-1][:3])) except: # return error jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def search(): if request.method == "POST": RESULTS_ARRAY = [] # get url image_url = request.form.get('img') try: # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it from skimage import io import cv2 query = cv2.imread( os.path.join(os.path.dirname(__file__), 'static/images/' + image_url)) features = cd.describe(query) # perform the search searcher = Searcher(INDEX) results = searcher.search(features) # loop over the results, displaying the score and image name for (score, resultID) in results: RESULTS_ARRAY.append({ "image": str(resultID), "score": str(score) }) # return success return jsonify(results=(RESULTS_ARRAY[:101]), preview="images/" + image_url) except Exception as e: print(str(e)) # return error return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
def func2(command): folder = 'target' command = os.path.join(folder, command) # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread(command) features = cd.describe(query) # perform the search searcher = Searcher("index.csv") results = searcher.search(features) # loop over the results result = [] for (score, resultp) in results: url, imgname, imgurl = resultp doct = {"url": url, 'name': imgname, 'imgurl': imgurl} result.append(doct) return result
def Disease_Detection(image_path): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it # query = cv2.imread(args["query"]) filelist = [ file for file in os.listdir(image_path) if file.endswith('.png') ] query = cv2.imread(image_path + filelist[0]) # query = cv2.imread( 'D:\\projects\\Plant_Disease_Detection\\dataset\\CollarRot_1.png' ) features = cd.describe(query) # perform the search #searcher = Searcher(args["index"]) searcher = Searcher(image_path + "/index.csv") results = searcher.search(features) results.pop(0) disease_name = results[1][1].split("_")[0].split("\\")[ 1] #.... extracting disease name return (disease_name)
def handle_upload_file(f): #存储文件 fobj = open('test.file','w') for chunk in f.chunks(): fobj.write(chunk) fobj.close() #初始化工作 conn = db.connect(host='localhost',user='******',passwd='dg123321',db='project') cur = conn.cursor() cd = ColorDescriptor((8, 12, 3)) rows = cur.execute('select url,width,height,time,type,hist from image') dataset = cur.fetchall() img1 = cv2.imread('test.file') feature = cd.describe(img1) #多进程 start,end,step= 0,0,100 processes = rows/step + 1 if rows%step else 0 pool = Pool(processes) operand = [] for i in range(processes): end = (end + step) if (rows - end > step) else (rows + 1) operand.append([dataset[start:end],feature[:]]) start = end result = [] for item in pool.imap(comp,operand): result.extend(item) result = sorted(result) final = {} for i in range(processes): temp = {} temp['name'] = 'item'+str(i) temp['url'] = result[i][1][0] temp['width'] = result[i][1][1] temp['height'] = result[i][1][2] temp['time'] = result[i][1][3] temp['type'] = result[i][1][4] final[str(i)] = temp return final
"--index", required=True, help="Path to where the computed index will be stored") ap.add_argument("-q", "--query", required=True, help="Path to the query image") ap.add_argument("-r", "--result-path", required=True, help="Path to the result path") args = vars(ap.parse_args()) # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread(args["query"]) features = cd.describe(query) # perform the search searcher = Searcher(args["index"]) results = searcher.search(features) # display the query cv2.imshow("Query", query) c = 0 r = [] # loop over the results for (score, resultID) in results: # load the result image and display it result = cv2.imread(args["result_path"] + "/" + resultID) cv2.imshow("Result", result) r.append(result) cv2.waitKey(0)
# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--index", required=True, help="Path to where the computed index will be stored") ap.add_argument("-q", "--query", required=True, help="Path to the query image") ap.add_argument("-r", "--result-path", required=True, help="path to the result path") args = vars(ap.parse_args()) # initialize the image descriptor cd = ColorDescriptor((8,12,3)) # load the query image and describe it query = cv2.imread(args["query"]) features = cd.describe(query) # perform the search searcher = Searcher(args["index"]) results = Searcher.search(features) # display the query cv2.imshow("Query",query) # loop over the results for (score, resultID) in results: # load the result image and display it result = cv2.imread(args["result_path"] + "/" + resultID) cv2.imshow("Result", result) cv2.waitKey(0)
# ==================================================================================================== # This file is reponsible for extracting features from the training images and write them to index.csv # ==================================================================================================== from pyimagesearch.colordescriptor import ColorDescriptor import glob import cv2 from keras.datasets import cifar10 (X_train, y_train), (X_test, y_test) = cifar10.load_data() cd = ColorDescriptor((8, 12, 3)) # Write feature info of each training image to index.csv output = open("index.csv", "w") for image in X_test: features = cd.describe(image) # extract features from each image features = [str(f) for f in features] output.write(",".join(features) + "\n") output.close()
import os # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required = True, help = "Path to the directory that contains the images to be indexed") ap.add_argument("-i", "--index", required = True, help = "Path to where the computed index will be stored") args = vars(ap.parse_args()) # initialize the color descriptor cd = ColorDescriptor() # open the output index file for writing output = open(args["index"], "w") count = 0 for imagePath in glob.glob(args["dataset"] + "/*.jpg"): # extract the image ID (i.e. the unique filename) from the image # path and load the image itself imageID = imagePath[imagePath.rfind("/") + 1:] image = cv2.imread(os.path.abspath(imagePath)) # describe the image features = cd.describe(image) #ignore image if empty features if features ==[] or features is None: continue features = features.ravel().tolist() # write the features to file output.write("%s,%s\n" % (imageID, features)) # close the index file output.close()
def searchVideo(query): index = "index.csv" # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query video and describe it temp = "/home/ankita/btp7/CBVR/temp" i = 0 # print path_video cap = cv2.VideoCapture("uploadedFiles/" + query) if cap.isOpened(): rval, frame = cap.read() else: rval = False # print rval # print query while rval: rval, frame = cap.read() if not rval: break name = str(i) + '.png' cv2.imwrite(os.path.join(temp, name), frame) i = i + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() avg_int = [] no_frames = i # print no_frames for img in glob.glob(temp + '/*png'): image = io.imread(img) im = rgb2gray(image) avg_int.append(np.mean(im)) # print(np.mean(im)) # for i in avg_int: # print i diff = [avg_int[p + 1] - avg_int[p] for p in range(len(avg_int) - 1)] # y = diff # plt.plot(y) # plt.show() # print diff.index(min(diff)) name1 = temp + '/' + str(diff.index(min(diff))) + '.png' # print diff.index(max(diff)) name2 = temp + '/' + str(diff.index(max(diff))) + '.png' # image1 = cv2.imread(name1) # save_name = fold +"#"+ video.split('.')[0]+'_1.png' # cv2.imwrite(os.path.join(data ,save_name ), image1) # image2 = cv2.imread(name2) # save_name = fold +"#"+ video.split('.')[0]+'_2.png' # cv2.imwrite(os.path.join(data ,save_name ), image2) query_image = cv2.imread(name2) for the_file in os.listdir(temp): file_path = os.path.join(temp, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) #elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print(e) # query = cv2.imread(args["query"]) features = cd.describe(query_image) # perform the search searcher = Searcher(index) results = searcher.search(features) result_images = [] # display the query # cv2.imshow("Query", query_image) # loop over the results path_video = [] # path_video.append("uploadedFiles/"+query) for (score, resultID) in results: # load the result image and display it result = cv2.imread("data/" + resultID) result_images.append(result) fold = resultID.split('#')[0] x = resultID.split('#')[1] name = x.rsplit('_', 1)[0] # print fold arr = os.listdir('data/' + fold) ext = arr[0].split('.')[1] video = 'data/' + fold + '/' + name + '.' + ext path_video.append(video) # print video # cv2.imshow("Result", result) # cv2.waitKey(0) return path_video
# path and load the image itself imgObj = {} imgObj["ImageName"] = imagePath[imagePath.rfind("/") + 1:] image_url = None if 'url' in args and args['url'] is not None: image_url = args['url'].strip('/') + '/' + imgObj["ImageName"] file = cStringIO.StringIO(urllib.urlopen(image_url).read()) image_src = Image.open(file) image = np.array(image_src) if image_url is None: image_src = Image.open(imagePath) image = np.array(image_src) imgObj[Feature.HSV] = [x for x in hsv_cd.describe(image)] imgObj[Feature.LUV] = luv_cd.describe(image) phash = imagehash.phash(image_src) imgObj["PHash"] = [x.item() for x in phash.hash.flatten()] except Exception, e: print(e) print("cannot get hsv luv:" + imagePath) continue # add labels try: labels, probs = clf.predict_label_proba(image) except Exception, e: print(e) print("can not predict image" + imagePath) print("shape:" + str(image.shape))
ap.add_argument("-i", "--index", required=False, default='index.csv', help="Path to where the computed index will be stored") args = vars(ap.parse_args()) # initialize the color descriptor #cd = ColorDescriptor((8, 12, 3)) cd = ColorDescriptor((3, 3, 2)) # open the output index file for writing output = open(args["index"], "w") # use glob to grab the image paths and loop over them for imagePath in glob.glob(args["dataset"] + "/*.jpg"): # extract the image ID (i.e. the unique filename) from the image # path and load the image itself imageID = imagePath[imagePath.rfind("/") + 1:] image = cv2.imread(imagePath) # describe the image features = cd.describe(image) # write the features to file features = [str(f) for f in features] output.write("%s,%s\n" % (imageID, ",".join(features))) # close the index file output.close()
for imgItem in imgList: image = None if "ImageUrl" in imgItem: try: image = io.imread(imgItem["ImageUrl"]) except: print("unable to fetch image:%s", imgItem["ImageUrl"]) if image is None and "Path" in imgItem: try: image = cv2.imread("." + imgItem["Path"]) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) except: print("unable to fetch image:%s", imgItem["Path"]) if image is None and "Path" not in imgItem and "ImageUrl" not in imgItem: print("unable to fetch image:%s", imgItem) continue if image is None: continue hsv_feature = cd.describe(image) imgItem["HSVFeature"] = hsv_feature collection.replace_one({"_id": imgItem["_id"]}, imgItem) count += 1 if count % 100 == 0: print(count) print(" --- %s seconds ---" % (time.time() - start_time)) print(count) print("finish --- %s seconds ---" % (time.time() - start_time))
return crop_img else: return image # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread(args["query"]) #print(crop(query)) cropped_query=crop(args["query"]) #cv2.imshow("Query", cropped_query) features = cd.describe(cropped_query) #cv2.imshow("Query", query) #features = cd.describe(query) # perform the search searcher = Searcher(args["index"]) results = searcher.search(features) # display the query cv2.imshow("Query", cropped_query) #cv2.imshow("Query", query) # loop over the results for (score, resultID) in results:
ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required = True, help = "path of folder of image") ap.add_argument("-i", "--index", required = True, help = "path where the new images would be stored") args = vars(ap.parse_args()) cd = ColorDescriptor((8, 12, 3)) output = open(args["index"], "w") for imagePath in glob.glob(args["dataset"] + "/*.png"): imageID = imagePath[imagePath.rfind("/") + 1:] image = cv2.imread(imagePath) features = cd.describe(image) features = [str(f) for f in features] output.write("%s,%s\n" % (imageID, ",".join(features))) output.close() #3.Tagging each image with its specific tag type class Searcher: def __init__(self, indexPath): self.indexPath = indexPath def search(self, queryFeatures, limit = 10): with open(self.indexPath) as f:
def callback(file): args={'result_path': 'dataset', 'index': 'index.csv', 'query': ''} args['query']=file # # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # # # load the query image and describe it query = cv2.imread(args["query"]) features = cd.describe(query) # # # perform the search searcher = Searcher(args["index"]) results = searcher.search(features) # return results # display the query #cv2.imshow("Query", query) #loop over the results # count=0 # print "Note: lower % of matching shows maximum similarity." # for (score, resultID) in results: # # load the result image and display it # i=0 # total=100 # perc=int(score) # while(i<=perc): # total-=5+i; # i+=1 # print "Image "+resultID +" got "+str(total+5)+"% similarity and "+'%2.3f'%score+" score.." # result = cv2.imread(args["result_path"] + "/" + resultID) # cv2.imwrite("/home/venkat/Review/output/"+str(score)+" "+resultID, result) # count+=1 # print "Got "+str(count)+" similer results...." # root = Tk() # root.geometry('{}x{}'.format(1330, 760)) # root.title("Content Based Image Retrieval") # label=Label(root,text = 'Resultant similer images..') # label.bind() # c=0 # r=0 # path='/home/venkat/CBIR/dataset/' # output='/home/venkat/CBIR/' # print "Note: lower % of matching shows maximum similarity." # for (score, resultID) in results: # # load the result image and display it # i=0 # total=100 # perc=int(score) # while(i<=perc): # total-=5; # i+=1 # print "Image "+resultID +" got "+str(total)+"% similarity and "+'%2.3f'%score+" score.." # path1 = Image.open("/home/venkat/Review/dataset/"+resultID) # path1=path1.resize((250, 250),Image.ANTIALIAS) # img1 = ImageTk.PhotoImage(path1) # myvar=Label(root,image = img1) # myvar.image = img1 # myvar.grid(row=r,column=c) # c+=250 # if(c>1000): # r+=250 # c=0 # root.mainloop() # USAGE # python search.py --index index.csv --query queries/103100.png --result-path dataset
def search(self, fileName): query = cv.imread(fileName) #Added check for file exist if query is None: return [] cd = ColorDescriptor((8, 12, 3)) classifierFilePath = './pyimagesearch/VWClassifier.dat' fe = FeatureEvaluator(classifierFilePath) self.queryfeatures = cd.describe(query) self.vmfeatures = fe.predictFromImage(query, k=-1) self.trfeatures = TRS.TRSolver(fileName).result self.sffeatures = TFSFS.SemanticFeatureSolver(fileName).result colorSearcher = colorHistogramSearcher("index.csv") colorResults = colorSearcher.search(self.queryfeatures) color_max = max(score for (score, resultID) in colorResults) chResults = [(1 - t[0] * (1 / color_max), t[1]) for t in colorResults] # perform VW search vm_max = max(score for (resultID, score) in self.vmfeatures) vmResults = [(1 - t[1] * (1 / vm_max), t[0]) for t in self.vmfeatures] # perform TR search tr_max = max(score for (resultID, score) in self.trfeatures) trResults = [(t[1], t[0]) for t in self.trfeatures] # perform SF search sfResults = [(t[1], t[0]) for t in self.sffeatures] #TODO: join results (top 16) image_dict = {} sf_weight = 0.6 vm_weight = 0.2 ch_weight = 0.1 tr_weight = 0.1 for (score, resultID) in sfResults: image_dict[resultID] = score * sf_weight for (score, resultID) in vmResults: if resultID in image_dict: image_dict[resultID] = image_dict[resultID] + score * vm_weight else: image_dict[resultID] = score * vm_weight for (score, resultID) in chResults: if resultID in image_dict: image_dict[resultID] = image_dict[resultID] + score * ch_weight else: image_dict[resultID] = score * ch_weight for (score, resultID) in trResults: if resultID in image_dict: image_dict[resultID] = image_dict[resultID] + score * tr_weight else: image_dict[resultID] = score * tr_weight k = 50 image_list = sorted(image_dict.items(), key=operator.itemgetter(1), reverse=True)[:k] result = [resultID for (resultID, score) in image_list] return result
from pyimagesearch.colordescriptor import ColorDescriptor import argparse import glob import cv2 import numpy from sklearn import datasets, svm, metrics # construct the argument parser and parse the arguments from the input ap = argparse.ArgumentParser() ap.add_argument( "-d", "--dataset", required=True, help="Path to the directory that contains the images to be indexed") ap.add_argument("-i", "--index", required=True, help="Path to where the computed index will be stored") args = vars(ap.parse_args()) cd = ColorDescriptor((8, 12, 3)) # initialize output = open(args["index"], "w") for imagePath in glob.glob(args["dataset"] + "/*.png"): imageID = imagePath[imagePath.rfind("/") + 1:] image = cv2.imread(imagePath) features = cd.describe(image) # describe the image using describe function features = [str(f) for f in features] # write the features to file output.write("%s,%s\n" % (imageID, ",".join(features))) output.close()