成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

opencv python 基于KNN的手寫體識(shí)別

wing324 / 1575人閱讀

摘要:我們的目標(biāo)是構(gòu)建一個(gè)可以讀取手寫數(shù)字的應(yīng)用程序?yàn)榇耍覀冃枰恍┖透綆б粋€(gè)在文件夾中,它有個(gè)手寫數(shù)字每個(gè)數(shù)字個(gè)每個(gè)數(shù)字是圖像所以首先要將圖片切割成個(gè)不同圖片每個(gè)數(shù)字變成一個(gè)單行像素前面的個(gè)數(shù)字作為訓(xùn)練數(shù)據(jù),后個(gè)作為測(cè)試數(shù)據(jù)輸出進(jìn)一步

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(在文件夾opencvsourcessamplesdata中),它有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ù).

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ù),以便下次使用.

# 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ù)字是它的不同特征.

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/42127.html

相關(guān)文章

  • opencv python 基于SVM寫體識(shí)別

    摘要:最后,與前一種情況一樣,我們首先將大數(shù)據(jù)集拆分為單個(gè)單元格,對(duì)于每個(gè)數(shù)字,保留個(gè)單元用于訓(xùn)練數(shù)據(jù),剩余的個(gè)數(shù)據(jù)保留用于測(cè)試。 OCR of Hand-written Data using SVM 在kNN中,我們直接使用像素強(qiáng)度作為特征向量。 這次我們將使用方向梯度直方圖(HOG)作為特征向量。在計(jì)算HOG之前,使用其二階矩來校正圖像: def deskew(img): m ...

    seanHai 評(píng)論0 收藏0
  • 如何使用Python Opencvann神經(jīng)網(wǎng)絡(luò)識(shí)別手寫數(shù)字功能

      寫這篇文章的主要目的,是關(guān)于Python Opencv的相關(guān)知識(shí),包括ann神經(jīng)網(wǎng)絡(luò)識(shí)別手寫數(shù)字功能,教給大家怎么去使用這種功能,接下來請(qǐng)大家仔細(xì)的進(jìn)行閱讀哦?! pencv會(huì)給大家提供一種神經(jīng)網(wǎng)絡(luò)的功能,即為ann,這種神經(jīng)的網(wǎng)絡(luò)功能與Keras的很接近?! £P(guān)于mnist數(shù)據(jù)怎么去進(jìn)行解析,讀者人員可以自己從網(wǎng)上downland軟件,用python自己編寫解析代碼,由于這里主要研究knn...

    89542767 評(píng)論0 收藏0
  • opencv+mtcnn+facenet+python+tensorflow 實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別

    摘要:實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別本文記錄了在學(xué)習(xí)深度學(xué)習(xí)過程中,使用,開發(fā)環(huán)境為,實(shí)現(xiàn)局域網(wǎng)連接手機(jī)攝像頭,對(duì)目標(biāo)人員進(jìn)行實(shí)時(shí)人臉識(shí)別,效果并非特別好,會(huì)繼續(xù)改進(jìn)這里是項(xiàng)目地址項(xiàng)目中用到的大文件地址如果各位老爺看完覺得對(duì)你有幫助的話,請(qǐng)給個(gè)小星星,當(dāng)前時(shí)間 opencv+mtcnn+facenet+python+tensorflow 實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別 Abstract:本文記錄了在學(xué)習(xí)深度學(xué)習(xí)過程中,...

    megatron 評(píng)論0 收藏0
  • opencv+mtcnn+facenet+python+tensorflow 實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別(20

    摘要:實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別更新新增測(cè)試方法直接使用特征進(jìn)行計(jì)算對(duì)比此次更新主要想法上一個(gè)版本是使用對(duì)準(zhǔn)備好的若干張照片進(jìn)行訓(xùn)練,首先準(zhǔn)確率不是很高還沒細(xì)究問題,猜測(cè)原因是自己準(zhǔn)備的圖片問題,以及實(shí)時(shí)采集實(shí)時(shí)的環(huán)境影響,但最主要的原因還是對(duì)每個(gè)目標(biāo)對(duì)象 opencv+mtcnn+facenet+python+tensorflow 實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別(2018.9.26更新) 新增測(cè)試方法直接使用em...

    jindong 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<