Ask Your Question
1

wrong predictions from OpenCV Sample Digits.py

asked 2018-11-06 16:14:08 -0600

dan1 gravatar image

updated 2018-11-07 02:53:55 -0600

berak gravatar image

What I did

  • Downloaded the opencv repo and setup the digit recognition sample (digits.py and digits_video.py)
  • Modify digits.py to modify line 44 (this prevents "ValueError: too many values to unpack" error)

contours, heirs = cv.findContours( bin.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)

with

_, contours, heirs = cv.findContours( bin.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)

  • Run digits.py and then run digits_video.py

Results

digits_video.py has no trouble isolating and deskewing the hand written digits but the prediction allows resolves to 0 (seen in blue above each green square). The only modification I've made to the code is the one line mentioned above in digits.py .

image description

System I'm testing this on OSX Sierra 10.12.6 with python3 and opencv 3.4.3

edit retag flag offensive close merge delete

Comments

just saying, if you're using 3.4.3, you should use 3.4.3 samples & docs, too !

berak gravatar imageberak ( 2018-11-07 00:38:07 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-11-07 02:21:16 -0600

berak gravatar image

updated 2018-11-08 02:26:55 -0600

ok, you found a bug !

problem is here

instead of

digit = model.predict(sample)[0]

it must be:

digit = model.predict(sample)[1].ravel()

(longer explanation: the python wrappers treat even a single test sample as a 2d Mat, so the prediction is returned in the results array, not in the retval (as it would be in c++). while other ml classes, like RTrees or KNearest at least make an attempt to put the 1st result into the retval, the SVM simply returns 0 in this case)

edit flag offensive delete link more

Comments

Thanks!. I tested on my raspberry pi 3. But number 2 on right side look like number 3. But it can detected 2 or 3, depending on handwriting.

supra56 gravatar imagesupra56 ( 2018-11-07 07:07:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-06 16:07:33 -0600

Seen: 563 times

Last updated: Nov 08 '18