-
Notifications
You must be signed in to change notification settings - Fork 3
/
AARC.py
65 lines (52 loc) · 1.93 KB
/
AARC.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
62
63
64
65
import sys
import pickle
import cv2
import os
import scipy
import scipy.spatial
import matplotlib.pyplot as plt
import random
import numpy as np
import TrainDB as moduleTDB
import FeatureExtraction as moduleFE
args = sys.argv
if args[1]=="1":
print("\nTraining data from the Data folder.")
moduleTDB.batch_extractor('Data')
print("\nFeatures extracted from dataset and stored in feature.pck file.")
else:
img = cv2.imread(args[1])
class Matcher(object):
def __init__(self, pickled_db_path="features.pck"):
with open(pickled_db_path, 'rb') as fp:
self.data = pickle.load(fp)
self.names = []
self.matrix = []
for k, v in self.data.items():
self.names.append(k)
self.matrix.append(v)
self.matrix = np.array(self.matrix)
self.names = np.array(self.names)
def cos_cdist(self, vector):
# getting cosine distance between search image and images database
v = vector.reshape(1, -1)
return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)
def match(self, image, topn=5):
features = moduleFE.extract_features(image)
img_distances = self.cos_cdist(features)
# getting top 5 records
nearest_ids = np.argsort(img_distances)[:topn].tolist()
nearest_img_paths = self.names[nearest_ids].tolist()
return nearest_img_paths, img_distances[nearest_ids].tolist()
def show_img(path):
plt.imshow(img)
plt.show()
def run():
ma = Matcher('features.pck')
names, match = ma.match(moduleTDB.create_boundaryImage(img), topn=1)
curr = names[0]
return curr[0].upper()
def crop_image(image, x, y, width, height):
return image[y:y + height, x:x + width]
result = run()
print("\nPredicted Label: "+str(result))