本项目是为了实现数字识别(ocr),包括训练模型代码和识别代码
训练模型:
import sys
import numpy as np
import cv2
im = cv2.imread('test6.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_LIST,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>13 and h < 20) or (h>2 and h < 7 and w > 4):#筛选不需要的区域
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)
图片识别代码:
import cv2
import numpy as np
####### 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()
model = cv2.ml.KNearest_create()
# model.train(samples,responses)
model.train(samples, cv2.ml.ROW_SAMPLE, responses)
############################# testing part #########################
im = cv2.imread('test7.png')
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_LIST,cv2.CHAIN_APPROX_SIMPLE)
list = []
for cnt in contours:
if cv2.contourArea(cnt)>5:
[x,y,w,h] = cv2.boundingRect(cnt)
print([x,y,w,h])
if (h>13 and h < 16) or (h>3 and h < 7 and w > 3 ):
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 = model.findNearest(roismall, k = 1)
string = str(int((results[0][0])))
print(type(string))
print(chr(int(string)))
list.append([x,chr(int(string))])
cv2.putText(out,chr(int(string)),(x,y+h),0,1,(0,255,0))
num = np.asarray(list)
data = num[num[:,0].argsort()] #通过x轴排序
# data = data[:,data[2].argsort()]
print(data)
data = data[:,1]
list = data.tolist()
string = ''.join(list)
print(string) #识别后的字符
cv2.imshow('im',im)
cv2.imshow('out',out)
cv2.waitKey(0)
网友评论