def get(self, filename): print("filename::") print(filename) #imagePath = glob.glob("/Users/speedkevin/python/search-engine/dataset/"+filename) imagePath = glob.glob(PATH_DATASET_THEONE + filename) print("imagePath::") print(imagePath) imagePath = ''.join(imagePath) print("imagePath::") print(imagePath) k = imagePath[imagePath.rfind("/") + 1:] print("k::") print(k) image = cv2.imread(imagePath) print("image::") print(image) # initialize the index dictionary to store our our quantifed # images, with the 'key' of the dictionary being the image # filename and the 'value' our computed features index = {} # initialize our image descriptor -- a 3D RGB histogram with # 8 bins per channel desc = RGBHistogram([8, 8, 8]) # 7 bins per channel # desc = RGBHistogram([1, 1, 1]) features = desc.describe(image) index[k] = features features = [str(f) for f in features] return jsonify(features)
def search(queryImage, indexPath, mask=None): desc = RGBHistogram([8, 8, 8]) queryFeatures = desc.describe(queryImage) index = cPickle.loads(open(indexPath).read()) searcher = Searcher(index) results = searcher.search(queryFeatures) return results[0][0][1]
def main(): parser = argparse.ArgumentParser() parser.add_argument("-m", "--models", required=True, help="Path to the directory that contains models") parser.add_argument("-d", "--dataset", required=True, help="Path to the directory that contains images") parser.add_argument("-b", "--bin_size", required=False, default=8, help="Histogram columns number") parser.add_argument("-img", "--image", required=False, default="Models/ryu_idle.png", help="Image for which we're looking for similar ones") args = parser.parse_args() bin_size = args.bin_size model_image = cv.imread(args.image) images = load_images(args.dataset) desc = RGBHistogram([bin_size, bin_size, bin_size]) image_descriptions = [] model_description = (model_image, desc.describe(model_image)) for i in images: image_descriptions.append((i, desc.describe(i))) results = [] for im_desc in image_descriptions: d = chi2_distance(model_description[1], im_desc[1]) results.append(d) sorted_images = [x[0] for _, x in sorted(zip(results, image_descriptions))] display_best_finds( model_description[0], sorted_images[:MAX_BEST_FITS_DISPLAY if len(image_descriptions) > MAX_BEST_FITS_DISPLAY else len(image_descriptions)])
def montage(queryImage, indexPath, datasetPath, mask=None): desc = RGBHistogram([8, 8, 8]) queryFeatures = desc.describe(queryImage) index = cPickle.loads(open(indexPath).read()) searcher = Searcher(index) results = searcher.search(queryFeatures) rows = 200 cols = 300 montage = np.zeros((rows * 3, cols, 3), dtype="uint8") for j in range(0, 3): (score, imageName) = results[j][0] print("PATH {}: {} {}").format(j, datasetPath, imageName) path = datasetPath + "%s" % (imageName) result = cv2.imread(path) result = cv2.resize(result, (cols, rows)) print "\t%d. %s : %.3f" % (j + 1, imageName, score) montage[j * rows:(j + 1) * rows, :] = result cv2.imshow("Results", montage) cv2.waitKey(0)
import cv2 imagePaths = sorted(glob.glob("images/*.png")) maskPaths = sorted(glob.glob("masks/*.png")) data = [] target = [] desc = RGBHistogram([8, 8, 8]) for (imagePath, maskPath) in zip(imagePaths, maskPaths): image = cv2.imread(imagePath) mask = cv2.imread(maskPath) mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) features = desc.describe(image, mask) data.append(features) target.append(imagePath.split("_")[-2]) targetNames = np.unique(target) le = LabelEncoder() target = le.fit_transform(target) (trainData, testData, trainTarget, testTaeget) = train_test_split(data, target, test_size=0.3, random_state=42) model = RandomForestClassifier(n_estimators=25, random_state=84) model.fit(trainData, trainTarget) print(
maskPaths = sorted(glob.glob(args["masks"] + "/*.png")) data = [] target = [] # yields 512 dimensional feature vector used to # characterize the color of the flower desc = RGBHistogram([8, 8, 8]) for (imagePath, maskPath) in zip(imagePaths, maskPaths): image = cv2.imread(imagePath) mask = cv2.imread(maskPath) # convert to grayscale mask = cv2.cv2Color(mask, cv2.COLOR_BGR2GRAY) features = desc.describe(image, mask) data.append(features) target.append(imagePath.split("_")[-2]) # encode labels # unique finds unique species names, which are fed to labelencoder targetNames = np.unique(target) le = LabelEncoder() # fits unique species names into integers, a category for each species # then transforms the strings into their corresponding integer classes # target now contains list of integers, one for each data point # where integer maps to flower species name target = le.fit_transform(target) (trainData, testData, trainTarget, testTarget) = train_test_split(data, target, test_size=0.3, random_state=42)
ap = argparse.ArgumentParser() ap.add_argument('-d', '--dataset', required=True, help = 'Path to the directory which contains the images') ap.add_argument('-i', '--index', required=True, help = 'Path to where the computed index will be stored') args = vars(ap.parse_args()) # index dictionary key - image filename, value - computed features index = {} # initialize 3D histogram image descriptor # 8 bits per channel desc = RGBHistogram([8, 8, 8]) # loop over all pngs in dataset directory for path in glob.glob(args['dataset'] + '/*.jpg'): # extract filename k = path[path.rfind('/') + 1:] # load image, describe it with RGB histogram descriptor # and update index image = cv2.imread(path) features = desc.describe(image) index[k] = features # index image f = open(args['index'], 'w') f.write(cPickle.dumps(index)) f.close()
"--index", required=True, help="Path to where we stored our index") ap.add_argument("-q", "--query", required=True, help="Path to query image") args = vars(ap.parse_args()) # load the query image and show it queryImage = cv2.imread(args["query"]) cv2.imshow("Query", queryImage) print("query: {}".format(args["query"])) # describe the query in the same way that we did in # index.py -- a 3D RGB histogram with # 8 bins per channel desc = RGBHistogram([8, 8, 8]) queryFeatures = desc.describe(queryImage) # load the index perform the search index = pickle.loads(open(args["index"], "rb").read()) searcher = Searcher(index) results = searcher.search(queryFeatures) # initialize the two montages to display our results -- # we have a total of 25 image in the index, but let's only # display the top 10 results; % image per montage, with # images thate are 400x166 pixels montageA = np.zeros((166 * 5, 400, 3), dtype="uint8") montageB = np.zeros((166 * 5, 400, 3), dtype="uint8") # loop over the top ten results for j in range(0, 10):
import pickle import cv2 import os 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()) index = {} desc = RGBHistogram([16, 16, 16]) for imagePath in os.listdir(args["dataset"]): # load the image, describe it using our RGB histogram # descriptor, and update the index image = cv2.imread(os.path.join(args["dataset"], imagePath)) image = cv2.resize(image, (166, 400)) features = desc.describe(image) index[imagePath] = features f = open(args["index"], "wb") f.write(pickle.dumps(index)) f.close() # show how many images we indexed print("[INFO] done...indexed {} images".format(len(index)))
# These masks isolate the flower from the background and other useless details in the picture. mask_paths = sorted(glob.glob(arguments['masks'] + '/*.png')) data = [] # Will hold the RGB feature vectors of each image. target = [] # Will hold the label corresponding to each image. descriptor = RGBHistogram([8, 8, 8]) for image_path, mask_path in zip(image_paths, mask_paths): image = cv2.imread(image_path) mask = cv2.imread(mask_path) mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) features = descriptor.describe(image, mask) data.append(features) target.append(image_path.split('_')[-2]) # The label is in the file path. # Encode the labels, train the model and report back the classification report. label_encoder = LabelEncoder() target = label_encoder.fit_transform(target) X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=.3, random_state=42) model = RandomForestClassifier(n_estimators=100, random_state=84) model.fit(X_train, y_train)
imagePaths = sorted(glob.glob(args["images"] + "/*.jpg")) maskPaths = sorted(glob.glob(args["masks"] + "/*.png")) data = [] target = [] desc = RGBHistogram([8, 8, 8]) labels = FlowerDictionary() for (imagePath, maskPath) in zip(imagePaths, maskPaths): image = cv2.imread(imagePath) mask = cv2.imread(maskPath) mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) features = desc.describe(image, mask, imagePath) # if cv2.waitKey(100) & 0xFF == ord('q'): # break # cv2.waitKey(0) data.append(features) target.append(labels.getlabel(imagePath)) targetNames = np.unique(target) le = LabelEncoder() target = le.fit_transform(target) (trainData, testData, trainTarget, testTarget) = train_test_split(data, target, test_size=0.3, random_state=42) model = RandomForestClassifier(n_estimators=25, random_state=84) fit = model.fit(trainData, trainTarget)
ap.add_argument('-d', '--dataset', required = True, help = 'Path to the directory that contains the images we just indexed') ap.add_argument('-i', '--index', required = True, help = 'Path to where we stored our index') ap.add_argument('-q', '--query', required = True, help = 'Path to query image') args = vars(ap.parse_args()) # load the query image and show it queryImage = cv2.imread(args['query']) cv2.imshow('Query', queryImage) print('query: %s' % (args['query'])) # describe the query desc = RGBHistogram([8, 8, 8]) queryFeatures = desc.describe(queryImage) # load the index and initialise our searcher index = cPickle.loads(open(args['index']).read()) searcher = Searcher(index) results = searcher.search(queryFeatures) # initialise the a montage to display the results # displaying the top 5 results (images are 460x636 pixels) montage = np.zeros((460 * 5, 636, 3), dtype = 'uint8') for j in xrange(0, 5): # grab results and load the result image (score, imageName) = results[j] path = args['dataset'] + '/%s' % (imageName) result = cv2.imread(path)