training haar cascade with single positive sample

asked 2020-04-06 22:01:23 -0500


I try to train OpenCV to recognize a logo that is flat, so I really only need 1 positive sample.

I used the following command to create 1000 samples with different distortion and store in sample.vec opencv_createsamples -img positive_images\3.jpeg -num 1000 -vec sample.vec

However, I don't know how to use opencv_traincascade command to train it properly. I tried the following command:

opencv_traincascade -data data -vec sample.vec -bg negatives.txt -numPos 1000 -numNeg 3000 -numStages 10 -w 24 -h 24

But it returns the following error, probably because I used only one positive sample although I thought the vec file should contain 1000 samples.

OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.

So I changed -numPos value to 1

opencv_traincascade -data data -vec sample.vec -bg negatives.txt -numPos 1 -numNeg 3000 -numStages 10 -w 24 -h 24

The training was complete very quickly and ends in "TRAINING 1-stage" that returns the following message: Required leaf false alarm rate achieved. Branch training terminated.

I know this is not an error, but the output xml file is very simple, only 2kbytes. I think it only trained the model with 1 image in the vec. Can anyone help me on how to use open_traincascade in this case?


edit retag flag offensive close merge delete


I don't know anything about this, but as far as I understand, the purpose of opencv_createsamples is to create a lot of positive samples from a single image. So, you'd have lots of positive samples, not one. That would mean that your ideas of training may be false, and thus, the commands you use.

mvuori gravatar imagemvuori ( 2020-04-07 13:12:40 -0500 )edit

mvuori, In the opencv document below, it clearly states I can use a single positive sample for rigid object like a logo.

LearningCV gravatar imageLearningCV ( 2020-04-07 14:33:56 -0500 )edit

"Positive samples are created by opencv_createsamples utility. They may be created from a single image with object or from a collection of previously marked up images."

My understanding of this sentence: you can sample multiple positives image from one image. Like you can sample multiple faces from one picture with a crowd of peoples in it. That's all there is about that "single image approach". You still need multiple positive images to train! Not only one - the model will overfit - to make it short - it wont work - in best case it will only work on that training image.

If you really want to detect a fixed image(something which is always the same / no variations - like a logo) you can think about using template matching. Training a cascade is also valid but requires more data.

holger gravatar imageholger ( 2020-04-08 04:56:53 -0500 )edit

@holger, if you go to that page again and search "logo", you will find this sentence. " For example you may need only one positive sample for absolutely rigid object like an OpenCV logo, but you definitely need hundreds and even thousands of positive samples for faces."
Anyway, when I use my opencv_createsamples command, if I add the "-show" option at the end of the command I could actually see a 1000 samples with different orientation angles and shades. The resulting vec file size is in megabytes. My doubt is that these auto generated samples were not used in opencv_traincascade, because the xml file doesn't contain much informatoin. Maybe it just looked at one sample in the vec file.

LearningCV gravatar imageLearningCV ( 2020-04-08 12:13:07 -0500 )edit

I still doubt the one image approach - even when the opencv guys state otherwise.

holger gravatar imageholger ( 2020-04-08 20:24:37 -0500 )edit