-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
113 lines (101 loc) · 2.94 KB
/
Copy pathmodel.py
File metadata and controls
113 lines (101 loc) · 2.94 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
__author__ = 'backing'
import numpy as np
import cv2
import os
import shutil
import number_cut as nc
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn import metrics
'''
@describe used only once, for writing number img data to file
next time we use numpy.load to get data
@return none
'''
def write_img_data():
data = np.array([])
label = np.array([])
filecnt = 0
for sub_dir in range(0,10):
dir = 'data/' + str(sub_dir)
for filename in os.listdir(dir):
img = cv2.imread(dir + '/'+ filename, 0)
img.reshape(1,-1)
data = np.append(data,img)
label = np.append(label,sub_dir)
filecnt += 1
data = data.reshape(filecnt, -1)
label = label.reshape(filecnt,)
np.save('data.npy',data)
np.save('label.npy',label)
'''
@test
'''
def test_accuracy():
data = np.load('data/data.npy')
label = np.load('data/label.npy')
data_train, data_test, label_train, label_test = train_test_split(data,label,test_size=0.9,random_state = 42)
print data_train.shape, data_test.shape, label_train.shape, label_test.shape
clf = svm.SVC(kernel = 'linear', C = 100)
#clf = KNeighborsClassifier()
clf.fit(data_train,label_train)
predict = clf.predict(data_test)
print predict
print label_test
print metrics.accuracy_score(label_test,predict)
def load_classifier():
data = np.load('data/data.npy')
label = np.load('data/label.npy')
#clf = KNeighborsClassifier()
clf = svm.SVC(probability = True, kernel = 'linear', C = 100)
clf.fit(data,label)
return clf
'''
@param clf classifier
@param img img to be classified
@return class label for sample img
'''
def pred(clf, img):
return clf.predict(img.reshape(1,-1))
'''
@param clf classifier
@param img img to be classified
@return class label for sample img and prob in range [0,...]
where prob close 0 means better similarity
'''
def pred_prob(clf, img):
res = clf.predict_log_proba(img.reshape(1,-1))
min_pro, label = 100,0
for i, pro in enumerate(res[0]):
if abs(pro)<min_pro:
min_pro = abs(pro)
label = i
return label, min_pro
'''
@param clf classifier
@param img image to be reconize
@return recognize number
'''
def recognize_number(clf, img):
imgs = nc.get_single_numbers(img)
res = 0
for number in imgs:
digit = pred(clf, number)
res = res * 10 + digit[0]
return res
'''
@test
'''
def test_pred_pos():
clf = load_classifier()
img = cv2.imread('data/train5/50114.jpg',0)
print pred_prob(clf, img)
if __name__ == '__main__':
# clf = load_classifier()
# img = cv2.imread('data/train/2.jpg',0)
# print img.shape
# print predict(clf,img)
test_pred_pos()
# test_accuracy()
# write_img_data()