2

I read this SOF page Simple Digit Recognition OCR in OpenCV-Python

Then I tried to run the below code by myself:

 import cv2
 import numpy as np


 samples = np.loadtxt('generalsamples.data',np.float32)
 responses = np.loadtxt('generalresponses.data',np.float32)
 responses = responses.reshape((responses.size,1))

 print "samples: \n",samples
 print "respnses: \n",responses

 model = cv2.ml.KNearest_create() 
 model.train(samples,cv2.ml.ROW_SAMPLE,responses)

Output:

samples:

[ 1.   1.   1.   1.   1.   1.   1.   1.   1.  65.   0.   0.   0.   0.   0.
  0.   0.   1.  65.   0.   0.   0.   0.   0.   0.   0.   1.  49.  16.   0.
  0.   0.   0.   0.   0.   1.   1.  65.   0.   0.   0.   0.   0.   0.   1.
  1.  65.   0.   0.   0.   0.   0.   0.   0.   1.  65.   0.   0.   0.   0.
  0.   0.   0.   1.  65.   0.   0.   0.   0.   0.   0.   0.   1.  65.   0.
  0.   0.   0.   0.   0.   0.   1.  11.  55.   0.   0.   0.   0.   0.   0.
  1.   4.  62.   0.   0.   0.   0.   0.   0.   0.]

respnses:
[[ 7.]]

Error:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    model.train(samples,cv2.ml.ROW_SAMPLE,responses)
cv2.error: OpenCV(4.0.0-pre) /home/shajal/opencv/opencv/modules/ml/src/data.cpp:259: error: (-215:Assertion failed) samples.type() == 5 || samples.type() == 4 in function 'setData'

What should I do?

I have reinstalled Opencv on my Ubuntu machine to resolve this.

Shajal Ahamed
  • 141
  • 2
  • 16

3 Answers3

2

When you use model.train(samples,cv2.ml.ROW_SAMPLE,responses) check next:

  1. dimensions of responses must be (n,1), where n length of samples (n,m).
  2. type of samples must be np.int64
Ivan Lipko
  • 126
  • 1
  • 6
  • I am still getting error..? Error: Assertion failed (samples.(322, 1) type() == 5 || samples.type() == 4) in cv::ml::TrainDataImpl::setData, file C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\ml\src\data.cpp, line 259 Traceback (most recent call last): – Prashant Dec 16 '18 at 16:29
  • Prashant Kuma, please give me a link to data. In my opencv repo have no 'generalsamples.data' and 'generalresponses.data' files. – Ivan Lipko Dec 18 '18 at 17:03
0

I got this error using OpenCV 3.4.0.12 and Python 3.6.

OpenCV Error: Assertion failed ((layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples)) in setData, file /io/opencv/modules/ml/src/data.cpp, line 298
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    model.train(samples,cv2.ml.ROW_SAMPLE,responses)
cv2.error: /io/opencv/modules/ml/src/data.cpp:298: error: (-215) (layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples) in function setData

So I think you must have an error with the dimensions as (layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples) says.

EDIT:

Yeah, the problem was the dimensions, after thinking a bit and understanding this, I finally realize what was happening, was easier to solve.

You do

responses = responses.reshape((responses.size,1))

Because responses is read as a float, not as an array. You get then [[ 7. ]] after doing the reshape. If you look at your samples array you'll see that it's a [ numbers ] array, not an [[ numbers ]] array.

Doing the same for samples makes everything work. So add just after the samples creation: samples = samples.reshape((1, samples.size))

0

This solved my problem:

After samples = np.loadtxt('generalsamples.data',np.float32)

Use samples = np.array(samples).astype('float32') to convert the numpy array type to float32

Dharman
  • 30,962
  • 25
  • 85
  • 135
KALOK
  • 1