"--relevant", required=True, help="Path to relevant dictionary") ap.add_argument("-q", "--query", required=True, help="Path to the query image") args = vars(ap.parse_args()) # initialize the keypoint detector and local invatiant descriptor detector = FeatureDetector_create("SURF") descriptor = DescriptorExtractor_create("RootSIFT") dad = DetectAndDescribe(detector, descriptor) # load the inverted document frequency array and codebook vocabulary, # then initialize the bag-of-visual-words transformer idf = pickle.loads(open(args["idf"], "rb").read()) vocab = pickle.loads(open(args["codebook"], "rb").read()) bovw = BagOfVisualWords(vocab) # load the relevant queries dictionary and lookup the relevant results for the # query image relevant = json.loads(open(args["relevant"]).read()) queryFilename = args["query"][args["query"].rfind("/") + 1:] queryRelevant = relevant[queryFilename] # load the query image and process it queryImage = cv2.imread(args["query"]) cv2.imshow("Query", imutils.resize(queryImage, width=320)) queryImage = imutils.resize(queryImage, width=320) queryImage = cv2.cvtColor(queryImage, cv2.COLOR_BGR2GRAY) # extract features from the query image and construct a bag-of-visual-words from it (queryKps, queryDescs) = dad.describe(queryImage)
ap.add_argument( "-b", "--bovw-db", required=True, help="Path to where the bag-of-visual-words database will be stored") ap.add_argument( "-s", "--max-buffer-size", type=int, default=500, help="Maximum buffer size for # of features to be stored in memory") args = vars(ap.parse_args()) # load the codebook vocabulary and initialize the bag-of-visual-words transformer vocab = pickle.loads(open(args["codebook"], "rb").read()) bovw = BagOfVisualWords(vocab) # open the features database and initialize the bag-of-visual-words indexer featuresDB = h5py.File(args["features_db"], mode="r") bi = BOVWIndexer(bovw.codebook.shape[0], args["bovw_db"], estNumImages=featuresDB["image_ids"].shape[0], maxBufferSize=args["max_buffer_size"]) # loop over the image IDs and index for i, (imageID, offset) in enumerate(zip(featuresDB["image_ids"], featuresDB["index"])): # check to see if progress should be displayed if i > 0 and i % 10 == 0: bi._debug(f"processed {i} images", msgType="[PROGRESS]")
# distance metric and inverted document frequency array detector = FeatureDetector_create("SURF") descriptor = DescriptorExtractor_create("RootSIFT") dad = DetectAndDescribe(detector, descriptor) distanceMetric = dists.chi2_distance idf = None # if the path to the inverted document frequency array was supplied, then load the # idf array and update the distance metric if args["idf"] is not None: idf = pickle.loads(open(args["idf"], "rb").read()) distanceMetric = distance.cosine # load the codebook vocabulary and initialize the bag-of-visual-words transformer vocab = pickle.loads(open(args["codebook"], "rb").read()) bovw = BagOfVisualWords(vocab) # connect to redis and initialize the searcher redisDB = Redis(host="localhost", port=6379, db=0) searcher = Searcher(redisDB, args["bovw_db"], args["features_db"], idf=idf, distanceMetric=distanceMetric) spatialVerifier = SpatialVerifier(args["features_db"], idf, vocab) # load the relevant queries dictionary relevant = json.loads(open(args["relevant"]).read()) queryIDs = relevant.keys() # initialize the accuracies list and timing list accuracies = [] timings = [] # initialize the progressbar