-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_recognition.py
122 lines (95 loc) · 3.2 KB
/
obj_recognition.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
import numpy as np
import cv2 as cv
import os
import time
import hog
### settings of image
ROWS = 256
COLS = 256
is_intensity = False
is_knn = False
training_set_dir = "./dataset/Training_set"
test_set_dir = "./dataset/test"
classes = os.listdir (training_set_dir)
classes.remove ('.DS_Store')
print (classes)
start_time = time.time ()
print ('Start time: %s' % time.ctime (start_time))
######### Construct training data
def append_data (X, Y, img, class_name):
if (is_intensity):
append_data_intensity (X, Y, img, class_name)
else:
append_data_hog (X, Y, img, class_name)
### knn
def append_data_intensity (X, Y, img, class_name):
gray_img = cv.cvtColor (img, cv.COLOR_BGR2GRAY)
X.append (np.array (gray_img[: , :]).reshape (ROWS * COLS).astype(np.float32))
Y.append (np.array ([classes.index (class_name)]).astype (np.int32))
### svm
def append_data_hog (X, Y, img, class_name):
hogdata = hog.hog (np.float32 (img) / 255.0)
X.append (np.float32 (hogdata))
Y.append (np.array ([classes.index (class_name)]).astype (np.int32))
training_X = []
training_Y = []
test_X = []
test_Y = []
def classes_loop (dir, X, Y) :
for class_name in classes:
files = os.listdir (dir + '/' + class_name)
for file in files:
img_t = cv.imread (dir + '/' + class_name + '/' + file)
append_data (X, Y, cv.resize (img_t, (ROWS, COLS), interpolation = cv.INTER_CUBIC), class_name)
train_data = np.load ('hog_train_data.npz')
test_data = np.load ('hog_test_data.npz')
if not len (train_data.files) == 0:
training_X = train_data['training_X']
training_Y = train_data['training_Y']
train_data.close ()
else:
classes_loop (training_set_dir, training_X, training_Y)
training_X = np.array (training_X)
training_Y = np.array (training_Y)
if (is_intensity):
np.savez ('intensity_train_data.npz', training_X = training_X, training_Y = training_Y)
else:
np.savez ('hog_train_data.npz', training_X = training_X, training_Y = training_Y)
if not len (test_data.files) == 0:
test_X = test_data['test_X']
test_Y = test_data['test_Y']
else:
classes_loop (test_set_dir, test_X, test_Y)
test_X = np.array (test_X)
test_Y = np.array (test_Y)
if (is_intensity):
np.savez ('intensity_test_data.npz', test_X = test_X, test_Y = test_Y)
else:
np.savez ('hog_test_data.npz', test_X = test_X, test_Y = test_Y)
############ Processing
if (is_knn):
knn = cv.ml.KNearest_create ()
knn.train (training_X, cv.ml.ROW_SAMPLE, training_Y)
k = 16
ret,result,neighbours,dist = knn.findNearest (test_X, k)
else:
svm = cv.ml.SVM_create ()
svm.setKernel (cv.ml.SVM_POLY)
svm.setType (cv.ml.SVM_C_SVC)
degree = 2.347
coef0 = 178
C = 2.67
gamma = 150.383
svm.setDegree (degree)
svm.setCoef0 (coef0)
svm.setC (C)
svm.setGamma (gamma)
print ("Params: ", degree, coef0, C, gamma)
svm.train (training_X, cv.ml.ROW_SAMPLE, training_Y)
result = svm.predict (test_X)[1]
matches = result == test_Y
correct = np.count_nonzero (matches)
accuracy = correct*100.0 / result.size
print (accuracy)
total_time = time.time () - start_time
print ('Total time: %s' % total_time)