forked from kushalvyas/Bag-of-Visual-Words-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bag.py
162 lines (121 loc) · 4.63 KB
/
Bag.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import cv2
import numpy as np
from glob import glob
import argparse
from helpers import *
from matplotlib import pyplot as plt
class BOV:
def __init__(self, no_clusters):
self.no_clusters = no_clusters
self.train_path = None
self.test_path = None
self.im_helper = ImageHelpers()
self.bov_helper = BOVHelpers(no_clusters)
self.file_helper = FileHelpers()
self.images = None
self.trainImageCount = 0
self.train_labels = np.array([])
self.name_dict = {}
self.descriptor_list = []
def trainModel(self):
"""
This method contains the entire module
required for training the bag of visual words model
Use of helper functions will be extensive.
"""
# read file. prepare file lists.
self.images, self.trainImageCount = self.file_helper.getFiles(self.train_path)
# extract SIFT Features from each image
label_count = 0
for word, imlist in self.images.iteritems():
self.name_dict[str(label_count)] = word
print "Computing Features for ", word
for im in imlist:
# cv2.imshow("im", im)
# cv2.waitKey()
self.train_labels = np.append(self.train_labels, label_count)
kp, des = self.im_helper.features(im)
self.descriptor_list.append(des)
label_count += 1
# perform clustering
bov_descriptor_stack = self.bov_helper.formatND(self.descriptor_list)
self.bov_helper.cluster()
self.bov_helper.developVocabulary(n_images = self.trainImageCount, descriptor_list=self.descriptor_list)
# show vocabulary trained
# self.bov_helper.plotHist()
self.bov_helper.standardize()
self.bov_helper.train(self.train_labels)
def recognize(self,test_img, test_image_path=None):
"""
This method recognizes a single image
It can be utilized individually as well.
"""
kp, des = self.im_helper.features(test_img)
# print kp
print des.shape
# generate vocab for test image
vocab = np.array( [[ 0 for i in range(self.no_clusters)]])
# locate nearest clusters for each of
# the visual word (feature) present in the image
# test_ret =<> return of kmeans nearest clusters for N features
test_ret = self.bov_helper.kmeans_obj.predict(des)
# print test_ret
# print vocab
for each in test_ret:
vocab[0][each] += 1
print vocab
# Scale the features
vocab = self.bov_helper.scale.transform(vocab)
# predict the class of the image
lb = self.bov_helper.clf.predict(vocab)
# print "Image belongs to class : ", self.name_dict[str(int(lb[0]))]
return lb
def testModel(self):
"""
This method is to test the trained classifier
read all images from testing path
use BOVHelpers.predict() function to obtain classes of each image
"""
self.testImages, self.testImageCount = self.file_helper.getFiles(self.test_path)
predictions = []
for word, imlist in self.testImages.iteritems():
print "processing " ,word
for im in imlist:
# print imlist[0].shape, imlist[1].shape
print im.shape
cl = self.recognize(im)
print cl
predictions.append({
'image':im,
'class':cl,
'object_name':self.name_dict[str(int(cl[0]))]
})
print predictions
for each in predictions:
# cv2.imshow(each['object_name'], each['image'])
# cv2.waitKey()
# cv2.destroyWindow(each['object_name'])
#
plt.imshow(cv2.cvtColor(each['image'], cv2.COLOR_GRAY2RGB))
plt.title(each['object_name'])
plt.show()
def print_vars(self):
pass
if __name__ == '__main__':
# parse cmd args
parser = argparse.ArgumentParser(
description=" Bag of visual words example"
)
parser.add_argument('--train_path', action="store", dest="train_path", required=True)
parser.add_argument('--test_path', action="store", dest="test_path", required=True)
args = vars(parser.parse_args())
print args
bov = BOV(no_clusters=100)
# set training paths
bov.train_path = args['train_path']
# set testing paths
bov.test_path = args['test_path']
# train the model
bov.trainModel()
# test model
bov.testModel()