当前位置:   article > 正文

python数字识别-模型应用

python数字识别

简介

数字识别是计算视觉中一个重要的任务,可以用来完成老设备加装摄像头完成数据采集的工作。本文来介绍下用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) #识别后的字符


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

执行程序,结果如图,会自动识别不同颜色,不同大小,不同位置的数字。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/679020
推荐阅读
相关标签
  

闽ICP备14008679号