使用python svm實(shí)現(xiàn)直接可用的手寫數(shù)字識(shí)別
python svm實(shí)現(xiàn)手寫數(shù)字識(shí)別——直接可用
最近在做個(gè)圍棋識(shí)別的項(xiàng)目,需要識(shí)別下面的數(shù)字,如下圖:
我發(fā)現(xiàn)現(xiàn)在網(wǎng)上很多代碼是良莠不齊,…真是一言難盡,于是記錄一下,能夠運(yùn)行成功并識(shí)別成功的一個(gè)源碼。
1、訓(xùn)練
1.1、訓(xùn)練數(shù)據(jù)集下載——已轉(zhuǎn)化成csv文件
下載地址
1.2 、訓(xùn)練源碼
train.py
import pandas as pd from sklearn.decomposition import PCA from sklearn import svm from sklearn.externals import joblib import time if __name__ =="__main__": train_num = 5000 test_num = 7000 data = pd.read_csv('train.csv') train_data = data.values[0:train_num,1:] train_label = data.values[0:train_num,0] test_data = data.values[train_num:test_num,1:] test_label = data.values[train_num:test_num,0] t = time.time() #PCA降維 pca = PCA(n_components=0.8, whiten=True) print('start pca...') train_x = pca.fit_transform(train_data) test_x = pca.transform(test_data) print(train_x.shape) # svm訓(xùn)練 print('start svc...') svc = svm.SVC(kernel = 'rbf', C = 10) svc.fit(train_x,train_label) pre = svc.predict(test_x) #保存模型 joblib.dump(svc, 'model.m') joblib.dump(pca, 'pca.m') # 計(jì)算準(zhǔn)確率 score = svc.score(test_x, test_label) print(u'準(zhǔn)確率:%f,花費(fèi)時(shí)間:%.2fs' % (score, time.time() - t))
2、預(yù)測(cè)單張圖片
2.1、待預(yù)測(cè)圖像
2.2、預(yù)測(cè)源碼
from sklearn.externals import joblib import cv2 if __name__ =="__main__": img = cv2.imread("img_temp.jpg", 0) #test = img.reshape(1,1444) Tp_x = 10 Tp_y = 10 Tp_width = 20 Tp_height = 20 img_temp = img[Tp_y:Tp_y + Tp_height, Tp_x:Tp_x + Tp_width] # 參數(shù)含義分別是:y、y+h、x、x+w cv2.namedWindow("src", 0) cv2.imshow("src", img_temp) cv2.waitKey(1000) [height, width] = img_temp.shape print(width, height) res_img = cv2.resize(img_temp, (28, 28)) test = res_img.reshape(1, 784) #加載模型 svc = joblib.load("model.m") pca = joblib.load("pca.m") # svm print('start pca...') test_x = pca.transform(test) print(test_x.shape) pre = svc.predict(test_x) print(pre[0])
2.3、預(yù)測(cè)結(jié)果
到此這篇關(guān)于使用python svm實(shí)現(xiàn)直接可用的手寫數(shù)字識(shí)別的文章就介紹到這了,更多相關(guān)python svm 手寫數(shù)字識(shí)別內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。