python数字识别-训练模型
简介
数字识别是计算视觉中一个重要的任务,可以用来完成老设备加装摄像头完成数据采集的工作。本文来介绍下用opencv实现数字识别训练。
代码
import sys
import numpy as np
import cv2
im = cv2.imread('train.png')
im3 = im.copy()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
################# Now finding Contours ###################
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
samples = np.empty((0,100))
responses = []
keys = [i for i in range(44,58)]
for cnt in contours:
# if cv2.contourArea(cnt)>5: #大于像素点的区域
[x,y,w,h] = cv2.boundingRect(cnt)
# print([x,y,w,h])#对应的区域的坐标
# if (h>25 and h < 29):#筛选不需要的区域
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
roi = thresh[y:y+h,x:x+w]
roismall = cv2.resize(roi,(10,10))
cv2.imshow('norm',im)
key = cv2.waitKey(0)
print(key)
if key == 27: # (escape to quit)
sys.exit()
elif key in keys:
key = str(key)
print(key)
responses.append(int(key))#保存ascii码
sample = roismall.reshape((1,100))
samples = np.append(samples,sample,0)
responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print ("training complete")
print(samples)
print(responses)
np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)
执行代码,在训练的图片上会圈出电脑识别到的数字,然后在键盘中认为输入对应数字给电脑训练,直到识别结束,会输出两个文件generalsamples.data,generalresponses.data,到这里就完成了训练的工作。下一篇介绍使用的代码