Script Error from "Simple Digit Recognition OCR in OpenCV-Python"
<p>I have update the Script but one Error i can not fix.
Here my script Version:</p>

<pre><code>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)&gt;50) and (cv2.contourArea(cnt)&lt;900):

        [x,y,w,h] = cv2.boundingRect(cnt)
        if  ((h&gt;0) and (h&lt;35)) and ((w&gt;0) and (w&lt;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)
</code></pre>

<p>When i run the code i get this Error:</p>

<blockquote>
  <p>Traceback (most recent call last):   File "file001.py", line 45, in
  
      sample = roismall.reshape((1,100)) ValueError: total size of new array must be unchanged</p>
</blockquote>

<p>The last print "print (len(roismall))" have a Value of 30.</p>

<p>regards Thomas</p>

<p>this Error was Homemade:</p>

<pre><code>sample = roismall.reshape((1,100))
</code></pre>

<p>is corresponding to this line:</p>

<pre><code>roismall = cv2.resize(roi,(30,30))
</code></pre>

<p>The 30 x 30 = 900 is the right Value or 10,10 = 100.
I change it back to:</p>

<pre><code>roismall = cv2.resize(roi,(10,10))
</code></pre>

<p>Here the complett script:</p>

<pre><code>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)&gt;10) and (cv2.contourArea(cnt)&lt;900):

        [x,y,w,h] = cv2.boundingRect(cnt)
        if  ((h&gt;15) and (h&lt;30)) and ((w&gt;8) and (w&lt;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)
</code></pre>

<p>regards Thomas</p>

python opencv ocr