コード例 #1
0
ファイル: evaluate.py プロジェクト: kyushu/Computer_Vision
# distance metric, and inverted document frequency array
detector = cv2.FeatureDetector_create("SURF")
descriptor = 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 = cPickle.loads(open(args["idf"]).read())
    distanceMetric = distance.cosine

# load the codebook vocabulary and initialize the bag-of-visual-words transformer
vocab = cPickle.loads(open(args["codebook"]).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)


# load the relevant queries dictionary
relevant = json.loads(open(args["relevant"]).read())
queryIDs = relevant.keys()

# initialize the accuracies list and the timings list
accuracies = []
timings = []
コード例 #2
0
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())

#
# 1. 初始化 Bag-Of-Visual-Word
#
# 將 codebook vocabulary 從 cPickle file 裡取出
vocab = cPickle.loads(open(args["codebook"]).read())
# 並以 codebook vocabulary 初始化 bag-of-visual-words
# bovw 用來將 target image 的 feature vectors 與 vocab 裡的 feature vectors 做 clustering
bovw = BagOfVisualWords(vocab)

#
# 2. 被始化 Bag-Of-Visual-Word Indexer
#
# 從 HDF5 取出 features database 並初始化 bag-of-visual-words indexer
# features_db 包含了 3 個 dataset: "image_ids", "index", "features"
featuresDB = h5py.File(args["features_db"], mode="r")
# bovw.codebook.shape[0]    : histogram 的個數 = 使用 k-means 所產生出來的 cluster center 的個數
# args["bovw_db"]           : 輸出 bovw_db HDF5 database 的路徑
# estNumImages              : 預估 database 裡 image 的數量
# maxBufferSize             : The maximum number of BOVW histograms to be stored in memory prior to
#                               writing them to disk
bi = BOVWIndexer(bovw.codebook.shape[0],
                 args["bovw_db"],
                 estNumImages=featuresDB["image_ids"].shape[0],
コード例 #3
0
    "-s",
    "--max-buffer-size",
    type=int,
    default=100,
    help="Maximum buffer size for # of features to be stored in memory")
ap.add_argument("-l",
                "--levels",
                type=int,
                default=2,
                help="# of pyramid levels to generate")
args = vars(ap.parse_args())

# load the codebook vocabulary
vocab = cPickle.loads(open(args["codebook"]).read())
# initialize bovw with vocab
bovw = BagOfVisualWords(vocab)
# initialize pbow with bovw and numLevels parameter
pbow = PBOW(bovw, numLevels=args["levels"])

featureDim = PBOW.featureDim(bovw.codebook.shape[0], args["levels"])
featuresDB = h5py.File(args["features_db"], mode="r")
bi = BOVWIndexer(featureDim,
                 args["pbow_db"],
                 estNumImages=featuresDB["image_ids"].shape[0],
                 maxBufferSize=args["max_buffer_size"])

# loop over the image IDs
for (i, imageID) in enumerate(featuresDB["image_ids"]):
    # grab the image dimensions, along with the index lookup values from the
    # database
    (h, w) = featuresDB["image_dims"][i]
コード例 #4
0
print("[INFO] features:\n{}\n".format(features))
'''
vocab: 假設已存在一個 bag of visual word 包含 3 個 vocabulary for image (也就是 feature vector)
features: 從某張圖片取出的 feature vectors

將每個 feature 跟 vacab 裡的 3 個 vocabulary feature vector 做 distance 
並記錄每個 feature 與 vocab 裡的那個 vocabluary featue 最接近
'''
# sklearn.metrics 的 pairwise 可以
# 1. vector 對 array
# 2. array 對 array
# 這裡示範使用 for loop 去算出 feature vector 跟 feature vector 兩兩之間的距離
# hist = np.zeros((vocab.shape[0],), dtype="int32")
# for (i, f) in enumerate(features):
#     # 取圖片畫每個 feature vector 跟 vocab 裡的 feature vector 兩兩之間的距離
#     D = pairwise.euclidean_distances(f.reshape(1, -1), Y=vocab)

#     # 取得 最小 Distance 的 index
#     j = np.argmin(D)

#     print("[INFO] Closest visual word to feature #{}: {}".format(i, j))
#     # 在 hist 對應的 index 的值加 1 (量化這張圖片裡每個 feature )
#     hist[j] += 1
#     print("[INFO] Updated histogram: {}".format(hist))

# BagOfVisualWords 使用 sklearn.metrics 的 pairwise 對 target feature array 跟 Vocab 裡的
# feature array 算兩兩之間的距離,比使用 for loop 更快
bovw = BagOfVisualWords(vocab, sparse=False)
hist = bovw.describe(features)
print("[INFO] BOVW histogram: {}".format(hist))
コード例 #5
0
ap.add_argument("-r",
                "--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())

# 1. initialize detector and descriptor
detector = cv2.FeatureDetector_create("SURF")
descriptor = RootSIFT()
dad = DetectAndDescribe(detector, descriptor)

# 2. initialize bovw and idf
idf = cPickle.loads(open(args["idf"]).read())
vocab = cPickle.loads(open(args["codebook"]).read())
bovw = BagOfVisualWords(vocab)

# 3. Load the relevant queries dictionary
relevant = json.loads(open(args["relevant"]).read())
queryFilename = args["query"][args["query"].rfind("/") + 1:]
queryRelevant = relevant[queryFilename]

# 4. Load the query image, resize it and convert its color space to grayscale
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)

# 5-1. extract features of query image
(queryKps, queryDescs) = dad.describe(queryImage)
# 5-2. construct a bag-of-visual-words of extracted features