Open CV Haar cascade training can't be trained - opencv

hello I'm trying to develop a haar cascade classifiers, I was able to run the basic example with detection of cars, so I tried to identify a fruit, I download some images of pears and then generate the positive file and then the vector file, until this point everything is ok, I tested the vector file with the command:
opencv_createsamples -vec vector.vec
so I tried to generate the xml cascade classifier wtih the next command and options:
opencv_traincascade -data images/cascade -vec vector.vec -bg bg.info -precalcValBufSize 2048 -precalcIdxBufSize 2048 -numPos 24 -numNeg 450 -numStages 10 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -w 24 -h 24
I get the next error:
PARAMETERS:
cascadeDirName: images/cascade
vecFileName: vector.vec
bgFileName: bg.info
numPos: 24
numNeg: 450
numStages: 10
precalcValBufSize[Mb] : 2048
precalcIdxBufSize[Mb] : 2048
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
POS count : consumed 24 : 24
Train dataset for temp stage can not be filled. Branch training terminated.
Cascade classifier can't be trained. Check the used training parameters.
I wasn't able to find the error... is there an opencv guru that can help me?

This is old but I just solved it:
You should give for -numPos and -numNeg a number slightly lower than your total positive and negative samples, around 80% of that.
E.g. if you have 24 positive images, give -numPos 18.
Also, 24 positive samples is way too low. Try to increase it to at least 200.

Related

Haar- Cascade object detection OpenCV - .xml file classifier not detecting properly

I have used the same parameters used in this tutorial.
perl bin/createsamples.pl positives.txt negatives.txt samples 1500\
"opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\
-maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40"
BUT changed the numPos parameter as it gave me an error of insufficient positive images.
ALSO had decreased the stages as it was below the desired accuracy rate(should be less than 0.0004) and mine was 0.0002.
Used these parameters.
opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 16 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 880 -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024 -precalcIdxBufSize 1024
Got my cascade.xml file, but still not detecting properly. cascade.xml file took 3 days for processing.(Image attached)
Have used Opencv3.1.0
Please help!!!
IMAGE detected by classifier(Click to see)
What is the reason for using the parameter -bgthresh 0. From what I know the standard setting is 80, there is no reason to change parameters if you don't know what they do. Read up on that.
BUT changed the numPos parameter as it gave me an error of
insufficient positive images.
Have you checked that you really have 1500 positives, verify this together with the lines in your positives.txt file.
-numPos 880 -numNeg 600
Different opinions here, I recommend amount of neg = positives*2.
but still not detecting properly
What do you mean? What do you define as "properly"? From the example you provide I can see a proper detection with a possible false positive (the rectangle in the middle). Increase minNeighbors (in your code) – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

opencv train cascade in less than 2 hours

I'm new to opencv I want to detect a fire object and I'm training a fire classifier using opencv traincascade, as I read various tutorials everyone said that training will use up days or even weeks.
I have 700 positives and 3k negatives, I read that I should not use all positive to train and ratio of 1:2 in positive negative so this is what I inputted
opencv_traincascade -data classifier -positive.vec -bg negatives.txt -numPos 500 -numNeg 1000 -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -precalcValBufSize 1024\ -precalcIdxBufSize 1024 -mode ALL PAUSE
the training only took 2 hours, Do I need to worry about it? is there something wrong with my samples?

opencv cascade haartraining bad result

I have 616 positive image profile face and 3020 image negative and I use this code to make my vector sample:
createsamples.exe -info positive/info.txt -vec vector/facevector.vec -num 616 -w 24 -h 24
size of vector 694 KB
size of image 630 * 480
and I use this for haartraining:
haartraining.exe -data cascades -vec vector/facevector.vec -bg negative/bg.txt -npos 616 -nneg 3020 -nstages 19 -nsplits 2 -nonsym -minhitrate 0.999 -maxfalsealarm 0.5 -mem 1024 -mode ALL -w 24 -h 24 PAUSE
and when it finishes I change it to cascade by:
haarconv.exe data myfacedetector.xml 24 24
and I have only 56 cascade size also my training data size all (every stage) its 1 KB or 2KB. When I test it it dosen't detect any face also the training positive image is not detected.
First thing, in haartraining.exe command -npos is not the number of poisitive images you have; it is the number little less than the number of samples (say 0.9*(number of samples)).
Second thing, I don't see how many samples you are creating using createsamples.exe command. Again, you don't specify number of positive images here; you should specify how many samples you want to create using those positive images you have. Please study the parameters again.
Hope this helps!

OpenCV - LBP Traincascade can’t pass Precalculation in Stage 0, but HAAR works fine?

I run opencv_traincascade, and I never reach the precalculation time for stage 0.
Edit: My problem seems to be specific to LBP training. I changed featureType to HAAR and the classifier below finished training in a matter of minutes
Edit: Precalculation time is in seconds, so at stage 0 you should see it reach precalculation within 10 seconds, but I've seen it as high as 40 seconds. Precalculation time may increase steadily from stage to stage, and then suddenly increase dramatically to half an hour in the later stages (and perhaps even longer than that if you are working with thousands of samples, but I haven't got that far yet)
(I will continue to update this post filling in things I've come to understand that prevented me from understanding precalculation, and perhaps find out why HAAR training would work while LBP doesn't. It would probably just require looking at the source code for initiating the stage cycle)
Problem
I run opencv_traincascade, and I never reach the precalculation time for stage 0.
I’m using an MacBook Air purchased in 2014.
Processor 1.3 GHz Intel Core i5
Memory 4 GB 1600 MHz DDR3
Graphics Intel HD Graphics 5000
Software OS X 10.9.4 (13E28)
My terminal looks something like this, and runs some 390% (really?) of my CPU and 4 threads in my Activity Monitor.
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed x : x
NEG count : acceptanceRatio y : 1
Although I’ve tried creating many classifiers, for the sake of sharing something we can all relate to, I’m going to reference a tutorial for car detection since I get the same result not matter if it’s a training of my own or not. This trainer has been published, and I’ve seen other people reference it, so I assume it works.
https://www.youtube.com/watch?v=WEzm7L5zoZE
On my desktop I have:
A pos folder with the relative file paths and information (1 0 0 100 40) in a cars.info folder;
A bg.txt file with relative paths to a neg folder containing the negative samples;
An empty data folder named “data”;
A cars.vec file of positive samples. I can view the vector file and the images are there.
There are 550 positive samples at 100x40 originally, and 48x24 after using createsamples to create the vec file
There are 500 negative samples at 100x40
Here was the createsamples command, for reference:
opencv_createsamples -info cars.info -num 550 -w 48 -h 24 -vec cars.vec
Then I run the following command:
opencv_traincascade -data data -vec cars.vec -bg bg.txt -numPos 500 -numNeg 500 -numStages 2 -w 48 -h 24 -featureType LBP
PARAMETERS:
cascadeDirName: data
vecFileName: cars.vec
bgFileName: bg.txt
numPos: 500
numNeg: 500
numStages: 2
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: LBP
sampleWidth: 48
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
This is my output that I’ve never seen reach precalculation for LBP, no matter what I’m trying to train; how I’ve tried to change the sample size, resolution of images, minHitRate, or lowering numPos.
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 500 : 500
NEG count : acceptanceRatio 500 : 1
Please ask for any information I failed to supply. I apologize.
I’ve read about trainers being stuck in infinite loops, and modification of the source code needed. However, I hope that can be avoided since this appears to work for others (at least the author).
Thank you all for all the past questions and answers that have helped me with various coding projects.
I have met such problem before. The way I solve it is to set the values of precalcValBufSize and precalcIdxBufSize to 0 and 0. It seems that precalculation can not work for LBP. However, it works well after they were set to 0. The training process for LBP is much faster than Haar.

OpenCV traincascade parameters

I try to train an LBP cascade classifier for emotion recognition i've managed to get 650 faces positives and 1002 negatives. These are my parameters:
numPos: 650
numNeg: 1002
numStages: 20
precalcValBufSize[Mb] : 1500
precalcIdxBufSize[Mb] : 1500
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 2
maxWeakCount: 100
The positives consists of 650 frontal faces laughing and the negatives consists of some faces not laughing and most of the others are random pics of animals, nature, trucks etc
I don't get it why it doesn't find anything most of the parameters have default values.
Any suggestion is really appreciated.
I have increased the number of positive faces to 735 and negative to 1700. The xml is loaded correctly but it detects random parts from the background or my face and laughing doesn't change the result.

Resources