-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.py
61 lines (49 loc) · 1.69 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import glob
import cv2
import numpy as np
import dbow
np.random.seed(123)
print("Loading Images")
images_path = glob.glob("./images/*.png")
images = []
for image_path in images_path:
images.append(cv2.imread(image_path))
n_clusters = 10
depth = 2
print("Creating Vocabulary")
vocabulary = dbow.Vocabulary(images, n_clusters, depth)
orb = cv2.ORB_create()
print("Creating Bag of Binary Words from Images")
bows = []
for image in images:
kps, descs = orb.detectAndCompute(image, None)
descs = [dbow.ORB.from_cv_descriptor(desc) for desc in descs]
bows.append(vocabulary.descs_to_bow(descs))
print("Creating Database")
db = dbow.Database(vocabulary)
for image in images:
kps, descs = orb.detectAndCompute(image, None)
descs = [dbow.ORB.from_cv_descriptor(desc) for desc in descs]
db.add(descs)
print("Querying Database")
for image in images:
kps, descs = orb.detectAndCompute(image, None)
descs = [dbow.ORB.from_cv_descriptor(desc) for desc in descs]
scores = db.query(descs)
match_bow = db[np.argmax(scores)]
match_desc = db.descriptors[np.argmax(scores)]
print("Saving and Loading Vocabulary")
vocabulary.save("vocabulary.pickle")
loaded_vocabulary = vocabulary.load("vocabulary.pickle")
for image in images:
kps, descs = orb.detectAndCompute(image, None)
descs = [dbow.ORB.from_cv_descriptor(desc) for desc in descs]
loaded_vocabulary.descs_to_bow(descs)
print("Saving and Loading Database")
db.save("database.pickle")
loaded_db = db.load("database.pickle")
for image in images:
kps, descs = orb.detectAndCompute(image, None)
descs = [dbow.ORB.from_cv_descriptor(desc) for desc in descs]
scores = loaded_db.query(descs)