-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlearnRecSVM.py
More file actions
64 lines (54 loc) · 1.57 KB
/
learnRecSVM.py
File metadata and controls
64 lines (54 loc) · 1.57 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
# -*- coding:utf-8 -*-
from sklearn.svm import SVC
from sklearn import grid_search
import numpy as np
from sklearn import cross_validation as cs
from sklearn.externals import joblib
from picPreHandle import loadSplitedBinaryPixImg
import warnings
warnings.filterwarnings("ignore")
PKL = "captcha.pkl"
def load_data():
dataset = np.loadtxt('train_data.txt',delimiter=',')
return dataset
def cross_validation():
dataset = load_data()
row,col=dataset.shape
X=dataset[:,:col-1]
Y=dataset[:,-1]
clf = SVC(kernel="linear",C=1)
scores = cs.cross_val_score(clf,X,Y,cv=5)
print("Accuracy:%0.2f (+/- %0.2f)" % (scores.mean(),scores.std()*2))
def train():
dataset=load_data()
row,col = dataset.shape
X=dataset[:,:col-1]
Y=dataset[:,-1]
clf = SVC(kernel="linear",C=1)
clf.fit(X,Y)
joblib.dump(clf,PKL)
def searchBestParameter():
parameters = {"kernel":("linear","poly","rbf","sigmoid"),"C":[1,100]}
dataset=load_data()
row,col=dataset.shape
X=dataset[:,:col-1]
Y=dataset[:,-1]
svr = SVC()
clf = grid_search.GridSearchCV(svr,parameters)
clf.fit(X,Y)
print clf.best_params_
def predict(pic_name):
clf=joblib.load(PKL)
rs=loadSplitedBinaryPixImg(pic_name)#get pixel data of splited image
predictValue=[]
for data in rs:
predictValue.append(clf.predict(data)[0])
predictValue = [str(int(i)) for i in predictValue]
print "the captcha is :%s" %("".join(predictValue))
return predictValue
if __name__=="__main__":
# cross_validation()
# searchBestParameter()
# train()
filePath = 'source/22.png'
predict(filePath)