Пример #1
0
# 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]")

    # extract the feature vectors for the current image and then quantize
    # the features to construct the bag-of-visual-words histogram
    features = featuresDB["features"][offset[0]:offset[1]][:, 2:]
    hist = bovw.describe(features)

    # add the bag-of-the-visual-words to the index
    bi.add(hist)

# close the features database and finish the indexing process
featuresDB.close()
bi.finish()
Пример #2
0
pbar = progressbar.ProgressBar(maxval=len(queryIDs), widgets=widgets).start()

# loop over the images
for (i, queryID) in enumerate(sorted(queryIDs)):
    # look up the relevant results for the query image
    queryRelevant = relevant[queryID]

    # load the query image and process it
    queryImage = cv2.imread(f"{args['dataset']}/{queryID}")
    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
    (kps, descs) = dad.describe(queryImage)
    hist = bovw.describe(descs).tocoo()

    # perform the search and then spatially verify
    sr = searcher.search(hist, numResults=20)
    sv = spatialVerifier.rerank(kps, descs, sr, numResults=4)

    # compute the total number of relevant images in the top-4 results
    results = set([r[1] for r in sv.results])
    inter = results.intersection(queryRelevant)

    # update the evaluation lists
    accuracies.append(len(inter))
    timings.append(sr.search_time + sv.search_time)

    pbar.update(i)
Пример #3
0
# 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)
queryHist = bovw.describe(queryDescs).tocoo()

# connect to redis and perform the search
redisDB = Redis(host="localhost", port=6379, db=0)
searcher = Searcher(redisDB,
                    args["bovw_db"],
                    args["features_db"],
                    idf=idf,
                    distanceMetric=distance.cosine)
sr = searcher.search(queryHist, numResults=20)
print("[INFO] search took: {:.2f}s".format(sr.search_time))

# spatially verified the results
spatialVerifier = SpatialVerifier(args["features_db"], idf, vocab)
sv = spatialVerifier.rerank(queryKps, queryDescs, sr, numResults=20)
print("[INFO] spatial verification took: {:.2f}s".format(sv.search_time))
Пример #4
0
descriptor = DescriptorExtractor_create("RootSIFT")
dad = DetectAndDescribe(detector, descriptor)

# load the codebook vocabulary and initialize the bovw transformer
vocab = pickle.loads(open(args["codebook"], "rb").read())
bovw = BagOfVisualWords(vocab)

# load the classifier
model = pickle.loads(open(args["model"], "rb").read())

# loop over the image paths
for imagePath in paths.list_images(args["images"]):
    # load and preprocess image
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = imutils.resize(gray, width=min(320, image.shape[1]))

    # describe the image and classify it
    (kps, descs) = dad.describe(gray)
    hist = bovw.describe(descs)
    hist /= hist.sum()
    prediction = model.predict(hist)[0]

    # show the prediction
    filename = imagePath[imagePath.rfind("/") + 1:]
    print(f"[PREDICTION] {filename}:{prediction}")
    cv2.putText(image, prediction, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0,
                (0, 255, 0), 2)
    cv2.imshow("Image", image)
    cv2.waitKey(0)