7

I am trying to train a Haar Cascade to detect hands. I have a vec file of size 1000. I have 40 positive images and 600 negative images. I have tried both dropping my positive images and negative images. When I run the following command I receive the following error:

opencv_traincascade -data classifier -data classifier -vec samples.vec -bg negatives.txt
-numstages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\ -numNeg 600 -w 80
-h 40 -mode ALL -precalcValBufSize 1024\ -precalcIdxBufSize 1024

PARAMETERS:
cascadeDirName: classifier
vecFileName: samples.vec
bgFileName: negatives.txt
numPos: 1000
numNeg: 1000
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.999
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC



===== TRAINING 0-stage =====
<BEGIN
OpenCV Error: Assertion failed (_img.rows * _img.cols == vecSize) in get, file /home/lie/Desktop/Install-OpenCV-master/Ubuntu/2.4/OpenCV/opencv-2.4.9/apps/traincascade/imagestorage.cpp, line 157
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/lie/Desktop/Install-OpenCV-master/Ubuntu/2.4/OpenCV/opencv-2.4.9/apps/traincascade/imagestorage.cpp:157: error: (-215) _img.rows * _img.cols == vecSize in function get

Aborted (core dumped)

I tried lowering my positive count and doing the whole process over again and still received the same error. Any suggestions?

By the way: I am following the tutorial at : http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html

Thank you

dylan7
  • 743
  • 1
  • 8
  • 21

4 Answers4

9

The error does not seem to be a result of large number of positive or negative samples. People do train very large data sets!

From the parameters described above, it can be noticed that the dimension of the positive samples that form the samples.vec is 24x24, which is denoted by the statement:

sampleWidth: 24
sampleHeight: 24

But while calling the opencv_traincascade function, you try to set the dimension as 80x40. Try changing this to -w 24 -h 24

Anoop K. Prabhu
  • 4,771
  • 1
  • 24
  • 41
  • After taking another look, I started to believe a similar thing was wrong. After you said it, I tried changing it to 24 and it worked. Thank you so much! Is there any reason traincascade wants 24 x 24 as default? just curious. – dylan7 Jan 08 '15 at 19:11
  • I am not sure whether its a default value or not. Most probably, you will be setting these values while calling "opencv_createsamples" to create the .vec file. The positive samples will be rescaled to the width and height specified with the "opencv_createsamples" call. – Anoop K. Prabhu Jan 09 '15 at 06:11
  • Thank you. And just one more question if you don't mind. I have been told that the distortion process of opencv_createsamples is not good. I see that the alternative is to not use the distortions and create a .dat file containing a box surrounding the object of interest. Is this correct? Is there a fast way to go through every positive and note down the dimensions of this box? Thank you. – dylan7 Jan 12 '15 at 01:21
0

I faced the same problem and specifying correct dimensions (w and h) did not work for me. It turned out that my output directory (in your case "classifier") was not empty. So the command was picking up where it left off the last time (with corresponding w and h) from the output directory. I cleared the output directory and then it worked.

9Lives
  • 1
-1

The assertion is quite clear: it expects that _img.rows*_img.cols == vecSize. I dont know what _img and vecSize are supposed to be, but that means that your input data are not correctly given. Just looking at your command line, you:

  1. Wrote -data classifier -data classifier twice. That should not be a problem but still.
  2. Wrote -numPos 1000\ -numNeg 600, while you talk about 40 positive and 600 negative images, so shouldn't you be using those figures instead?

You say you have a vecSize of size 1000. What is this vecSize for again?

remi
  • 3,786
  • 1
  • 17
  • 35
  • Well .vec or vector file is the output of a posibly well known script among haar tutorials called createsamples.pl. This pearl script uses the command opencv_createsamples. The script merges the positive and negatives to create 1000 vec samples. However through further inspection I believe vecSize is the size of a vector sample. What is strange is when I specified 80 x 40 vectors I get 320 x 160 pics . And this multiplier of 4 on each dimension occurs for all input dimensions. – dylan7 Jan 07 '15 at 18:04
  • Thus _img.cols x _img.rows is not the vecSize. I assume _img is a vec sample. This is the only thing that make sense. Do u know about this script or how opencv_createsamples works? In addition i believe vectors are a required input for traincascade. Thank you for the help. – dylan7 Jan 07 '15 at 18:06
-1

Lowering numPos and numNeg under the real value, works for me.