国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

opencv python 基于KNN的手寫體識別的實例

2020-02-15 22:40:22
字體:
供稿:網(wǎng)友

OCR of Hand-written Data using kNN

OCR of Hand-written Digits

我們的目標是構(gòu)建一個可以讀取手寫數(shù)字的應(yīng)用程序, 為此,我們需要一些train_data和test_data. OpenCV附帶一個images digits.png(在文件夾opencv/sources/samples/data/中),它有5000個手寫數(shù)字(每個數(shù)字500個,每個數(shù)字是20x20圖像).所以首先要將圖片切割成5000個不同圖片,每個數(shù)字變成一個單行400像素.前面的250個數(shù)字作為訓練數(shù)據(jù),后250個作為測試數(shù)據(jù).

import numpy as npimport cv2import matplotlib.pyplot as pltimg = cv2.imread('digits.png')gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# Now we split the image to 5000 cells, each 20x20 sizecells = [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 datak = 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=1knn = 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 wrongmatches = result==test_labelscorrect = np.count_nonzero(matches)accuracy = correct*100.0/result.sizeprint( accuracy )

輸出:91.76

進一步提高準確率的方法是增加訓練數(shù)據(jù),特別是錯誤的數(shù)據(jù).每次訓練時最好是保存訓練數(shù)據(jù),以便下次使用.

# save the datanp.savez('knn_data.npz',train=train, train_labels=train_labels)# Now load the datawith 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 /文件夾中附帶一個數(shù)據(jù)文件letter-recognition.data.在每一行中,第一列是一個字母表,它是我們的標簽. 接下來的16個數(shù)字是它的不同特征.

import numpy as npimport cv2import matplotlib.pyplot as plt# Load the data, converters convert the letter to a numberdata= 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 testtrain, test = np.vsplit(data,2)# split trainData and testData to features and responsesresponses, 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/10000print( accuracy )            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 哈巴河县| 老河口市| 莆田市| 社会| 高青县| 饶河县| 循化| 越西县| 宽城| 五原县| 达孜县| 玛多县| 饶阳县| 铁岭县| 汾西县| 宽甸| 禹城市| 平和县| 延川县| 舟曲县| 唐海县| 雅江县| 大丰市| 浦县| 句容市| 阿荣旗| 江都市| 芜湖县| 遂平县| 剑河县| 大姚县| 德清县| 济阳县| 平武县| 余庆县| 手游| 土默特左旗| 吕梁市| 星座| 青海省| 丁青县|