【问题标题】:Script Error from "Simple Digit Recognition OCR in OpenCV-Python"来自“OpenCV-Python 中的简单数字识别 OCR”的脚本错误
【发布时间】:2017-02-07 03:08:19
【问题描述】:

我已经更新了脚本,但是我无法修复一个错误。 这是我的脚本版本:

import sys
import numpy as np
import cv2

im = cv2.imread('test001.png')
res = cv2.resize(im,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
im3 = res.copy()

gray = cv2.cvtColor(res,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(48,58)]

for cnt in contours:
    if (cv2.contourArea(cnt)>50) and (cv2.contourArea(cnt)<900):

        [x,y,w,h] = cv2.boundingRect(cnt)
        if  ((h>0) and (h<35)) and ((w>0) and (w<35)):
            cv2.rectangle(res,(x,y),(x+w,y+h),(0,0,255),1)
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(30,30))
            cv2.imshow('norm',res)
            key = cv2.waitKey(0) % 256
            print ("+")
            print (key)
            print ("+")

            if key == 27:  # (escape to quit)
                sys.exit()
            elif key in keys:
                print ("-")
                print (key)
                print ("-")
                responses.append(int(key))
                print (len(roismall))
                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")

np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)

当我运行代码时,我得到了这个错误:

Traceback(最近一次调用最后一次):文件“file001.py”,第 45 行,在 sample = roismall.reshape((1,100)) ValueError: 新数组的总大小必须不变

最后一个打印“print (len(roismall))”的值为 30。

问候托马斯

【问题讨论】:

    标签: python opencv ocr


    【解决方案1】:

    这个错误是自制的:

    sample = roismall.reshape((1,100))
    

    对应这一行:

    roismall = cv2.resize(roi,(30,30))
    

    30 x 30 = 900 是正确的值或 10,10 = 100。 我把它改回:

    roismall = cv2.resize(roi,(10,10))
    

    这里是完整的脚本:

    import sys
    
    import numpy as np
    import cv2
    
    im = cv2.imread('test001.png')
    res = cv2.resize(im,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
    im3 = res.copy()
    
    gray = cv2.cvtColor(res,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))
    sample =  np.empty((0,100))
    responses = []
    keys = [i for i in range(48,58)]
    
    for cnt in contours:
        if (cv2.contourArea(cnt)>10) and (cv2.contourArea(cnt)<900):
    
            [x,y,w,h] = cv2.boundingRect(cnt)
            if  ((h>15) and (h<30)) and ((w>8) and (w<30)):
                cv2.rectangle(res,(x,y),(x+w,y+h),(0,0,255),1)
                roi = thresh[y:y+h,x:x+w]
                roismall = cv2.resize(roi,(10,10))
                cv2.imshow('roi',roismall)
                cv2.imshow('norm',res)
                key = cv2.waitKey(0) % 256
                if key == 27:  # (escape to quit)
                    sys.exit()
                elif key in keys:
                    responses.append(int(key))
                    sample = roismall.reshape((1,100))
                    samples = np.append(samples,sample)
    
    responses = np.array(responses,np.float32)
    responses = responses.reshape((responses.size,1))
    print ("training complete")
    
    np.savetxt('generalsamples.data',samples)
    np.savetxt('generalresponses.data',responses)
    

    问候托马斯

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多