欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年08月03日 14:04:04   作者:sakurala  
這篇文章主要介紹了opencv python 基于KNN的手寫體識別的實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

OCR of Hand-written Data using kNN

OCR of Hand-written Digits

我們的目標(biāo)是構(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ù)字作為訓(xùn)練數(shù)據(jù),后250個作為測試數(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ù),特別是錯誤的數(shù)據(jù).每次訓(xùn)練時最好是保存訓(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 /文件夾中附帶一個數(shù)據(jù)文件letter-recognition.data.在每一行中,第一列是一個字母表,它是我們的標(biāo)簽. 接下來的16個數(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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • pyinstaller 3.6版本通過pip安裝失敗的解決辦法(推薦)

    pyinstaller 3.6版本通過pip安裝失敗的解決辦法(推薦)

    這篇文章主要介紹了pyinstaller 3.6版本通過pip安裝失敗的解決辦法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • 使用pip下載時提示"You?are?using?pip?version?8.1.1,?however?version?22.1?is?available."錯誤解決

    使用pip下載時提示"You?are?using?pip?version?8.1.1,?howev

    最近在使用python的pip下載庫時,出現(xiàn)了報錯,所以下面這篇文章主要給大家介紹了關(guān)于使用pip下載時提示“You?are?using?pip?version?8.1.1,?however?version?22.1?is?available.“錯誤的解決方法,需要的朋友可以參考下
    2022-08-08
  • Python+SeaTable實(shí)現(xiàn)生成條形碼圖片并寫入表格

    Python+SeaTable實(shí)現(xiàn)生成條形碼圖片并寫入表格

    不管是錄入信息時需要用掃碼器掃碼錄入,還是有別的生成條形碼的需要,這在?SeaTable?表格中用?Python?腳本就可以輕松實(shí)現(xiàn),本文就來為大家詳細(xì)講解一下
    2022-07-07
  • Python熱重載調(diào)試新利器問題解決

    Python熱重載調(diào)試新利器問題解決

    Reloading是一個Python工具庫,它讓我們可以在每次迭代之前從源代碼中重新加載(或函數(shù))而不丟失任何當(dāng)前已執(zhí)行過程,這篇文章主要介紹了Python熱重載調(diào)試新利器,需要的朋友可以參考下
    2024-06-06
  • Python類中__init__()?和self的詳細(xì)解析

    Python類中__init__()?和self的詳細(xì)解析

    self和__init__的語法學(xué)過Python的都清楚,但是靠死記硬背來迫使自己理解并不是個好辦法,下面這篇文章主要給大家介紹了關(guān)于Python類中__init__()?和self的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Python中的pydot庫實(shí)現(xiàn)復(fù)雜圖形使用教程

    Python中的pydot庫實(shí)現(xiàn)復(fù)雜圖形使用教程

    pydot是一個用于生成和操作DOT圖文件的Python庫,封裝了Graphviz的功能,適用于可視化圖結(jié)構(gòu),它可以生成依賴圖、流程圖、樹形圖等,并支持復(fù)雜的圖形樣式、網(wǎng)絡(luò)數(shù)據(jù)集成、循環(huán)圖、輸出其他格式、處理大規(guī)模圖數(shù)據(jù)的等等
    2025-01-01
  • 用python寫一個windows消息提醒小程序

    用python寫一個windows消息提醒小程序

    上班時,由于自己經(jīng)常coding到忘記時間,經(jīng)常會一坐坐很久,搞的勞資腰都不好了,所以沒事閑的寫了個久坐提醒的小程序,文中有詳細(xì)的代碼示例,講解的非常詳細(xì),感興趣的朋友可以參考下
    2023-12-12
  • python如何實(shí)現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開問題

    python如何實(shí)現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開問題

    這篇文章主要介紹了python實(shí)現(xiàn)wifi自動連接,解決電腦wifi經(jīng)常斷開的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python 常見加密操作的實(shí)現(xiàn)

    Python 常見加密操作的實(shí)現(xiàn)

    這篇文章主要介紹了Python 常見加密操作的實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • 給Python中的MySQLdb模塊添加超時功能的教程

    給Python中的MySQLdb模塊添加超時功能的教程

    這篇文章主要介紹了給Python中的MySQLdb模塊添加超時功能的教程,timeout功能在服務(wù)器的運(yùn)維當(dāng)中非常有用,需要的朋友可以參考下
    2015-05-05

最新評論