def main_fun(self): # initialize the color descriptor cd = ColorDescriptor((8, 12, 3)) # open the output index file for writing output = open(self.path, "w") #data contains the path to your images folder data = '/home/farhan/project/CBIR/my_contrib/test_dataset' # use glob to grab the image paths and loop over them types = ['/*.png', '/*.jpg'] # os.chdir(data) for filetype in types: for imagePath in glob.glob(data + filetype): # 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()
def main_fun(self): # initialize the color descriptor cd = ColorDescriptor((8, 12, 3)) # open the output index file for writing output = open(self.path, "w") #data contains the path to your images folder data='/home/farhan/project/CBIR/my_contrib/test_dataset' # use glob to grab the image paths and loop over them types=['/*.png','/*.jpg'] # os.chdir(data) for filetype in types: for imagePath in glob.glob(data+filetype): # 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()
def searchimg(self, index1='index1.csv', index2='index2.csv', result_path='jpg'): cd = ColorDescriptor((8,12,3)) vd = Vgg16Descriptor((224,224,3)) query = cv2.imread("./query/" + self.filename) feature1 = cd.describe(query) feature2 = vd.describe(self.querypath) searcher = Searcher(index1,index2) results = searcher.search(feature1,feature2) result0 = cv2.resize(query,(128,128),interpolation=cv2.INTER_CUBIC) name = locals() i = 1 for (score, resultID) in results: name['result%d'%i] = cv2.imread(result_path + "/" + resultID) name['result%d'%i] = cv2.resize(name['result%d'%i],(128,128),interpolation=cv2.INTER_CUBIC) i = i + 1 result_0 = np.hstack(name['result%d'%i] for i in range(1,6)) result_1 = np.hstack(name['result%d'%i] for i in range(6,11)) result = np.vstack((result_0,result_1)) cv2.imwrite("./result/%s" % self.filename, result) return self.filename
def indexpy(dataset, index): print('hello') cd = ColorDescriptor((8, 12, 3)) output = open(index, "w") for imagePath in glob.glob(dataset + "/*.jpg"): 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()
def search(img): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it #query = cv2.imread(args["query"]) #query = cv2.imread("queries/2.png") features = cd.describe(img) # perform the search searcher = Searcher("feature.csv") results = searcher.search(features) return results
def search_cut(self): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread("out.jpg") features = cd.describe(query) # perform the search searcher = Searcher("index.csv") results = searcher.search(features) # loop over the results for (score, resultID) in results: # load the result image and display it result = cv2.imread(resultID) cv2.imshow("Result", result) cv2.waitKey(0)
def create_features(path): # initialize the color descriptor cd = ColorDescriptor((4, 4, 2)) # use glob to grab the image paths and loop over them image_names, X = [], [] for imagePath in listdir(path): # extract the image ID (i.e. the unique filename) from the image # path and load the image itself imageID = basename(imagePath).split('.')[0] image = cv2.imread(join(path, imagePath)) # describe the image features = cd.describe(image) image_names.append(imageID) X.append(features) return np.array(image_names), np.array(X)
def create_features(path): # initialize the color descriptor cd = ColorDescriptor((4, 4, 2)) # use glob to grab the image paths and loop over them image_names, X = [], [] for imagePath in listdir(path): # extract the image ID (i.e. the unique filename) from the image # path and load the image itself imageID = basename(imagePath).split('.')[0] image = cv2.imread(join(path,imagePath)) # describe the image features = cd.describe(image) image_names.append(imageID) X.append(features) return np.array(image_names), np.array(X)
def search(index, query, result_path): # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query1 = cv2.imread(query) features = cd.describe(query1) # perform the search searcher = Searcher(index) results = searcher.search(features) # display the query #cv2.imshow("Query", query1) search_output = [] for (score, resultID) in results: #print(result_path+"/"+resultID) full_path = result_path + "/" + resultID #result=cv2.imread(result_path+"/"+resultID) #cv2.imshow("Result", result) #cv2.waitKey(0) search_output.append(resultID) #print(search_output) return search_output
def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No file selected for uploading') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # initialize the image descriptor cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it photos_result = [] query = cv2.imread(app.config['UPLOAD_FOLDER']+"/"+filename) features = cd.describe(query) query_photo = {"score": 100, "photo": "queries/"+filename} photos_result.append(query_photo) # perform the search searcher = Searcher("./index.csv") results = searcher.search(features) # loop over the results for (score, resultID) in results: score = log10(abs(score)) # load the result image and display it result = {"score": score, "photo": "dataset/"+resultID} photos_result.append(result) return render_template('result.html', results=photos_result) else: flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') return redirect(request.url)
def search(filename): #print(query) cd = ColorDescriptor((8, 12, 3)) query = cv2.imread(filename) query = cv2.resize(query, (416, 416)) filepath = 'static/' + filename cv2.imwrite(filepath, query) features = cd.describe(query) searcher = Searcher() results = searcher.search(features) #cv2.imshow("Query", query) # loop over the results for (score, resultID) in results: # load the result image and display it #print(resultID) img = mongo.db.myImages.find({'filename': resultID}, { "_id": 0, "name": 0, "filename": 0 }) l = [] for x in img: #print(x) x = x['images'][0] #print('hey') #print(x) fout = fs.get(x['imageID']) im = np.frombuffer(fout.read(), dtype=np.uint8) im = np.reshape(im, x['shape']) # cv2.imshow("Result", im) # cv2.waitKey(0) return results
def main_search(self): cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread('test_dataset/' + self.name) features = cd.describe(query) # perform the search searcher = Searcher(self.file_path) 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(self.data_path + "/" + resultID) r = 300.0 / result.shape[1] dim = (300, int(result.shape[0] * r)) resized = cv2.resize(result, dim, interpolation=cv2.INTER_AREA) cv2.imshow("RESULT", resized) cv2.waitKey(0) cv2.destroyAllWindows()
def main_search(self): cd = ColorDescriptor((8, 12, 3)) # load the query image and describe it query = cv2.imread('test_dataset/'+self.name) features = cd.describe(query) # perform the search searcher = Searcher(self.file_path) 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(self.data_path + "/" + resultID) r=300.0/result.shape[1] dim=(300,int(result.shape[0]*r)) resized = cv2.resize(result, dim, interpolation = cv2.INTER_AREA) cv2.imshow("RESULT", resized) cv2.waitKey(0) cv2.destroyAllWindows()
fea = [] final_fea = {} 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:] print imagePath, "imagePath" # print imageID,"imageID" image = cv2.imread(imagePath) # print image[210,210,1],"pixel" #print imageID,"imageID" # describe the image features = cd.describe(image) # write the features to file #features = [str(f) for f in features] #output.write("%s\n" % (imageID)) #output.write("%s\n" % (imageID)) #for f in features: #fea.append(f) #print f,"fea" final_fea[imageID] = features print len(final_fea), "shape" # print final_fea,"final_fea" # load the query image and describe it output.write("%s;" % ("testing image name")) output.write("%s\n" % ("training image name")) for imagePath1 in glob.glob(args["query"] + "/*.jpg"): imageID1 = imagePath1[imagePath1.rfind("/") + 1:]
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)) output = open(args["index"], "w") # use glob to grab the image paths and loop over them for imagePath in glob.glob(args["dataset"] + "/*.png"): 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()
from colordescriptor import ColorDescriptor from searcher import Searcher import argparse import cv2 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()) cd = ColorDescriptor((8, 12, 3)) query = cv2.imread(args["query"]) features = cd.describe(query) searcher = Searcher(args["index"]) results = searcher.search(features) query = cv2.resize(query, (960, 540)) cv2.namedWindow("Query", cv2.WINDOW_NORMAL) cv2.imshow("Query", query) cv2.waitKey(0) for (score, resultID) in results: result = cv2.imread(args["result_path"] + "/" + resultID) #result = cv2.resize(result, (960, 540)) cv2.namedWindow("Result", cv2.WINDOW_NORMAL) cv2.imshow("Result", result) #print(args["result_path"] + "/" + resultID) cv2.waitKey(0)
# initialize the color descriptor cd = ColorDescriptor( (8, 12, 3)) # HSV binning -- 8 hue, 12 saturation, 3 value features = [] ctrcorr = [] images = glob.glob(folderPath + '/*.jpg') for pic in images: image = cv2.imread(pic) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ctr = [] temp = cd.describe(image) features.append(temp) for i in temp[0:4]: ctr.append(np.corrcoef(i, temp[4])[1, 0]) ctrcorr.append(ctr) names = [t[t.rfind("/") + 1:] for t in images] zippy = zip(features, names, ctrcorr) """ Perform 3 kinds of checks: 1) How many peaks are there within a certain range of the first peak? Uniform background will have relatively few high peaks while variable background will have many lower peaks and no strong signals. Measure by counting the number of peaks within some threshold below the highest peak [peak-thresh:peak]. 1a) How dissimilar are the corners from the center of the image? If the corners are extremely uniform, this is a fairly lenient check (correlation between corners and center < 0.998, mostly just gets rid of tiled images), since often a fish will be partially transparent or similar in color to the background. 2) Of those without a single strong peak, are the four corners extremely similar? Measure using correlation coefficients between four corners. If at least two are above the threshold then we have at least two pairs of corners that are extremely similar (usually top two and bottom two). If # (corrcoef >= failCorr) >= 2 then keep image. 2a) If the corners are fairly similar but not extremely similar, check whether the center of the image is extremely dissimilar from the corners (meaning there's probably a fish taking up most of the center). Take images that passed one of two checks and copy them to a 'Pass' folder within current WD; put remaining images in a 'Fail' folder - can be accessed later to check for any missing images.
import argparse import cv2 import time # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--index", default='index.csv', help="Path to where the computed index will be stored") ap.add_argument("-l", "--limit", default=10, help="Query result limit") ap.add_argument("-d", "--dist", default='euclidean', help="Distance model") ap.add_argument("-q", "--query", required=True, help="Path to the query image") 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, int(args["limit"]), args["dist"]) # loop over the results for (score, resultID) in results: # load the result image and display it print resultID
import cv2 # construct an argument parser 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 of file where the computed index will be stored") # vars() return a dict, it can be retrieved as args[dataset]/args[index] args = vars(ap.parse_args()) # in HSV, 8 bins for hue, 12 bins for saturation, 3 for values(brightness) cd = ColorDescriptor((8, 12, 3)) # open output index file for writing output = open(args["index"], "w") # use glob to grab the image files in path and iterate them for imagePath in glob.glob(args["dataset"] + "/*.jpg"): # extract the image ID(i.e. the unique filename) imageID = imagePath[imagePath.rfind('/') + 1 :] image = cv2.imread(imagePath) features = cd.describe(image) # write the features to file features = [str(f) for f in features] output.write("%s,%s\n" % (imageID, ",".join(features))) output.close()
import glob import cv2 # Parse arguments argParser = argparse.ArgumentParser() argParser.add_argument("-d", "--dataset", required=True, help="Path to directory that contains the images to be indexed") argParser.add_argument("-i", "--index", required=True, help="Path to where teh computed idnex will be stored") args = vars(argParser.parse_args()) # 8 hue bins, 12 saturation bins, 3 value bins colorDescriptor = ColorDescriptor((8, 12, 3)) # Open the indexFile in which we will save the indexed images indexFile = open(args["index"], "w") # Loop over image files with glob for imagePath in glob.glob(args["dataset"] + "/*.jpg"): imageID = imagePath[imagePath.rfind("\\") + 1:] image = cv2.imread(imagePath) # get the features features = colorDescriptor.describe(image) # write feature to file features = [str(f) for f in features] indexFile.write("%s,%s\n" % (imageID, ",".join(features))) indexFile.close()
ap.add_argument("-v", "--index2", required=True, help="Path to where the vd 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()) cd = ColorDescriptor((8, 12, 3)) vd = Vgg16Descriptor((224, 224, 3)) query = cv2.imread(args['query']) feature1 = cd.describe(query) feature2 = vd.describe(args['query']) searcher = Searcher(args["index1"], args["index2"]) results = searcher.search(feature1, feature2) result0 = cv2.resize(query, (128, 128), interpolation=cv2.INTER_CUBIC) name = locals() i = 1 for (score, resultID) in results: name['result%d' % i] = cv2.imread(args["result_path"] + "/" + resultID) name['result%d' % i] = cv2.resize(name['result%d' % i], (128, 128), interpolation=cv2.INTER_CUBIC) i = i + 1
os.makedirs(folderPath + '/Fail') # initialize the color descriptor cd = ColorDescriptor((8, 12, 3)) # HSV binning -- 8 hue, 12 saturation, 3 value features = [] ctrcorr = [] images = glob.glob(folderPath+'/*.jpg') for pic in images: image = cv2.imread(pic) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ctr = [] temp = cd.describe(image) features.append(temp) for i in temp[0:4]: ctr.append(np.corrcoef(i, temp[4])[1,0]) ctrcorr.append(ctr) names = [t[t.rfind("/")+1:] for t in images] zippy = zip(features, names, ctrcorr) """ Perform 3 kinds of checks: 1) How many peaks are there within a certain range of the first peak? Uniform background will have relatively few high peaks while variable background will have many lower peaks and no strong signals. Measure by counting the number of peaks within some threshold below the highest peak [peak-thresh:peak]. 1a) How dissimilar are the corners from the center of the image? If the corners are extremely uniform, this is a fairly lenient check (correlation between corners and center < 0.998, mostly just gets rid of tiled images), since often a fish will be partially transparent or similar in color to the background. 2) Of those without a single strong peak, are the four corners extremely similar? Measure using correlation coefficients between four corners. If at least two are above the threshold then we have at least two pairs of corners that are extremely similar (usually top two and bottom two). If # (corrcoef >= failCorr) >= 2 then keep image.
# 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 Shape descriptor cd = ColorDescriptor(21) # 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"] + "/*.png"): # 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() # 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()
# -i = path to the csv index containing all feature vectors from colordescriptor import ColorDescriptor from searcher import Searcher from imutils import build_montages import argparse import cv2 ap = argparse.ArgumentParser() ap.add_argument("-i", "--index", type=str, default="index.csv") ap.add_argument("-u", "--upload", required=True) args = vars(ap.parse_args()) cd = ColorDescriptor((8, 12, 3)) upload = cv2.imread(args["upload"]) features = cd.describe(upload) searcher = Searcher(args["index"]) results = searcher.search(features) i = 0 locs = [] similars = [] for (score, resultID) in results: result = cv2.imread(resultID) if (i < 1): if score < 1.0: title = "Found: " + resultID else: title = "Not Found - Best Match: " + resultID im = cv2.resize(result, (600, 400)) cv2.imshow(title, im)