博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv python 基于KNN的手写体识别
阅读量:7005 次
发布时间:2019-06-27

本文共 2510 字,大约阅读时间需要 8 分钟。

OCR of Hand-written Digits

我们的目标是构建一个可以读取手写数字的应用程序, 为此,我们需要一些train_data和test_data. OpenCV附带一个images digits.png(在文件夹opencv\sources\samples\data\中),它有5000个手写数字(每个数字500个,每个数字是20x20图像).所以首先要将图片切割成5000个不同图片,每个数字变成一个单行400像素.前面的250个数字作为训练数据,后250个作为测试数据.

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

进一步提高准确率的方法是增加训练数据,特别是错误的数据.每次训练时最好是保存训练数据,以便下次使用.

# 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 /文件夹中附带一个数据文件letter-recognition.data.在每一行中,第一列是一个字母表,它是我们的标签. 接下来的16个数字是它的不同特征.

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 )

输出:93.06

转载地址:http://rsytl.baihongyu.com/

你可能感兴趣的文章
一线:阿里云不做SaaS,那这件事会交给谁?
查看>>
使用 JavaScript 根据用户照片和姓名生成海报
查看>>
ASP.NET Core:简洁的力量
查看>>
远程桌面网关Apache Guacamole 发布1.0.0版本\n
查看>>
重磅!尤雨溪发布Vue 3.0开发路线
查看>>
拼多多回应漏洞:比薅羊毛更快的是“资损200亿”谣言的传播速度
查看>>
SRE工程师到底是做什么的?
查看>>
硅谷AI发展简史:AI和区块链都是死路一条?
查看>>
解读微软开源MMLSpark:统一的大规模机器学习生态系统
查看>>
Reinhold就Jigsaw投票一事向JCP提交公开信
查看>>
Microsoft发布了Azure Bot Service和LUIS的GA版
查看>>
第三天 函数
查看>>
string 高频使用
查看>>
Javascript中的抛物线 ~ 加入购物车小动画
查看>>
学习|学习的奥秘
查看>>
Windows 命令行下解决python utf-8中文输出的终极解决方案
查看>>
Go 性能优化技巧 10/10
查看>>
一个通过物理地址查询网卡所属厂商的Python库——mac.py
查看>>
【编码】切割单词流并逆向、大小写反转输出-牛客联合笔试编程题(一)-2016.04.08...
查看>>
Vim实战指南(五):文本替换
查看>>