opencv python 基于KNN的手写体识别的实例
作者:sakurala 时间:2021-02-22 13:03:02
OCR of Hand-written Data using kNN
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 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
进一步提高准确率的方法是增加训练数据,特别是错误的数据.每次训练时最好是保存训练数据,以便下次使用.
# 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 /文件夹中附带一个数据文件letter-recognition.data.在每一行中,第一列是一个字母表,它是我们的标签. 接下来的16个数字是它的不同特征.
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
来源:https://segmentfault.com/a/1190000015841285
标签:opencv,KNN,手写体识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django卸载之后重新安装的方法
2023-07-23 18:27:58
python 爬虫基本使用——统计杭电oj题目正确率并排序
2021-11-25 17:16:35
sqlserver2005利用临时表和@@RowCount提高分页查询存储过程性能示例分享
2024-01-21 05:41:06
MySQL安全策略(MySQL安全注意事项)
2024-01-22 19:57:25
Go调度器学习之goroutine调度详解
2024-04-30 10:06:10
![](https://img.aspxhome.com/file/2023/4/130694_0s.jpg)
Python实现求最大公约数及判断素数的方法
2021-06-13 02:20:20
详解vue中$nextTick和$forceUpdate的用法
2024-06-05 09:15:44
![](https://img.aspxhome.com/file/2023/1/123061_0s.jpg)
MySQL数据库JDBC编程详解流程
2024-01-15 09:39:55
![](https://img.aspxhome.com/file/2023/9/112329_0s.png)
详谈vue中router-link和传统a链接的区别
2024-04-09 10:46:05
Python爬虫之xlml解析库(全面了解)
2023-03-30 21:16:17
Mysql中新建用户及授权的方法分享
2024-01-24 16:14:20
Python 命令行非阻塞输入的小例子
2023-12-09 19:48:29
Vue生命周期示例详解
2024-04-28 09:25:46
![](https://img.aspxhome.com/file/2023/7/133027_0s.jpg)
如何利用python 读取配置文件
2023-11-08 14:40:21
![](https://img.aspxhome.com/file/2023/4/83814_0s.png)
python使用tkinter库实现五子棋游戏
2021-07-25 20:35:17
![](https://img.aspxhome.com/file/2023/6/65026_0s.png)
Python数据分析之pandas比较操作
2021-11-23 05:14:22
![](https://img.aspxhome.com/file/2023/4/66874_0s.png)
Pycharm最全报错的原因与解决方法总结(推荐!)
2023-10-26 04:20:03
![](https://img.aspxhome.com/file/2023/4/79534_0s.jpg)
Python数据结构之双向链表详解
2023-02-01 05:10:07
![](https://img.aspxhome.com/file/2023/9/97309_0s.png)
python 获取等间隔的数组实例
2023-05-21 15:07:16
实例剖析:MySQL数据库优化详解
2008-11-22 12:19:00