OCR of Hand-written Data using kNN
OCR of Hand-written Digits
我們的目標(biāo)是構(gòu)建一個(gè)可以讀取手寫數(shù)字的應(yīng)用程序, 為此,我們需要一些train_data和test_data. OpenCV附帶一個(gè)images digits.png(在文件夾opencv\sources\samples\data\中),它有5000個(gè)手寫數(shù)字(每個(gè)數(shù)字500個(gè),每個(gè)數(shù)字是20x20圖像).所以首先要將圖片切割成5000個(gè)不同圖片,每個(gè)數(shù)字變成一個(gè)單行400像素.前面的250個(gè)數(shù)字作為訓(xùn)練數(shù)據(jù),后250個(gè)作為測(cè)試數(shù)據(jù).
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
|
import numpy as np import cv2 import matplotlib.pyplot as plt img = cv2.imread( 'digits.png' ) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Now we split the image to 5000 cells, each 20x20 size cells = [np.hsplit(row, 100 ) for row in np.vsplit(gray, 50 )] # Make it into a Numpy array. It size will be (50,100,20,20) x = np.array(cells) # Now we prepare train_data and test_data. train = x[:,: 50 ].reshape( - 1 , 400 ).astype(np.float32) # Size = (2500,400) test = x[:, 50 : 100 ].reshape( - 1 , 400 ).astype(np.float32) # Size = (2500,400) # Create labels for train and test data k = np.arange( 10 ) train_labels = np.repeat(k, 250 )[:,np.newaxis] test_labels = train_labels.copy() # Initiate kNN, train the data, then test it with test data for k=1 knn = cv2.ml.KNearest_create() knn.train(train, cv2.ml.ROW_SAMPLE, train_labels) ret,result,neighbours,dist = knn.findNearest(test,k = 5 ) # Now we check the accuracy of classification # For that, compare the result with test_labels and check which are wrong matches = result = = test_labels correct = np.count_nonzero(matches) accuracy = correct * 100.0 / result.size print ( accuracy ) |
輸出:91.76
進(jìn)一步提高準(zhǔn)確率的方法是增加訓(xùn)練數(shù)據(jù),特別是錯(cuò)誤的數(shù)據(jù).每次訓(xùn)練時(shí)最好是保存訓(xùn)練數(shù)據(jù),以便下次使用.
1
2
3
4
5
6
7
8
|
# save the data np.savez( 'knn_data.npz' ,train = train, train_labels = train_labels) # Now load the data with np.load( 'knn_data.npz' ) as data: print ( data.files ) train = data[ 'train' ] train_labels = data[ 'train_labels' ] |
OCR of English Alphabets
在opencv / samples / data /文件夾中附帶一個(gè)數(shù)據(jù)文件letter-recognition.data.在每一行中,第一列是一個(gè)字母表,它是我們的標(biāo)簽. 接下來的16個(gè)數(shù)字是它的不同特征.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import numpy as np import cv2 import matplotlib.pyplot as plt # Load the data, converters convert the letter to a number data = np.loadtxt( 'letter-recognition.data' , dtype = 'float32' , delimiter = ',' , converters = { 0 : lambda ch: ord (ch) - ord ( 'A' )}) # split the data to two, 10000 each for train and test train, test = np.vsplit(data, 2 ) # split trainData and testData to features and responses responses, trainData = np.hsplit(train,[ 1 ]) labels, testData = np.hsplit(test,[ 1 ]) # Initiate the kNN, classify, measure accuracy. knn = cv2.ml.KNearest_create() knn.train(trainData, cv2.ml.ROW_SAMPLE, responses) ret, result, neighbours, dist = knn.findNearest(testData, k = 5 ) correct = np.count_nonzero(result = = labels) accuracy = correct * 100.0 / 10000 print ( accuracy ) |
輸出:93.06
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000015841285