Python digital recognition - model application

Introduction

Digital recognition is an important task in computational vision, and it can be used to complete the work of adding cameras to old equipment to complete data collection. This article introduces the use of opencv to realize digital recognition training.

the code

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) #识别后的字符


Execute the program, the result is as shown in the figure, and it will automatically recognize numbers of different colors, different sizes, and different positions.
insert image description here

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/132082521
Recommended