This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathhelpers.py
150 lines (118 loc) · 3.78 KB
/
helpers.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
import cv2
import numpy as np
from glob import glob
from sklearn.cluster import KMeans
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from matplotlib import pyplot as plt
class ImageHelpers:
def __init__(self):
self.sift_object = cv2.xfeatures2d.SIFT_create()
def gray(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return gray
def features(self, image):
keypoints, descriptors = self.sift_object.detectAndCompute(image, None)
return [keypoints, descriptors]
class BOVHelpers:
def __init__(self, n_clusters = 20):
self.n_clusters = n_clusters
self.kmeans_obj = KMeans(n_clusters = n_clusters)
self.kmeans_ret = None
self.descriptor_vstack = None
self.mega_histogram = None
self.clf = SVC()
def cluster(self):
"""
cluster using KMeans algorithm,
"""
self.kmeans_ret = self.kmeans_obj.fit_predict(self.descriptor_vstack)
def developVocabulary(self,n_images, descriptor_list, kmeans_ret = None):
"""
Each cluster denotes a particular visual word
Every image can be represeted as a combination of multiple
visual words. The best method is to generate a sparse histogram
that contains the frequency of occurence of each visual word
Thus the vocabulary comprises of a set of histograms of encompassing
all descriptions for all images
"""
self.mega_histogram = np.array([np.zeros(self.n_clusters) for i in range(n_images)])
old_count = 0
for i in range(n_images):
l = len(descriptor_list[i])
for j in range(l):
if kmeans_ret is None:
idx = self.kmeans_ret[old_count+j]
else:
idx = kmeans_ret[old_count+j]
self.mega_histogram[i][idx] += 1
old_count += l
print "Vocabulary Histogram Generated"
def standardize(self, std=None):
"""
standardize is required to normalize the distribution
wrt sample size and features. If not normalized, the classifier may become
biased due to steep variances.
"""
if std is None:
self.scale = StandardScaler().fit(self.mega_histogram)
self.mega_histogram = self.scale.transform(self.mega_histogram)
else:
print "STD not none. External STD supplied"
self.mega_histogram = std.transform(self.mega_histogram)
def formatND(self, l):
"""
restructures list into vstack array of shape
M samples x N features for sklearn
"""
vStack = np.array(l[0])
for remaining in l[1:]:
vStack = np.vstack((vStack, remaining))
self.descriptor_vstack = vStack.copy()
return vStack
def train(self, train_labels):
"""
uses sklearn.svm.SVC classifier (SVM)
"""
print "Training SVM"
print self.clf
print "Train labels", train_labels
self.clf.fit(self.mega_histogram, train_labels)
print "Training completed"
def predict(self, iplist):
predictions = self.clf.predict(iplist)
return predictions
def plotHist(self, vocabulary = None):
print "Plotting histogram"
if vocabulary is None:
vocabulary = self.mega_histogram
x_scalar = np.arange(self.n_clusters)
y_scalar = np.array([abs(np.sum(vocabulary[:,h], dtype=np.int32)) for h in range(self.n_clusters)])
print y_scalar
plt.bar(x_scalar, y_scalar)
plt.xlabel("Visual Word Index")
plt.ylabel("Frequency")
plt.title("Complete Vocabulary Generated")
plt.xticks(x_scalar + 0.4, x_scalar)
plt.show()
class FileHelpers:
def __init__(self):
pass
def getFiles(self, path):
"""
- returns a dictionary of all files
having key => value as objectname => image path
- returns total number of files.
"""
imlist = {}
count = 0
for each in glob(path + "*"):
word = each.split("/")[-1]
print " #### Reading image category ", word, " ##### "
imlist[word] = []
for imagefile in glob(path+word+"/*"):
print "Reading file ", imagefile
im = cv2.imread(imagefile, 0)
imlist[word].append(im)
count +=1
return [imlist, count]