-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject_Detection.py
More file actions
256 lines (106 loc) · 5.21 KB
/
Object_Detection.py
File metadata and controls
256 lines (106 loc) · 5.21 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
##############
#### Your name: Alexis Vincent
##############
import numpy as np
import re
from skimage.color import convert_colorspace
from sklearn.model_selection import GridSearchCV
from sklearn import svm, metrics
from skimage import io, feature, filters, exposure, color
from skimage.feature import hog
import matplotlib.pyplot as plt
class ImageClassifier:
def __init__(self):
self.classifer = None
def imread_convert(self, f):
return io.imread(f).astype(np.uint8)
def load_data_from_folder(self, dir):
# read all images into an image collection
ic = io.ImageCollection(dir + "*.jpg", load_func=self.imread_convert)
# create one large array of image data
data = io.concatenate_images(ic)
# extract labels from image names
labels = np.array(ic.files)
for i, f in enumerate(labels):
m = re.search("_", f)
labels[i] = f[len(dir):m.start()]
return (data, labels)
def extract_image_features(self, data):
# Please do not modify the header above
# extract feature vector from image data
fd = None
for pic in data:
#grey_picture = color.rgb2gray(pic)
#gaussian_picture = filters.gaussian(pic, 1)
rescaled_picture = exposure.rescale_intensity(pic)
feature_data = hog(rescaled_picture,
orientations=11,
#pixels_per_cell=(32, 32),
pixels_per_cell=(20, 20),
cells_per_block=(6, 6),
# transform_sqrt=True,
feature_vector=True,
block_norm='L2-Hys')
# self.print_hog_pics(color.rgb2gray(gaussian_picture))
if fd is None:
fd = feature_data.reshape(1, feature_data.shape[0])
else:
fd = np.concatenate([fd, feature_data.reshape(1, feature_data.shape[0])])
# Please do not modify the return type below
return fd
def train_classifier(self, train_data, train_labels):
# Please do not modify the header above
# train model and save the trained model to self.classifier
clf = svm.SVC(C=1, gamma=0.001, kernel='linear')
self.classifer = clf.fit(train_data, train_labels)
def predict_labels(self, data):
# Please do not modify the header
# predict labels of test data using trained model in self.classifier
# the code below expects output to be stored in predicted_labels
predicted_labels = self.classifer.predict(data)
# Please do not modify the return type below
return predicted_labels
def print_hog_pics(self, image):
#orientations=8, pixels_per_cell=(16, 16) cells_per_block=(1, 1), visualise=True
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualise=True)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex='all', sharey='all')
ax1.axis('off')
ax1.imshow(image)
ax1.set_title('Input image')
ax1.set_adjustable('box-forced')
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
ax2.axis('off')
ax2.imshow(hog_image_rescaled)
ax2.set_title('Histogram of Oriented Gradients')
ax1.set_adjustable('box-forced')
plt.show()
def main():
img_clf = ImageClassifier()
# load images
(train_raw, train_labels) = img_clf.load_data_from_folder('./train/')
(test_raw, test_labels) = img_clf.load_data_from_folder('./test/')
# convert images into features
train_data = img_clf.extract_image_features(train_raw)
test_data = img_clf.extract_image_features(test_raw)
# train model and test on training data
img_clf.train_classifier(train_data, train_labels)
predicted_labels = img_clf.predict_labels(train_data)
print("\nTraining results")
print("=============================")
print("Confusion Matrix:\n", metrics.confusion_matrix(train_labels, predicted_labels))
print("Accuracy: ", metrics.accuracy_score(train_labels, predicted_labels))
print("F1 score: ", metrics.f1_score(train_labels, predicted_labels, average='micro'))
print(predicted_labels)
# test model
predicted_labels = img_clf.predict_labels(test_data)
print("\nTesting results")
print("=============================")
print("Confusion Matrix:\n", metrics.confusion_matrix(test_labels, predicted_labels))
print("Accuracy: ", metrics.accuracy_score(test_labels, predicted_labels))
print("F1 score: ", metrics.f1_score(test_labels, predicted_labels, average='micro'))
print(predicted_labels)
if __name__ == "__main__":
main()