Top banner

Banner

Thursday, August 18, 2011

Creating a haar cascade classifier aka haar training

In the previous posts, I used haar cascade xml files for the detection of face, eyes etc.., In this post, I am going to show you, how to create your own haar cascade classifier xml files. It took me a total of 16 hours to do it. Hope you can do it even sooner, following this post

Note : The below is only for linux opencv users. If you are a windows user, use this link

For most of the dough, that is going to come, you will need these executable linux files. Here's the link for it.

Before I start, remember two important definetions

Positive images : These images contain the object to be detected
Negative images : Absolutely except that image, anything can be present

It's better to explain, with an example. So I will tell you, a step by step procedure, to make a classifier that detects a pen. You can use the same for any object, you are going to experiment.

First of all, I took the photographs of three of my pens, along with some background, the pics looked like the one below


I took a total of 7 photographs (I didn't care to count, for which pen, I took more photographs, out of three) with my 2MP camera phone and loaded them into my computer. Now I cropped, each one of them using Image Clipper excluding the background and storing only the pen's image. Image clipper can be downloaded from the link below

http://code.google.com/p/imageclipper/

The one thing that I hated about this imageclipper is that even though it made me work fast, it requires me to install a lot of other libraries. It would have been much simpler, if I just used GIMP, to crop, since there were only 7 images and cropping would have been easier. It is upto you, to follow the way you find easier.The cropped image looks like the one below


From here on the procedure is simple. As I told you at the starting of post, after downloading the necessary linux executables, keep them in a seperate folder named "Haar_Training", where you need to carry on your experimentations. Now inside that folder, create three folders and fill them with the necessary data, as explained below

1. Positive_Images : In this folder, I kept all my seven cropped images
2. Negative_Images : In this folder, I kept some other images (99 in total), which can be any image that you have, except, they should be of like genre and should not contain any where the cropped part i.e., pen
3. Samples : Keep it empty

Now, your Haar_Training folder looks like this


Now, navigate into that Haar_training folder through terminal. In the folder named "Positive_Images", the images are in png format and in "Negative_Images", they are in ppm format. So accordingly, I collected the information about those files in two text files named postives.dat and negatives.dat repectively using the below two commands


find ./Negative_Images -name '*.ppm' >negatives.dat
find ./Positive_Images -name '*.png' >positives.dat

In the next step, I used the samples folder, to store the training samples, by issuing it as arguement, along with positives.dat and negatives.dat using the below command

perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples  -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20"

The important arguements in the above command, to be discussed are
positives.dat - Contains the list of positive image paths
negatives.dat - Contains the list of negative image paths
samples - The folder that is used to store the data of training samples
250 - No. of training samples
-w 160 -h 20 - These two signify the width and height ratio of the pen. Originally it's -w 20 -h 20 for face. But, since I am using a pen here which is long and thin unlike face I made it 160 and 20


The next two commands use the data in samples folder to create a unified training data, that is used for haar training

find samples/ -name '*.vec' > samples.dat
./mergevec samples.dat samples.vec

Finally, run this command


opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 99 -w 160 -h 20 -nonsym -mem 2048 -mode ALL

This creates the haarcascade xml file desired by us in the same Haar_training folder, keeping all the meta data, that is forms in the meanwhile in the haarcascade folder, which is created automatically. The arguements to be noted in the above command are

haarcascade : The folder in which meta data can be kept
samples.vec : The unified training data, that we created just a while ago, before this command
negatives.dat : The list of negative image paths
20 - Stages, the more, the better, but great time consumer
99 - No. of negative images
2048 - The RAM memory that can be used

It took me five hours for this process to complete and generate my final pen detector. If you are a bit skeptic about the final outcome, instead of waiting for five hours, at any time, when the above command is running, you can issue the below command, to generate a intermediate haar cascade xml file, from the data available.

./convert_cascade --size="160x20" haarcascade haarcascade-inter.xml
Now finally after five hours, I got the haar cascade xml file. As I have shown you, in the previous posts, the way to use haar cascade xml files. I experimented on some pens of my own. Below is the youtube video for it.







Update (16 March, 2012): On the request of people I am sharing the haar classifier file for Pen detection. Below it the link for it.

Pen detector

Please note that I used very less sample images, less than a 100 to create it, while in reality we use at-least 5000-10000 images to create a robust classifier. So, it may give false positives in some cases

Update (25 March,2012): The link to the executable Linux files that I gave earlier is slowly loading or sometimes not working at all. So here's the new link for it

Linux Executables

Update (16 Nov,2012): Some of the commands, go out of page. For them select the first few words of the command; press shift+end; and then copy

143 comments:

marwa hmida said...

Hi,
Can you send me please your haar classifier?
Thanks in advance

Dileep Kumar said...

Hello Marwa,

I have updated this post(in the bottom) to include the link to the haar classifier. I have deleted your comment with your mail ID since it may introduce spam to you.

marwa hmida said...

Thank you very much

marwa hmida said...

Hi Dileep,
I can't download the executable linux files.
Can you send me your Haar_Training folder (with positive and negative images...).
Thanks in advance

Dileep Kumar said...

Updated the post; now you can download Linux executables, easily from the link given at the bottom of the post.

marwa hmida said...

And what about positive and negative images?

Dileep Kumar said...

That will be a very big database to share or send. Creating those databases shouldn't be a problem for you. For positive images, I just took photographs of my pen and cropped them, so that only the pen is visible. For negative images, take any kind of images - any image you want as long as the image doesn't have pen in it.

marwa hmida said...

Hi Dileep,
Is there any problem if i use negative images in png format?wahat is the software that you used to save images in ppm format?
thanks a lot

RINSON said...

do you have any other xml files for object detection.pls send it if you have.thnx in advance

Dileep Kumar said...

Marwa,
It's not a problem, but since the negative images I had were in ppm format and so inorder to find them all at a time I used '*.ppm'.

Rinson,
Sorry, I have the collection related to human biometrics like face, eyes, left eye, right eye etc.,

But it's very intuitive. Just let me know if you face any problem in following the above said process

tjv said...

hi dileep, i followed your steps for creating an xml for mobile phone detection. does the size of positive image affects the speed of training? i used 50*75 but the training seems to be stuck at stage 4. i tried it several times but the process get stuck either at stage3 or at stage 4
pls help..

Dileep Kumar said...

tjv,
Good that you tried to create one by yourself. Did you try to create an intermediate classifier at that stage using the last of the commands that I mentioned ? If yes, howz the performance using it?

Size of positive samples shouldn't be a problem. I used 157*1096 sized ones. And the timing speed solely depends on the number of training samples. How many are you using ? 250 ?

tjv said...

hi dileep,
first of all thanks for your reply,
i used 250 samples for the process
and this is where iam getting stuck,
Tree Classifier
Stage
+---+---+---+---+
| 0| 1| 2| 3|
+---+---+---+---+

0---1---2---3

Number of features used : 11017037

Parent node: 3

*** 1 cluster ***
POS: 6 6 1.000000

from here onwards the process wont proceed further i tried for more than 6 hrs also icant insert the create intermediate cascade command here.
can you suggest a solution?

tjv said...

hi dileep,
i created an intermediate xml file at stage 4,
but it showed lots of false results, its a 5kb file.

Dileep Kumar said...

tjv,

Did you change the -w and -h? These attributes tell d width and height in the above said commands. Since a pen's width is very high compared to it's length, I took '-w 160 -h 20'. This is the least sized object that I could find in a given scene. If it's size is greater than this, I could find, but if it's less, I fail. So as per the mobile phone size, change the dimensions to be the minimum level, but make sure you don't keep them so minimum that you lose robustness.

My final classifier is around 25Kb.

Also, try changing the various parameters in 'opencv_haartraining' command, like reducing the number of stages, increasing splits etc.,

Mario said...

Hi Dileep, can you share your pen detection code?

Dileep Kumar said...

Hello Mario,
It is exactly similar to the face detection code
http://opencvuser.blogspot.in/2011/06/face-detector.html
except when you refer the haarcascade xml file ( char filename[]="haarcascade_frontalface_alt.xml";), you refer to the haar cascade file that I mentioned in this post.

Mario said...

Really nice tutorial, thank you for the link's and code

nicolas said...

When I try the command. / Createtestsample me it generates no files ".vec" you have a solution?

ps: I tried on mac and linux same result...

Adil Soomro said...

Same here, it doesn't generate any ".VEC" file, I'm trying on mac.
below is the result, which is echoed during the process:

...
Info file name: samples//03BC7FCB287F4A2E.png/info.dat
Img file name: ./Positive_Images//03BC7FCB287F4A2E.png
Vec file name: (NULL)
BG file name: tmp
...

Any solution?

Dileep Kumar said...

When you run the command "perl createtrainsamples.pl positives.dat negatives.dat samples 250 ......" as stated in the above post, it should generate ".vec" files in the samples folder. Or is there some problem with perl in mac ? .

I tried in my Ubuntu and it's working fine

Adil Soomro said...

I figured that out. however now I'm stuck on
"mergevec samples.dat samples.vec"

I got yours mergevec.exec, but it gives error

"cannot execute binary file. "

Any Idea?

Dileep Kumar said...

what exactly is the problem you are getting when the command "mergevec samples.dat samples.vec" gets executed ?

Try using "./opencv_createsamples -info samples.dat -vec samples.vec -w 160 -h 20" instead of dat command and try again, replacing the correct width and height at -w and -h

Adil Soomro said...

Now the problem I'm facing is, I can't merge vec files using mergevec.exec, whenever I run this command
mergevec samples.dat samples.vec

it says:
bash mergevec: cannot execute binary file

Dileep Kumar said...

So the problem is with mergevec executable. Try changing it's permissions using chmod or running it in a shell other than bash.

Try to replace that command with the command in my previous comment and complete haar training

Adil Soomro said...

Thanks Dileep..!!

I copied all of my material to an Ubuntu system and perform mergevec there successfully and then came back to mac :p

Thanks again.

koji said...

I have a createtrainsamples.pl in my working directory but entering the command does not quite seem to output anything to my samples folder. im using windows and using cygwin terminal for all the linux commands. i've been reading your tutorial and http://note.sonots.com/SciSoftware/haartraining.html#tbc650a5 tutorial. hope you could help me with this.

Jad Chmaytilli said...

Dileep, I would like to thank you for posting this. It is exactly what I need for my project.

However, I am having issues concerning the "perl createtrainsamples.pl positives.dat ..." (it is createtestsamples.pl right ? just to make sure):

The command works and it is generating the samples with the distortions and all, but the problem is that it does not generate the .vec files. --> Vec file name: (NULL). Here is the output of one of the files:

opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 24 -h 20 -img ./Positive_Images/5.png -bg tmp -info samples/5.png/info.dat -num 31
Info file name: samples/5.png/info.dat
Img file name: ./Positive_Images/5.png
Vec file name: (NULL)
BG file name: tmp
Num: 31
BG color: 0
BG threshold: 0
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: 24
Height: 20
Create test samples from single image applying distortions...
Done

I hope you reply as soon as possible, and I hope that I am not taking a lot of your time.

Thanks in advance Dileep.

Dileep Kumar said...

@toall

I am busy till 26th of this month. Have to travel a lot.

Try the links given in the blog of sandarenu regarding haar cascade classifier. They were ample to solve majority of the problems.

But in between if i got time, i will look into your problems

Alex Wu said...

Thanks for the post Dileep, but I have an odd error. When trying to execute the mergevec, it says error while loading shared libraries: libml.so.2.1:cannot open shared object file: No such file or directory. I already chmodded mergevec with 777, is there anything else I should do?

hiposai said...

I cannot run the "./convert_cascade" command. The import files are missing.

Alex Wu said...

Additionally, nothing is created in the samples folder after executing the Perl program.

Moiz Adamjee said...

Hi Dileep .. Can u tell me the working of OCR i have to implement it using opencv libraries but dont know from where to start ..
tell me how can find a letter from an image and how can i make segments of every letter .. ??

thanks in advance..

Dileep Kumar said...

@Koji
I too followed that notes. Try with the createtrainsamples.pl in the link I have provided.

@Jad
Thanks for pointing it out. Actually it's what I have given, createtrainsamples only. But I forgot to keep that executable in the link. Now everything is set right. Try again with the createtrainsamples.pl provided in the link to executables to get .vec

@Alex Try chmodding the whole directory, where you are doing the haar training. Please try again for the samples folder (perl program), with the executable from the updated link of executables, now. I have updated it.

@Hiposai
Can you elaborate and paste the error here ?

@Moiz Optical Character Recognition or OCR is a very interesting field. I haven't worked on it, but one of my friends did research on extracting and recognizing the numbers from a number plate. You can read his thesis here http://ethesis.nitrkl.ac.in/3352/.

He worked with openCV in windows and he may be the one to help you better. Best of luck for your project.

thejoker said...

Hi Dileep,
I'm using OpenCV for a face detection application. I have a performance issue with face detection, and I want to train my own haar classifier to see if I can bring down the execution time. Do you have any idea as to what the minimum number of positive samples should be for decent accuracy? Also, do you have any approximation about time taken for training haar classifier vs. time taken for training lbp classifier?
P.S - I'm an alumni of NIT Rkl. :P

Dileep Kumar said...

For decent, atleast 5000 positive samples would do. But it's simply waste of time to make your own classifier, when there is already one. Don't try to reinvent the wheel. In the opencv installation folder, if you search, there is a folder exclusively for haar classifiers. In that there are around 3-4, exclusively for face. Try with them. I donno about lbp, but to train a decent haar classifier, it would take 15-20 days, if you leave your computer as is

Nour said...

Dileep, I would like to thank you for your great efforts
But please...I already downloaded the code in ur given link
http://opencvuser.blogspot.in/2011/06/face-detector.html
when i try it it build successfully but it gives me the error message
Assertion failed:cascade && Membuffer && capture line 46
I'm working on Win XP and VS 2010
Please Help me as I'm beginner in AR field

Quingshan Zhang said...

Hi Dileep, thanks very much for this article and it's really helpful. Just one small question, is there any width and height ratio requirments in the cropped images? I mean, do they have to be with the same width and height ratio when be cropped?

mesteruh mesteruh said...

opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 99 -w 160 -h 20 -nonsym -mem 2048 -mode ALL
Data dir name: haarcascade
Vec file name: samples.vec
BG file name: negatives.dat, is a vecfile: no
Num pos: 250
Num neg: 99
Num stages: 20
Num splits: 2 (tree as weak classifier)
Mem: 2048 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 160
Height: 20
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-07

Tree Classifier
Stage
+---+
| 0|
+---+


Number of features used : 6858870

Parent node: NULL

*** 1 cluster ***
POS: 0 0 -nan
OpenCV Error: Unspecified error (Unable to obtain positive samples) in cvCreateTreeCascadeClassifier, file /build/buildd/opencv-2.3.1/modules/haartraining/cvhaartraining.cpp, line 2467
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.3.1/modules/haartraining/cvhaartraining.cpp:2467: error: (-2) Unable to obtain positive samples in function cvCreateTreeCascadeClassifier

Decio Schmitt said...

Hello Dileep Kumar,

I'm trying to create my own classifier, firstly I would like to thank his otimo tutorial.

I am facing a problem and I'm very grateful if you could help me.
The mergevc compiled which you provided not worked on my 64-bit ubuntu.
I downloaded the source and tried to compile it into the source of the opencv haartraining however got an error:

erro fatal: cvhaartraining.h: file not found

Any idea how to solve?

Or by chance you do not have a mergevc compiled for 64-bit version to send me?

Thank you in advance.

Dileep Kumar said...

Sorry, that I don't have one for a 64-bit ubuntu version for the same. The problem you are facing is clear that the header file cvhaartraining.h is missing. Google for that header file and keep it in the same folder where you are doing the training and try again

Ronald said...

Hi, I got this error "./opencv_createsamples: ./opencv_createsamples: cannot execute binary file" when running "pecl ..." and "zsh: exec format error: ./opencv_createsamples" when running "opencv_createsamples ...".

Did you have any ideas about that??

Thanks in advance.

nguyễn sỹ huy said...

i have a problem . I run this command "perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20", but "createtrainsamples" is empty and no .vec file can be found. And i have a question about format of negative and positive images, can i use jpg format?

ABHI said...

hi Dileep i run all commands mention by you postive.dat,negative.dat ,sample.dat file is also made but when i run this command:
find samples/ -name '*.vec' > samples.dat
./mergevec samples.dat samples.vec
on terminal i got error bash: ./mergevec: Permission denied
please help on it
Thanks in Advance

Dileep Kumar said...

Hi Abhi,
Make "chmod 777 *" in the folder where you are running the tests. Let me know if the issue gets resolved.

Hi huy,
you cannot use jpg format; I haven't tested it, but even if it works, you know the fact the jpg uses compressioin techniques that take the originality of bits present per pixel away. So, better to stick with .bmp or .png

Are you referring to createtrainsamples.pl as being empty?

Ronald,
Please try in a 32-bit Ubuntu, that has proper perl and opencv 2.1 installation. Many people have reported problems with other variants..

ABHI said...

hi Dileep,
i still got same error as i new to linux ios so not much know about its commands .i paste here the command line o/p's please help me on this
root@nitin:/media/New Volume/Haar_Training# find samples/ -name '*.vec' > samples.dat
find: `samples/': No such file or directory
root@nitin:/media/New Volume/Haar_Training# ./mergevec samples.dat samples.vec
bash: ./mergevec: Permission denied

Dileep Kumar said...

Abhi,

It's pretty clear that there is no "samples" folder at all in the directory.

In the first step, where I suggested to create three folders, did you create "samples" - as one of the three. And then when you use "createtrainsamples..." command, the samples folder gets populated. Make sure, these both are done, before moving onto your next step

ABHI said...

Hi Dileep,
i created the sample folder when i run command to create sample.dat sample.dat file created inside samples folder & 1 outside inside Haar_Training but both contain no data (0 byte) after that i run command find samples/ -name '*.vec' > samples.dat
./mergevec samples.dat samples.vec
but it showing error O/p
root@nitin:/media/New Volume/Haar_Training# find samples/ -name '*.vec' > samples.dat
root@nitin:/media/New Volume/Haar_Training# ./mergevec samples.dat samples.vec
bash: ./mergevec: Permission denied

please help me on this im using AMD processor sysyem having Ubuntu OS .
thanks in Advance


ABHI said...

thanks alot dileep i use the link in which method to create haar xml for window system its works & i got xml

Vrushali Akant said...

Hi Dileep,
i m trying to create unified training data. i executed
find samples/ -name '*.vec' > samples.dat
./mergevec samples.dat samples.vec
these 2 command.
but i m getting the following error
./mergevec: error while loading shared libraries: libml.so.2.1: cannot open shared object file: No such file or directory
.
and libml.so.2.1 is not is usr/local/lib. i also tried to set the path in bashrc file.
any solution?


Dileep Kumar said...

This may be due to the problem with your openCV installation. Please see my previous post on openCV installation on how to confirm, whether openCV is properly installed or not

Vrushali Akant said...

Ok.. thank you. :)

Vrushali Akant said...

Hey Dileep,
I have installed OpenCV 2.4.1 from the following link.
http://www.samontab.com/web/2012/06/installing-opencv-2-4-1-ubuntu-12-04-lts/
All the samples are running fine.
So i guess the problem is not with installation.
Because I am getting the same error.

dgd said...

hi Dileep

when I run the last command, stay in this screen:

http://dl.dropbox.com/u/40163724/tela.png

it's normal before generating the haar cascade?

vikas60vasani said...

i am creating xml file for detecting a cell phone.
i have used 2000 positive images with size 320*240 and m creating samples with following command:
"perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 24 -h 24".

my question is that is it necessary to convert positive images to 24*24 size first or they are converted automatically?

i have doubt for -w(width) and -h(height) parameter in above command whether this parameters are size of object(cell phone) or they are size of positive image?

pls help...

Dileep Kumar said...

Hii dgd,
The pic is not opening due to some authentication issues, since I use my company laptop. Can you upload it in Picasa..

Hii Vikas60vasani,
They are converted automatically. They are the size of positive images, that are going to be generated by running that command

vikas60vasani said...

Hi dillep!!!
I m still confused with -w and -h parameters in createsamples.pl command.

Actually i have 320*240 sized images and i am creating samples with this command "perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 24 -h 24".

will it work?

Or i have ti use "perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 320 -h 240".

and if i keep my positive images size to 24*24 than it becomes very small and i m unable to mark the object from it in objectmarker tool. so i have kept the positive images size to 320*240. Pls tell me what should be my -w and -h values.(i m detecting cell phone in positive images)

pls help.

Dileep Kumar said...

Hii,

There is no 24*24 in the command I have mentioned. It's 160*20. That is the minimum size of the pen, that I want in an given large image to be detected. One more thing to note here is, that it is the size of pen, not the whole positive image(sorry that I have told it's positive image size in my previous comment).


Now let's go to your cell phone, which is 320*240 is width and height. If it is the cell phones width and height and it is the minimum size you want to detect, you can keep it as "perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 320 -h 240"". But if the whole image in which the cell phone is present is some 300*200 photo, then it's not possible to detect cell phone. Now the whole image, in which you are detecting cell phone should be greater than 320*240 in size.

vikas60vasani said...

In the following command:
perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20"

what means to 250..? it is no. of positive sample? or negative or Positive + negetive samples?

Dileep Kumar said...

First of all you have to understand the difference between both images. Positive images are images of pen and negative images are images of scenes with out pen. So putting the positive on negative images, will give us a scene with a pen inside it.

Those 250 are thus the scenes that contain pen, are being generated. These were needed to train our classifier

Manoharan H said...

Hi dear Friend's,
I'm doing my project in FPGA based Face recognition using Haar Classifier Algorithm. i want to know how to set the threshold value's in cascade(PIPELINE scheme) method.

After getting the two or three Haar-Like Feature, to add that feature value's. that result is Final haar feature classifier resultant value, then that resultant value compare with the Feature Threshold vale.

And also the accumulator value also compare with the Stage threshold value.

I have some doubt setting the threshold value's.

what is meant by feature threshold value, then how to set that feature threshold value? And

What is meant by Stage threshold value? How to set that stage threshold value's ?

I want detail about that threshold value's ???

by, Mano.H(TN)

HASHEM said...

^_^ thank u that's really helpful

Alok Kashyap said...

Thank you very much.
Its a good and nice tutorial.
Worked in windows.Had problem in the perl command in linux.

Hassan Murat said...

Hi Kumar.
I have a problem. If I want to merge samples.dat and samples.vec i am getting always an error:
Error while loading shared libraries: libcvaux file is too short

Please help
Thanks

Dileep Kumar said...

Hi Hassan,

'libcvaux' is a file related to opencv. So, I guess the problem is with opencv installation or may be due to compatibility issues. Can you try in open cv 2.1 and Ubuntu; If you don't have, can you try in one of your friends and check the result.

One thing I can suggest is to have a look at one of my previous posts of openCV installation, where I have given some checks to know whether it is properly installed or not

Hassan Murat said...

Hello Kumar,
After installing Ubuntu 10.04 with OpenCV 2.1 i am able to generate a xml-file.

Thanks for help

Hassan Murat said...

Hi
I have another problem now. I want to generate the xml file with 170 positive images and 100 negative images. After typing "opencv_haartraining -data haarcascade -vec samples.vec...." in the console there is always written "killed". Can you help please?
Thanks

Dileep Kumar said...

This is the first time I am hearing such problem. Can you paste the logs. I believe the error may be due to permissions to access the files. Can you give "chmod 777 *" and try again

Hassan Murat said...

Hello Kumar,
I can not give you more information because in the console there i can see only:
"Tree Classifier
Stage
0

Killed"

Info what i am doing:
1)I have 179 positive images. The positive images contains only the object of interest(size 200 x 150).
2)100 negative images.

My Commands:
1)find ./Negative_Images -name '*.jpg' >negatives.dat
2)find ./Positive_Images -name '*.png' >positives.dat
3)perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples - bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 200 -h 150"

4)opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 179 -nneg 100 -w 200 -h 150 -nonsym -mem 2048 -mode ALL

Another thing:
In my samples folder there are only 179 *.vec files. Is it right?

Thanks for help

Dileep Kumar said...

After -npos, give "250" in the 4th command you have mentioned, instead of 179 and try.


If the above doesn't work
===========================
Also, in your samples folder, there should be 250 *.vec files. I guess while running the 3rd command, you gave 179 in place of 250.

Marcus Vinicius Rangel Renno said...

Just to say thank you, man!!!

Pulak Bhushan said...

hi dileep sorry to disturb you my . vec file is not created after

perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20"

please help me if possible

Dileep Kumar said...

Are you sure, that positive.dat and negatives.dat exist. Also, any error messages appeared?

The .vec files were created inside "samples" folder. Did you check inside that?

Rachel Aron said...

Hi Dileep, this is a fantastic tutorial! Trying to get this to recognize flowers (specifically, daffodils)
However, I'm having the same problem that some others were having earlier in the comments. I'm using a Mac, and I'm having trouble with this step:

./mergevec samples.dat samples.vec
It returns "cannot execute binary file".

I was able to make the samples.dat file, but not the samples.vec, and I don't have access to linux as the other person did to solve their same problem. I'm very new at using terminal, and I"m not sure how to change the shell out of bash as you suggested. I've got the positives.dat and negatives.dat files. though.

Hassan Murat said...

Hello Kumar,

i solved my problems with this link:
http://nayakamitarup.blogspot.co.at/2011/07/how-to-make-your-own-haar-trained-xml.html

Thanks for your effort and time
Baris

Dileep Kumar said...

Cool man. If you have only windows that's the best solution.

Robert Svec said...

Thanks for the tutorial. Do you have any plans on releasing an updated tutorial for the current Ubuntu/64-bit/OpenCV versions?

Dileep Kumar said...

Hi Robert,
no man. Right now I am working full time and preparing for some exams.

Radiation Proctector said...

hello Dileep Kumar,

i'm using this command it didn't generate any file is it any mistake?? my picture is w=189 h=232(because more than one picture so the picture size will slightly different a bit)
perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples  -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20"

Dileep Kumar said...

Is it throwing any error. Can you paste it? The size of your picture is not an issue

aziza cheikhna said...

Thanks a lot for posting this tutorial;

i have a problem When I try the command. / Createtestsample on ubuntu it works without any errors but it doesn't generates any ".vec" files
can you help me plz ?


PS:

i used the same command i only edited the params:
perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 1183 -h 228"

Ricardo Ortega said...

Hi Dileep

I'm trying to make this work, but i got stucked when i try to execute the "./mergevec samples.dat samples.vec" part.
This is my error: ./mergevec: No such file or directory
i tried with chmod 777 and with sudo, and still doesn't work. Can you help me?

Dileep Kumar said...

Hi Ricardo,

Think either of the three files is missing. Are samples.dat and samples.vec are present in the same folder?

Dileep Kumar said...

Hi Aziza,

Is the samples folder created before hand?

Can you paste, if any error messages, that came after you gave the command.

soumeya aziza said...

Hi kumar, thx for this post

di you try to evaluate the performance of the generated classifier using the performance utility??

Can you post the results as soon as possible plz?

Dileep Kumar said...

Nope, I did not try to evaluate the performance, as anyway it will be lower. You need to use atleast 4000 positive images to construct a good classifier. Since I used only 7, I know the performance will be lower and hence did not try evaluating.

Dinh Nguyen said...

Hello Dileep Kumar,
I have a problem, when I try the command opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 98 -w 135 -h 132 -nonsym -mem 512 -mode ALL
This is my error:
OpenCV Error: Insufficient memory (Failed to allocate 65408 bytes) in OutOfMemoryError, file /home/xuandinh/opencv-2.4.4/modules/core/src/alloc.cpp, line 52
terminate called after throwing an instance of 'cv::Exception'
what(): /home/xuandinh/opencv-2.4.4/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 65408 bytes in function OutOfMemoryError

Aborted (core dumped)
Can you help me?
Thank you in advance

Dinh Nguyen said...

Hello Dileep kumar,
I have a problem, when I try the command:
opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 98 -w 135 -h 132 -nonsym -mem 512 -mode ALL
This is my error:
OpenCV Error: Insufficient memory (Failed to allocate 65408 bytes) in OutOfMemoryError, file /home/xuandinh/opencv-2.4.4/modules/core/src/alloc.cpp, line 52
terminate called after throwing an instance of 'cv::Exception'
what(): /home/xuandinh/opencv-2.4.4/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 65408 bytes in function OutOfMemoryError
Aborted (core dumped)
Can you help me?
Thank you in advance.

Dileep Kumar said...

Hello Dinh Nguyen,

This looks like some ram problem with in your computer. Are you using some old computer or a computer with a very low ram? You can try in some other computer with a good ram.


ilias20 said...

hello dileep,

i have tried the opencv_haartraining but it's get stucked on the 4th stage . any ideas?
thanks

utkarsh said...

Hello Dileep: I wanted to ask that when do you come to know that the training is complete? i waited for like 6 hours now and still i feel the training is not yet completed.. the number of positive sample i have given is 570 and negative samples is 1320..
The height and width are 24 and 24 respectively..
Please help me

Dileep Kumar said...

Hi ilias20,

Try increasing the min. resolution.

Hi utkarsh,

You can generate an intermediate classifer as I have written in the post and check the results. I have 99 negative and an equal amount of positive and it took me 6 hours (mine is a 2.1 GHZ, 3 GB RAM computer). For your size, better to take some highly configured computer and leave it over weekend.

Narendra Palavelli said...

Hi Deelip,
ur tutorial is good and nice

i studied different tutorials and i cant under stand after studying ur tutorial i got idea and i understood

when im trying to excute the following command i got some errors

perl createtrainsamples.pl positives.dat negatives.dat Samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 140 -h 190"

the errors are ...

./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 140 -h 190 -img ./Positive_Images/img_2.jpeg -bg tmp -vec Samples/img_2.jpeg.vec -num 36
./opencv_createsamples: error while loading shared libraries: libcxcore.so.2.1: cannot open shared object file: No such file or directory
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 140 -h 190 -img ./Positive_Images/img_20130818_095928.jpeg -bg tmp -vec Samples/img_20130818_095928.jpeg.vec -num 36
./opencv_createsamples: error while loading shared libraries: libcxcore.so.2.1: cannot open shared object file: No such file or directory
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 140 -h 190 -img ./Positive_Images/img_20130.jpeg -bg tmp -vec Samples/img_20130.jpeg.vec -num 36
./opencv_createsamples: error while loading shared libraries: libcxcore.so.2.1: cannot open shared object file: No such file or directory
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 140 -h 190 -img ./Positive_Images/img_20130818_095921.jpeg -bg tmp -vec Samples/img_20130818_095921.jpeg.vec -num 36
./opencv_createsamples: error while loading shared libraries: libcxcore.so.2.1: cannot open shared object file: No such file or directory

and im using OpenCV2.4.1 O S Ubuntu 12.04

address me for vedio & text tutorial

so can you help me to know the
how to create our calssifer features.


Thank you in advance....

Dileep Kumar said...

Hi,

Please try with openCV 2.1. There is some problem with newer versions with shared libraries. Earlier also, many people posted the same. Just for this one step, execute it in one of your friend's computers who have openCV 2.1 installed.

Narendra Palavelli said...

Thankyou Dileep

and
1. i tried in another computer with the same version but i didnt get any error like this. any how then fine but

when i run this command it crates only the +ve number of images only but not 250 images
Ex.. in +ve image folder there are 10 images, it creates only 10 ".vec" files in samples folder after run this command even i gave 250.
why so..? or is there any mistake ..?

perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20"


2. what are the minimum images of +ve and -ve images?
to detect the object.

thank you in advance...

Hassan Mehmood said...

Thankyou Dileep

but "For most of the dough, that is going to come, you will need these executable linux files. Here's the link for it."


is not working could you suggest alternatives

Dileep Kumar said...

Hi Hassan,

Just scroll to the end of the post; I gave one more link there.

Abdullah Jamal said...

Hey Kumar ,very useful blog, I have a amateur level in OPENCV but I have a problem ,i have to detect patterns using haar classifer. The problem is, I'm using windows 7 and in OPENCV/apps/haartraining i found the c files of create samples , but i dont knw how to use them, they havent been compiling since i have been working.can u tell how createsample.cpp can be used in windows?

Hassan Mehmood said...

hi Dileep

Thanks for ur help


Dileep, when i am executing the "perl createtrainsamples.pl.........-w 160 -h 20""

i am getting this reply and it does not creating any files in my samples folder....


./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 160 -h 20 -img ./Positive_Images/IMG_3873.jpg -bg tmp -vec samples/IMG_3873.jpg.vec -num 250



i m using the jpg format for my positive images.... can you plz elaborate the problem i am using opencv 4.0




Natal Henrique Cordeiro said...

Hi Kumar. Ilike your stuff. All steps were successful until this command (opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 99 -w 160 -h 20 -nonsym -mem 2048 -mode ALL) that generated the following result:


root@byteapath-pc:/home/byteapath/Área de Trabalho/Haar_Training_Imp2# opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 99 -w 160 -h 20 -nonsym -mem 2048 -mode ALL
Data dir name: haarcascade
Vec file name: samples.vec
BG file name: negatives.dat, is a vecfile: no
Num pos: 250
Num neg: 99
Num stages: 20
Num splits: 2 (tree as weak classifier)
Mem: 2048 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 160
Height: 20
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-07

Tree Classifier
Stage
+---+
| 0|
+---+


Number of features used : 6858870

Parent node: NULL

*** 1 cluster ***
OpenCV Error: Unspecified error (Vec file sample size mismatch) in icvGetHaarTrainingDataFromVec, file /build/buildd/opencv-2.1.0/apps/haartraining/cvhaartraining.cpp, line 1925
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.1.0/apps/haartraining/cvhaartraining.cpp:1925: error: (-2) Vec file sample size mismatch in function icvGetHaarTrainingDataFromVec

Aborted


You've got the same error?

Dileep Kumar said...

Hi,
i guess you missed out something in samples.vec creation. Please follow the steps carefully. Also please make sure that positives.dat and negatives.dat files are intact and have correct paths to exact number of images they represent

Trigo89 said...

Hi there, thanks for this tutorial !
When I'm running the following command:
sudo ./mergevec samples.dat samples.vec
I get this error:
sudo: ./mergevec: command not found
Any suggestion ? =/
Thanks !

Dileep Kumar said...

Hi,
sudo is not needed. Also make sure, a file called "mergevec" is present in the path you are running the command.

Trigo89 said...

Thanks :)
I'm running on the folder "Haar_training" which have the original mergevec (executable). I've to add other mergevec file?

Airvikar said...

how to do opencv_traincascade on Ubuntu (64-bit):
http://ubuntu-wine.ru/publ/ubuntu_zapisnaja_knizhka/ubuntu_opencv_obuchenie_kaskadnogo_klassifikatora/2-1-0-34

Ioanna Charalambous said...

when run the command find ./Positive_Images -name '*.png' >positives.dat
it returns File not found - '*.png' and my images is saves with png format. Do you know what is the problem?

Thanks in advance

Dileep Kumar said...

Is it .PNG?

Foxy said...

hello ,
really thanks for the tutorial,it helped me but i have a problem.
when i try run :

perl createtrainsamples.pl positives.dat negatives.dat samples 1500\
"opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\
-maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 20"

it show me error :
opencv_createsamples: error while loading shared libraries: /lib64/libImath.so.6: file too short

can you please help me ?

Foxy said...

hello ,
really thanks for the tutorial,it helped me but i have a problem.
when i try run :

perl createtrainsamples.pl positives.dat negatives.dat samples 1500\
"opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\
-maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 20"

it show me error :
opencv_createsamples: error while loading shared libraries: /lib64/libImath.so.6: file too short

can you please help me ?

sd s said...

Hi, I have a problem. When I use [perl createtrainsamples.pl positive.dat negative.dat Samples 300] it appears that the operation goes well but I can not find any files in my Samples folder. Please help me!

asaaki said...

Hi Dileep Kumar,
Thanks for your tutorial!
I have a question, can OpenCV handle samples as irregular polygons? Do they always have to be simple rectangles?

Dileep Kumar said...

Hi Asaaki,

From my experience, I don't think it can. I think it's possible to single out rectangular focus area from an irregular polygon.

SriSam said...

Hello I am using JavaCv(new to this), how to create haar cascade classifer ? I am working with NetBeans IDE. And can you please explain about -w ad -h parameters ?

Mohammed Magdy Ismael said...

please..your email ?

Dileep Kumar said...

dileep98490@gmail.com

raviteja said...

Hi Dilleep,
can you please look in to my issue . http://answers.opencv.org/question/27653/unspecified-error-in-persistencecpp-during/ .
thanks

Dileep Kumar said...

Problem with your openCV installation.Try to see, in one of my earlier posts on openCV installation; Of how to check whether openCV is installed in proper

srinivas nair said...

Hey Dileep,thanks for the tutorial.I have followed the steps you have said.At the end when I execute the following command on the terminal it threw me an error initially saying that "permission is denied" later on I have changed the permissions using chmod.Later on when I execute the same command it is showing up as something like this.Could you help me in this regard..."cannot open shared object file:No such file or directory" when it is already present

Marco Gallardo said...

Hello I need help with my homework about Haar-cascade detection in open CV, is anybody good with paython programming to help me. and interested in some tutoring remotely please let me know at megrobelly@hotmail.com for more details. thanks

DJ Rafa said...

hi Dileep, thank you for u're tutorial, but i have problem with the 3th step, with the "createtrainsamples.pl " , the samples folder is still empty. thanks !!

Dileep Kumar said...

Hi Rafa,
Did the command run perfect or it exited with some error. Also, can you post the exact command that you ran

DJ Rafa said...

i ran < perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134" >

exactly like u're example, i did copy/paste, i have no error but as result i have something like that :

./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000003a.png -bg tmp -vec samples/crop_000003a.png.vec -num 42
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000002b.png -bg tmp -vec samples/crop_000002b.png.vec -num 42
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000001a.png -bg tmp -vec samples/crop_000001a.png.vec -num 42
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000004a.png -bg tmp -vec samples/crop_000004a.png.vec -num 42
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000002a.png -bg tmp -vec samples/crop_000002a.png.vec -num 41
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 70 -h 134 -img ./Positive_Images/crop_000003b.png -bg tmp -vec samples/crop_000003b.png.vec -num 41

Thanks for helping , and sorry for my bad english :)

Dileep Kumar said...

Hi Rafa,
From the output, you can see that samples/crop_000003b.png.vec, samples/crop_000002a.png.vec. It means it is trying to create the .vec files with in samples folder. But is is not able to do so. Can you check whether the samples folder has all Read/Write permissions. Also, can you rerun the script and check for error messages again. Also, make sure you are giving the folder name correct "samples". Is it the exact same name of the folder present in the directory, or are you missing "S", something like that.

DJ Rafa said...

i checked, permissions are ok, same thing with folder name, i tried again and again with no sucess.
however i used haartraining in windows using cygwin and following ( http://note.sonots.com/SciSoftware/haartraining.html ) , i used their binaries for windows , it worked ( generate the xml ) , but with the ./haartraining , it works only with -w 24 -h 24 or less , when i try to use -w 70 -h 134 for example , it runs about 1 minute after stopping with error " OpenCV gui error handler insufficient memory " , any ideas about the origin of the problem ??

yovenita said...

Hello, I'm confused about the height and width i should use.
I'm creating a haar cascade classifier to detect cars in an image and i'm confused about which height and width i should use.

Please help. Thank You

Ajith Francis said...

Hi Dileep,

Very good tutorial. Thanks a lot. It seems like createtrainsamples.pl is missing in my download. Let me try download once again.

-- Ajith

Dileep Kumar said...

Hi Ajith,
Most of the files are sourced from different sources in the internet. Even if something is missing, you can google and get it.

jokeat NG said...

Hi! I managed to execute most of the command lines except for the ./mergevec samples.dat samples.vec command

Error was : bash: ./mergevec: cannot execute binary file

Initially it was permission denied but I ran chmod 777 and I got this. Thanks for the help!

Sawper hayabosa said...

Hi Dileep
unfortunately cant finish the training because of bash:cannot execute binary file on opencv_createsample.
chmod 777 has no effect , what must i do ,bored of searching...
thanks.

Dileep Kumar said...

What is the exact error message it is showing?

Pablo Ramiro Tozzetto said...

hi this is my second post, i read your post here and im really interesting. Im doing a human detector in python but i have a problen with this when the human is not in front position... i know that i have to do my xml file with people in an other position but, could it be posible that you have any xml file for human detection? if yes, could you sen me it?
Good job, and thanks!

Dileep Kumar said...

I too only have in front position. Here you can get some classifier files. You can grep

http://alereimondo.no-ip.org/OpenCV/34/

Sandeep Dhawan said...

Dileep, First of all thanks for posting this tutorial. I have followed your tutorial to do exactly your example for the pen and created the haarclassifier file. I am using OpenCV to detect the image. I dont know why but there is always a flickering rectangular box on the image even with no pen, as if it is detecting a pen, unlike on your post. The haarclassifer ended with an error of 0.0003625 after 20 stages. Can you 1) Share with me your OpenCV file you used to detect the pen. and 2) Any thoughts on why I am getting this box. I tried changing the background to a completely white one by putting a piece of paper in front of the camera, and then this rectangular box does not show up. 3) The box does not get created if I hold the pen vertical-How can this be changed so that no matter what angle the pen is held at it recognizes it. Thanks for your prompt response.

Dileep Kumar said...

Hi Sandeep,
It is expected. I will answer your points one by one.
1. It will be same as yours
2. This is because the samples size is less, so it cannot be accurate. Furthur, you are using one pen only. For greater accuracy, I recommend using atleast 10,000 positive/negative images.
3. You can create another xml file for the verticle angle and put it in the loop, so that when you hold it vertical, the vertical xml file gets triggered. But for all angles it may not be possible. Because, if you hold it in 45 degrees, you may have to draw a parellelogram, but not rectangle around it, which is quite complex.

Sandeep Dhawan said...

Dileep,

Thanks for question 1) attached is the code I am using. Specifically the parameters on this opencv command are what I am wondering what you used.

Rectangle[] faces = opencv.detect( 1.2, 3, 0, 0, 0 );


CODE IS BELOW FROM PROCESSING
____________
parameters import hypermedia.video.*;
import java.awt.*;
OpenCV opencv;

void setup() {
size( 640, 480 );
opencv = new OpenCV( this );
opencv.capture( width, height );
opencv.cascade("haarcascade_final.xml");

}

public void stop() {
opencv.stop();
super.stop();
}


void draw() {

// grab a new frame
// and convert to gray
opencv.read();

// proceed detection

Rectangle[] faces = opencv.detect( 1.2, 3, 0, 0, 0 );
//Rectangle[] faces = opencv.detect( );
// display the image
image( opencv.image(), 0, 0 );

// draw face area(s)
noFill();
stroke(255,0,0);
for( int i=0; i<faces.length; i++ ) {
rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
}
}

jokeat NG said...

Hi again. My problem started when I ran the perl createsamples.pl. It ran for a bit but nothing was created in the samples folder. Initially I thought it was nothing so I ran the next step which is the merge.vec command but then I got an error where it stated

Error was : bash: ./mergevec: cannot execute binary file

Please assist, its relating to my FYP. Thanks !

Tomáš Škvarek said...

Hi,

is there any way or tut how to use multiple classifiers. For example i want to detect pen and an apple simultaneously. Thank you for any advice.

Tomas

Fabiano Leite said...

Hello Dilleep,
Great post, thanks.
I have followed your instructions :
1- I took some photos from a pen, in my case face, to form positive samples.
2- I took any photos from any places and thing to form negative samples.
3- I have gotten some files.txt from photos (positive and negative).
4- I used the command “opencv_ createsamples” and I have gotten samples.txt
5- I used the command “ mergev “ I have gotten samples.vec.
Finally when I try running this command:
opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 200 -nneg 1000 -w 500 -h 300 -nonsym -mem 2048 -mode ALL
I have gotten this:
Fabiano@SERVIDOR-PC /cygdrive/c/HAAR/opencv-haar-classifier-training
$ opencv_haartraining -data classifier -vec samples.vec -bg negatives.txt -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 200 -nneg 1000 -w 500 -h 300 -nonsym -mem 2048 -mode ALL
Data dir name: classifier
Vec file name: samples.vec
BG file name: negatives.txt, is a vecfile: no
Num pos: 200
Num neg: 1000
Num stages: 20
Num splits: 2 (tree as weak classifier)
Mem: 2048 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 500
Height: 300
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-007

Tree Classifier
Stage
+---+
| 0|
+---+


OpenCV Error: Insufficient memory (Failed to allocate 65408 bytes) in unknown function, file ..\..\..\..\opencv\modules\core\src\alloc.cpp, line 52






I have ran the code in the follow computers:
1 – Quad core intel with 4G of RAM.
2- Notebook i5 with 4G of RAM .
But I have had the same results above.
Questions:
1- The formats of positive pictures “photos” must be like below:
2- img/img1.jpg 1 140 100 45 45
3- img/img2.jpg 2 100 200 50 50 50 30 25 25
4- img/img3.jpg 1 0 0 20 20
Or just: img1.jpg ?

5- The negative pictures can have any formats and resolutions?
6- When a ran the code:
perl createtrainsamples.pl positives.dat negatives.dat samples 1000 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 500 -h 300"

I have gotten “SAMPLES.TXT” only the same numbers of positive_ samples (200 photos). It is not the numbers of “samples 1000”. Is it correct?
7- What is the best computer configuration to run this application?
8- Must the parameters “-mem 2048” limit the memory used? If yes. Why my computers have stopped run code?
Best regards
Fabiano.

Fabiano Leite said...

Hello Dilleep,
Great post, thanks.
I have followed your instructions :
1- I took some photos from a pen, in my case face, to form positive samples.
2- I took any photos from any places and thing to form negative samples.
3- I have gotten some files.txt from photos (positive and negative).
4- I used the command “opencv_ createsamples” and I have gotten samples.txt
5- I used the command “ mergev “ I have gotten samples.vec.
Finally when I try running this command:
opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 200 -nneg 1000 -w 500 -h 300 -nonsym -mem 2048 -mode ALL
I have gotten this:
Fabiano@SERVIDOR-PC /cygdrive/c/HAAR/opencv-haar-classifier-training
$ opencv_haartraining -data classifier -vec samples.vec -bg negatives.txt -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 200 -nneg 1000 -w 500 -h 300 -nonsym -mem 2048 -mode ALL
Data dir name: classifier
Vec file name: samples.vec
BG file name: negatives.txt, is a vecfile: no
Num pos: 200
Num neg: 1000
Num stages: 20
Num splits: 2 (tree as weak classifier)
Mem: 2048 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 500
Height: 300
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-007

Tree Classifier
Stage
+---+
| 0|
+---+


OpenCV Error: Insufficient memory (Failed to allocate 65408 bytes) in unknown function, file ..\..\..\..\opencv\modules\core\src\alloc.cpp, line 52






I have ran the code in the follow computers:
1 – Quad core intel with 4G of RAM.
2- Notebook i5 with 4G of RAM .
But I have had the same results above.
Questions:
1- The formats of positive pictures “photos” must be like below:
2- img/img1.jpg 1 140 100 45 45
3- img/img2.jpg 2 100 200 50 50 50 30 25 25
4- img/img3.jpg 1 0 0 20 20
Or just: img1.jpg ?

5- The negative pictures can have any formats and resolutions?
6- When a ran the code:
perl createtrainsamples.pl positives.dat negatives.dat samples 1000 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 500 -h 300"

I have gotten “SAMPLES.TXT” only the same numbers of positive_ samples (200 photos). It is not the numbers of “samples 1000”. Is it correct?
7- What is the best computer configuration to run this application?
8- Must the parameters “-mem 2048” limit the memory used? If yes. Why my computers have stopped run code?
Best regards
Fabiano.

Anh Phạm said...

Hi Dileep Kumar
I just begin with opencv, after create project in vs10 and link with opencv lib I try this code below:
http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier
But when I debug, the console has "--(!)Error loading"
what wrong? Can you help me please?

Anh Phạm said...

Hi Dileep Kumar
I just begin with opencv, after create project in vs10 and link with opencv lib I try this code below:
http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier
But when I debug, the console has "--(!)Error loading"
what wrong? Can you help me please?

Sane Schio said...

Hi Dileep, i've follow every single stelp and when i run
" opencv_haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 99 -w 160 -h 20 -nonsym -mem 2048 -mode ALL "
my terminal cames to
"
Data dir name: haarcascade
Vec file name: samples.vec
BG file name: negatives.dat, is a vecfile: no
Num pos: 250
Num neg: 99
Num stages: 20
Num splits: 2 (tree as weak classifier)
Mem: 2048 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 160
Height: 20
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 9.53674e-07

Tree Classifier
Stage
+---+
| 0|
+---+


Number of features used : 6858870

Parent node: NULL

*** 1 cluster ***
POS: 250 250 1.000000
NEG: 99 1
BACKGROUND PROCESSING TIME: 0.00
Precalculation time: 0.00
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+

| 1|100%|-| 1.000000| 1.000000| 0.000000| 0.000000|
+----+----+-+---------+---------+---------+---------+
Stage training time: 548.00
Number of used features: 2

Parent node: NULL
Chosen number of splits: 0

Total number of splits: 0

Tree Classifier
Stage
+---+
| 0|
+---+

0


Parent node: 0

*** 1 cluster ***
POS: 250 250 1.000000
NEG: 99 0.258486
BACKGROUND PROCESSING TIME: 0.00
Precalculation time: 0.00
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+

" and it here for something like 5 minutes.
What is wrong?

Punith Krishnegowda said...

After Executing,I am uisng Ubuntu 14.04 LTS

perl createtrainsamples.pl positives.dat negatives.dat samples 100 "./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160"

I got following results,
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive4.jpg -bg tmp -vec samples/positive4.jpg.vec -num 13
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive8.jpg -bg tmp -vec samples/positive8.jpg.vec -num 13
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive.jpg -bg tmp -vec samples/positive.jpg.vec -num 13
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive1.jpg -bg tmp -vec samples/positive1.jpg.vec -num 13
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive3.jpg -bg tmp -vec samples/positive3.jpg.vec -num 12
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive2.jpg -bg tmp -vec samples/positive2.jpg.vec -num 12
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive6.jpg -bg tmp -vec samples/positive6.jpg.vec -num 12
./opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 160 -img ./Positive_Images/positive5.jpg -bg tmp -vec samples/positive5.jpg.vec -num 12


It hasn't created any samples.dat file in samples folder, but it created positives.dat and negatives.dat files successfully. please do help me find the solution.

-- Thanks a lot for your detailed information.

Post a Comment