赞
踩
数字识别是计算视觉中一个重要的任务,可以用来完成老设备加装摄像头完成数据采集的工作。本文来介绍下用opencv实现数字识别训练。
import cv2
import numpy as np
class OCR():
def __init__(self):
####### training part ###############
samples = np.loadtxt('generalsamples.data',np.float32)
responses = np.loadtxt('generalresponses.data',np.float32)
responses = responses.reshape((responses.size,1))
# model = cv2.KNearest()
self.model = cv2.ml.KNearest_create()
# model.train(samples,responses)
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
def OCR_image(self,image_path):
############################# testing part #########################
im = cv2.imread(image_path)
out = np.zeros(im.shape,np.uint8)
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# print(contours)
# print(hierarchy)
list = []
for cnt in contours:
# if cv2.contourArea(cnt)>5: #面积
[x,y,w,h] = cv2.boundingRect(cnt)
# # print([x,y,w,h])
# if (h>0 ):
# print([x, y, w, h])
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
roi = thresh[y:y+h,x:x+w]
roismall = cv2.resize(roi,(10,10))
roismall = roismall.reshape((1,100))
roismall = np.float32(roismall)
retval, results, neigh_resp, dists = self.model.findNearest(roismall, k = 1)
string = str(int((results[0][0])))
# print(type(string))
# print(chr(int(string)))
list.append([int(x),chr(int(string))])
cv2.putText(out,chr(int(string)),(x,y+h),0,1,(0,255,0))
list = sorted(list) #排序
str1 = ''.join(str(i[1]) for i in list)
# print("结果:",str1)
cv2.imshow('im', im)
cv2.imshow('out', out)
cv2.waitKey(0)
return str1
image_OCR = OCR()
print(image_OCR.OCR_image('test1.png'))
# num = np.asarray(list,dtype="int64")
# # data = num[num[:,0].argsort()] #通过x排序
# data = num[np.argsort(num[:,0])]
# # data = data[:,data[2].argsort()]
# print(data)
# data = data[:,1]
# list = data.tolist()
# string = ''.join([str(i) for i in list])
# print(string) #识别后的字符
执行程序,结果如图,会自动识别不同颜色,不同大小,不同位置的数字。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。