Repository navigation
Expand file tree
/
Copy pathtrain_test.py
More file actions
288 lines (191 loc) · 6.71 KB
/
Copy pathtrain_test.py
File metadata and controls
288 lines (191 loc) · 6.71 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python2.7
# coding: utf-8
import cv2
import numpy as np
import sys
import os
import random
import math
import time
from config_5_4 import root, bin_n, feature_size, width_percent, height_percent, shape0, size0, tv_names, train_percent
from hog_5_4 import cut, hog, resize_cut_hog_add_shape
#import use_classifier_5_3 as c1
#import config_5_3 as config
reload(sys)
sys.setdefaultencoding('utf8')
def readDir(filePath):
fileNames = []
if os.path.isdir(filePath):
for f in os.listdir(filePath):
newFilePath = os.path.join(filePath, f)
if os.path.isdir(newFilePath):
fileNames.extend(readDir(newFilePath))
elif os.path.splitext(f)[-1] == '.JPG' or os.path.splitext(f)[-1] == '.jpg':
fileNames.append(newFilePath)
return fileNames
else:
return filePath
def readDirWithTargetShape(filePath, targetShape):
fileNames = []
if os.path.isdir(filePath):
for f in os.listdir(filePath):
newFilePath = os.path.join(filePath, f)
if os.path.isdir(newFilePath):
fileNames.extend(readDir(newFilePath))
elif os.path.splitext(f)[-1] == '.JPG' or os.path.splitext(f)[-1] == '.jpg':
img = cv2.imread(newFilePath,0)
if img.shape == targetShape:
fileNames.append(newFilePath)
return fileNames
else:
return filePath
def show_cut_img(img_name):
img = cv2.imread(img_name, 0)
cut_img = cut(img)
cv2.imshow('cut image', cut_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return cut_img
# 得到所有包含图标的文件夹地址,台标的id为在logoDirs中的位置
def getLogoDirs(dirPath):
#noLogoDir = dirPath + 'Notv'
logoDirs = []
for f in os.listdir(dirPath):
#if f != 'Notv':
newPath = os.path.join(dirPath, f)
if os.path.isdir(newPath):
logoDirs.append(newPath)
#logoDirs.append(noLogoDir)
return logoDirs
def getTvNames(logoDirs):
tvNames = []
for f in logoDirs:
name = os.path.basename(f)
tvNames.append(name)
return tvNames
def classify_logo(imagePath):
predicted_label = 0
img = cv2.imread(imagePath, 0)
hist = np.float32(resize_cut_hog_add_shape(img))
predicted_label = int(svm.predict(hist))
#print 'predicted tv: ', predicted_tvName
return predicted_label
# get all Images and all tvnames
logoDirs = getLogoDirs(root)
#tvNames = getTvNames(logoDirs)
imagePathsOfTvs = []
All_tv_names=[]
for logoDir in logoDirs:
tv_Dir = getLogoDirs(logoDir)
tvNames = getTvNames(tv_Dir)
for dir in tv_Dir:
imagePaths = readDir(dir)
print len(imagePaths)
imagePathsOfTvs.append(imagePaths)
All_tv_names.extend(tvNames)
print 'get train and test images'
train_images = []
train_labels = []
test_images = []
test_labels = []
# start train
for i, paths in enumerate(imagePathsOfTvs):
#paths.decode('utf-8').encode('gbk')
n = len(paths)
n_test = int(n*(1.0-train_percent))
n_train = n - n_test
#print len(paths)
#if len(paths)>10:
#random.shuffle(paths)
if n_train>1000:
n_train = 1000
if n==1:
n_test=1
train_images.extend(paths[:])
train_labels.extend([i]*n_train)
test_images.extend(paths[:])
test_labels.extend([i]*n_test)
else:
if (n_test >1000):
n_test = 1000
train_images.extend(paths[:n_train])
train_labels.extend([i]*n_train)
test_images.extend(paths[n_train:n_train+n_test])
test_labels.extend([i]*n_test)
else:
train_images.extend(paths[:n_train])
train_labels.extend([i]*n_train)
test_images.extend(paths[n_train:])
test_labels.extend([i]*n_test)
#print i, 'n_train = ', n_train, 'n_test = ', n_test
print '*********************************************************************start training***************************************************************************'
train_labels = np.array(train_labels)
test_labels = np.array(test_labels)
n_train = len(train_images)
n_test = len(test_images)
train_hists = np.float32(np.zeros((n_train, feature_size)))
#test_hists = np.float32(np.zeros((n_test,64)))
for i, imagePath in enumerate(train_images):
if(i%1000==0):
print imagePath
img = cv2.imread(imagePath, 0)
#cv2.imshow('img',img)
hist = resize_cut_hog_add_shape(img)
train_hists[i,:] = hist
svm_params = dict(kernel_type=cv2.SVM_LINEAR,
svm_type=cv2.SVM_C_SVC,
C=2.67, gamma=5.383)
svm = cv2.SVM()
print 'training svm'
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
svm.train(train_hists, train_labels, params=svm_params)
print 'saving svm'
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
svm.save('svm_5_3_1.0.xml')
time.sleep(10)
#the num of predict tv_logo right
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# star test
print '*****************************************************************************start test***********************************************************************'
svm_file = './svm_5_3_1.0.xml'
svm = cv2.SVM()
svm.load(svm_file)
test_right_logo ={}
test_wrong_logo = {}
# for each tv calculate numbers
test_tv_Nums ={}
for i, name in enumerate(tv_names):
test_wrong_logo[name] = 0
test_right_logo[name] = 0
test_tv_Nums[name] = 0
# for each tv VS tv_labels
for i,name in enumerate(test_labels):
#print test_labels[i]
test_tv_Nums[tv_names[test_labels[i]]] += 1
#print len(test_labels)
#for i,name in enumerate(test_tv_Nums):
#print i,tv_names[i],test_tv_Nums[tv_names[i]]
for i,name in enumerate(test_images):
imagePath = test_images[i]
predictLabel =classify_logo(imagePath)
if(predictLabel!=test_labels[i]):
test_wrong_logo[tv_names[predictLabel]] += 1
#print i, tv_names[predictLabel], tv_names[test_labels[i]], test_images[i], '**'
else:
test_right_logo[tv_names[predictLabel]] += 1
hit_rate_sum = 0
acc_rate_sum = 0
for i, name in enumerate(tv_names):
hit_rate = 100.0*test_right_logo[tv_names[i]]/test_tv_Nums[tv_names[i]]
if((test_right_logo[tv_names[i]]+test_wrong_logo[tv_names[i]])==0):
accuracy = 100.0*test_right_logo[tv_names[i]]/1
else:
accuracy = 100.0*test_right_logo[tv_names[i]]/(test_right_logo[tv_names[i]]+test_wrong_logo[tv_names[i]])
if (tv_names[i] != 'Notv'):
hit_rate_sum += hit_rate
acc_rate_sum += accuracy
tv_names[i] = tv_names[i].decode('utf-8').encode('gbk')
print tv_names[i] , 'hit rate = ',hit_rate,'%','accuracy = ',accuracy,'%'
print 'hit_rate_sum = ', 1.0 * hit_rate_sum / (len(tv_names)-1) , '%'
print 'acc_rate_sum = ', 1.0 * acc_rate_sum / (len(tv_names)-1), '%'
#'''