Search code examples
python-3.xopencvobject-detectionimage-recognitionhaar-classifier

How to run opencv_traincascade?


I am having some trouble with opencv_traincascade this may trace back to earlier method calls, however I am not 100% sure.

opencv_traincascade -data classifier -vec samples.vec -bg 
negatives.txt -numStages 20 -minHitRate - 0.999 -maxFalseAlarmRate 0.5 
-numPos 4000 -numNeg 200000 -w 17 -h 23 -mode ALL 
-precalcValBufSize 1024 -precalcIdxBufSize 1024

error:

terminate called after throwing an instance of 'std::logic_error'
what():  basic_string::_M_construct null not valid
Aborted (core dumped)

I am following along with this tutorial. However, I have 5000 positive images and 200000 negative images to work from. So, Instead I followed this link and I am using the method to create training samples from some instead of distorting a single image.

Create Samples:

opencv_createsamples -info positives.dat -vec samples.vec -w 17 -h 23 
-num 5000

Output:

    Info file name: positives.dat
    Img file name: (NULL)
    Vec file name: samples.vec
    BG  file name: (NULL)
    Num: 5000
    BG color: 0
    BG threshold: 80
    Invert: FALSE
    Max intensity deviation: 40
    Max x angle: 1.1
    Max y angle: 1.1
    Max z angle: 0.5
    Show samples: FALSE
    Width: 17
    Height: 23
    Max Scale: -1

I have viewed the sample images via the vec file with

opencv_createsamples -vec samples.vec -w 17 -h 23

If it is needed, a partial capture of positives.dat

    positive_images/0/18693.png 1 0 0 17 23
    positive_images/0/18501.png 1 0 0 17 23
    positive_images/0/19998.png 1 0 0 17 23
    positive_images/0/18224.png 1 0 0 17 23
    positive_images/0/19527.png 1 0 0 17 23
    positive_images/0/18739.png 1 0 0 17 23
    positive_images/0/6926.png 1 0 0 17 23
    positive_images/0/19520.png 1 0 0 17 23
    positive_images/0/7982.png 1 0 0 17 23
    positive_images/0/17458.png 1 0 0 17 23
    positive_images/0/17312.png 1 0 0 17 23
    positive_images/0/8497.png 1 0 0 17 23

I have searched a few different posts on Stack Overflow but the issues are slightly different than mine.

thread1

thread2

thread3

I moved my windows machine over to a dual boot that is running the latest lts distro of Ubuntu.


Solution

  • I solved my issue! I created my negatives.dat file incorrectly so 1 issue was the negative files where not being found. Also I re-ran using the opencv_traincascade implementation here. only adjusting the amount of samples and the width and height of the image.

    code from link

    opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
    -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 4500\
    -numNeg 600 -w 17 -h 23 -mode ALL -precalcValBufSize 1024\
    -precalcIdxBufSize 1024
    

    While typing this I have just had a message pop up that says

    Required leaf false alarm rate achieved. Branch training terminated.
    

    Time to figure this out. Oh, it's not an error it is saying it did what I asked it to. Time to test this out.