Ask Your Question

wrong predictions from OpenCV Sample

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

dan1 gravatar image

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

berak gravatar image

What I did

  • Downloaded the opencv repo and setup the digit recognition sample ( and
  • Modify 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)


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

  • Run and then run

Results 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 .

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


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 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

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

berak gravatar image

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

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


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 -0500 )edit

Question Tools

1 follower


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

Seen: 315 times

Last updated: Nov 08 '18