You are on page 1of 11

http://nayakamitarup.blogspot.com/2011/07/how-to-make-your-own-haar-trained-xml.

html

How to make your own haar trained ".xml" files


hiii friends Today i write on how to make ur own xml file. It took me long to figure it out. The basic requirement is for u to have OpenCV installed and integrated to ur IDE. If u haven't, then u could do it by following this link: http://nayakamitarup.blogspot.com/2011/05/introduction-to-opencv-for-image.html Now that u have installed OpenCV, u r ready to go. To start, dowload the "tools.exe" from the link below: http://www.4shared.com/file/mpbQkWp4/tools.html Unzip the tools.exe into a folder. U will find the contents like given below:

For training, u would need a database of thousands of images, with and without the object u want to train the xml file for, respectively called as positive and negative images. Near about 1000-2000 positives and 3000 negetives, sound fine for training. Now u need to place all the positive images(images with object) inside the folder "\temp\positive\rawdata". Now that u have placed all the positives, execute the "objectmarker.exe". On executing it, it will show the image, and u need to mark the area containing the object. and u get some thing like this:

Then press "Space Bar". It will feed the coordinates of the bounding rectangle into a text file "info.txt". Then Press "Enter" to go to next image, and continue similarly till u are done with all the images. This is the most tiring part, and makes u wanna kill urself :P . WARNING: Donot kill urself, have patience. :) Now u can see the "info .txt" looks somthing like this:

Phew!!!! Done with the most tiring part. Now insert all the negetive images(images without the object) or the so called background images into the folder "temp\negative", and execute the "create_list.bat" file. On executing it, it stores the names of negative images along with extension, into a text file "infofile.txt" and it looks something like this:

Now u need to create samples for haar training. It is done by typing the following command in "start>run" C:\Users\amit\Desktop\hand\lasssssssssssssst\temp\createsamples.exe -info positive/info.txt -vec data/vector.vec -num 527 -w 24 -h 24

Here, to use this command u need to edit it , with the number of positive and negetive images. Since I had used 527 positive images and 1142 negetive images and 30 stages, u can change it according to use by simply replacing the numbers in the command. Now that the samples have been created, u can start haar training. The same procedure is to be followed. Type the command in "start->run". C:\Users\amit\Desktop\hand\lasssssssssssssst\temp\haartraining.exe -data data/cascade -vec data/vector.vec -bg negative/infofile.txt -npos 527 -nneg 1142 -nstages 30 -mem 1000 -mode ALL w 24 -h 24 -nonsym This starts the haar training, and u can see on the window, something like this:

This will continue upto the stage u have mentioned. It takes hell lot of a time, and during this time it occupies nearly 60% of the processor. As the training proceeds,u will find folders forming with the text file "AdaBoostCARTHaarClassifier.txt" forming and a "vector.vec" file is also formed.

On completion of training, replace the "data" in "haar\cascade2xml" by the "data" folder in"haar\temp", and copy the "vector.vec" to location "haar\cascade2xml". Now execute the "convert.bat" file. It finally forms the output.xml file. Congrats, u made ur own xml file :) Now to detect using the xml file the code is:

#include <opencv/cv.h> #include <opencv/highgui.h> #include <stdio.h>

#include <stdlib.h> #include <string.h> #include <assert.h> #include <math.h> #include <float.h> #include <limits.h> #include <time.h> #include <ctype.h> const char *cascade_name="openpalm.xml"; void detect_and_draw( IplImage* img ) { // Create memory for calculations static CvMemStorage* storage = 0; // Create a new Haar classifier static CvHaarClassifierCascade* cascade = 0; // Sets the scale with which the rectangle is drawn with int scale = 1; // Create two points to represent the hand locations CvPoint pt1, pt2; // Looping variable int i; // Load the HaarClassifierCascade cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); // Check whether the cascade has loaded successfully. Else report and error and quit if( !cascade ) { fprintf( stderr, "ERROR: Could not load classifier cascade\n" ); return; } // Allocate the memory storage storage = cvCreateMemStorage(0); // Create a new named window with title: result cvNamedWindow( "result", 1 ); // Clear the memory storage which was used before cvClearMemStorage( storage ); // Find whether the cascade is loaded, to find the hands. If yes, then: if( cascade ) { // There can be more than one hand in an image. So create a growable sequence of hands. // Detect the objects and store them in the sequence CvSeq* hands = cvHaarDetectObjects( img, cascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING,

cvSize(40, 40) ); // Loop the number of hands found. for( i = 0; i < (hands ? hands->total : 0); i++ ) { // Create a new rectangle for drawing the hand CvRect* r = (CvRect*)cvGetSeqElem( hands, i ); // Find the dimensions of the hand,and scale it if necessary pt1.x = r->x*scale; pt2.x = (r->x+r->width)*scale; pt1.y = r->y*scale; pt2.y = (r->y+r->height)*scale; // Draw the rectangle in the input image cvRectangle( img, pt1, pt2, CV_RGB(230,20,232), 3, 8, 0 ); } } // Show the image in the window named "result" cvShowImage( "result", img ); } int main() { // Gets the input video stream from camera CvCapture* capture = cvCaptureFromCAM( 0 ); // Checks if the input stream is obtained if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } // Show the image captured from the camera in the window and repeat while( 1 ) { // Get one frame IplImage* frame = cvQueryFrame( capture ); // Cecks if a frame is obtained if( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } // Flips the frame into mirror image cvFlip(frame,frame,1);

// Call the function to detect and draw the hand positions detect_and_draw(frame); //If ESC key pressed exit if( (cvWaitKey(10) & 255) == 27 ) break; } // Release the capture device housekeeping cvReleaseCapture( &capture ); return 0; }

Posted by @mit at 08:29 Email ThisBlogThis!Share to TwitterShare to Facebook

23 comments:
1. AndySep 6, 2011 05:53 PM So far so good, just waiting for training to be complete...

Wondering how well this will actually work. You are like the only person I could find that has wrote this tutorial. Thank you, I have been fighting OOM errors, no clue why..

One thing I am concerned about is the low resolution 24x24, is that something to worry about? I used the createsamples with just the -vec flag and that allows you to view your samples, they are so low resolution you can hardly see them. If you increase to anything higher then 24x24 you get OOM errors... I'm only working with 50 positives right now, not sure how well that will work but its my first go at it. I'd like to get multi threading going on, I have a 16 core system it can handle much more and would likely complete much faster, I saw comparisons going from single to multi 24 hours to 3 hours. :P tip: If you type the word "you" you will look more professional ;) Reply

2. @mitSep 7, 2011 06:01 AM OOM errors may be due to your operating system being unable to create any more virtual memory. About the low resolution 24x24, it wont be a problem. I have tried and its working great.

And if you do multi threading, do tell me how it goes. :) Reply 3. AndySep 8, 2011 06:48 PM .. was deep into a 48 hour multiple learning session... and I bluescreened.... stage 16 of 20 ... anyway to resume? As for multi threading... I'm going to try and recompile with TBB.. but this time in a VM in linux.. if my host bluescreens I'll have auto protect enabled and I'll be able to resume.. Reply 4. AndySep 8, 2011 08:34 PM I converted what was left in the directory and it works although I am getting some false positives because all the images were taken in the same room that I'm testing in... As for TBB and VS2010 support I found a wonderful tutorial on using CMake to generate VS10 projects that will compile openCV with TBB support. I read some place that the TBB support will cut the training time from 24 hours to 3 hours, I'm running out of time tonight so I'll give it a go tomorrow. http://www.youtube.com/watch?v=XeBhwbRoKvk Reply

5. @mitSep 9, 2011 10:04 AM To resume you have to start the training again. It will resume itself from where it was last trained upto. Reply 6. AndySep 11, 2011 08:32 AM Oh that's nice. Do you know if you would get better accuracy using positive images that are PNGs of only the content you want to detect with a transparent background? Reply Replies

1.
@mitFeb 24, 2012 10:22 AM Yaa its giving better results..

Reply

7. AndySep 11, 2011 08:40 AM BTW, I recompiled OpenCV with TBB and it appears to only multithread the detection, this is great but the newest version doesn't appear to support multi-threaded training. I haven't tested the performance increase you get from that just yet. I also plan on using the CUDA support. I have been using the Microsoft Kinect and EmugCV, I've built a flimsy .NET adapter for the camera. I'd like to come up with a complete package that provides a single interface for the skeleton tracking, IR depth camera, and video camera. Something that will provide much more information than the beta SDK that is out. Reply

8. @mitSep 11, 2011 10:37 AM Thats nice..I must give it a try training images with transparent background.

And i think I will get myself a Kinect :) Reply 9. AndySep 19, 2011 08:06 PM Compiled OpenCV haartrain with OpenMP, performance doubles, load is balanced across all cores, I'm still bound by certain operations that cannot be multithreaded but its an improvement in training time. Instead of 1 core running at 100%, I have 4 cores running at 50% each. Reply 10. josephFeb 3, 2012 01:12 AM what is openmp? Reply Replies

1.
@mitFeb 24, 2012 10:16 AM OpenMP Open Multi-Processing The OpenMP API supports multi-platform shared-memory parallel programming in C/C++ and Fortran. Reply

11. KamranFeb 22, 2012 05:12 AM Dude thanks for the tutorial. It is very helpful but I faced a problem at the end. Everything worked fine but when tried the convert.bat it gives me an error. This is the error: "Null pointer (NULL in function D:\User\VP\opencv\cxcore\src\cxpersistence.cpp(5097)" object pointer) cvSave,

But I don't have any D drive. If you could help me then it will be really greatful. K Out! Reply Replies

1.
@mitFeb 24, 2012 10:10 AM Hii Kamran.. U have not copied the files from the location- "temp/data/cascade/" to "cascade2xml/data/" Also copy the file vector.vec from location "temp/data/" to "cascade2xml/" After copying files try using the convert.bat. It will work.

Hope it is helpful :)

2. KamranMar 19, 2012 01:51 AM Dude sorry for the late reply had exams. I did do that before and did do that after you told me to but it still gives me the same error.

3. @mitMar 19, 2012 07:12 AM I tried everything and found that only not copying the files gave the same error as u have.... Are u doing it right? It would be helpful if u could tell me the steps followed..

4.

JustinApr 3, 2012 12:00 AM i have the same problem with Kamran. Reply

12. marwa hmidaMar 14, 2012 07:29 AM Can you post a Thanks in advance Reply Replies code for detecting an object using the xml file please?

1.
@mitMar 18, 2012 09:50 AM Hii Uploaded Hope its helpful the code

2. marwa hmidaMar 19, 2012 01:53 AM Thank you :) Reply

13. Shaurik KMar 19, 2012 06:44 AM Hi @mit, amazing work you have here! Thanks for the information - helped me ALOT - I was stuck on producing positives until I saw your tutorial. I'm haar training right now, and am using 1600 positives and around 2000 negatives with 30 stages - and it's taking FOREVER! would you recommend reducing the number of stages to a smaller number? what effect does that have on the result? Thanks SK Reply Replies

1.

@mitMar 19, 2012 07:45 AM Hii SK It takes forever really, makes u want to kill urself :) BTW how long have u been training? Reducing is not a good option if u want to develop a gud one, but if u got less time then reduce it. If u take a small number of stage, then the cascade formed will not be properly trained, i.e. before reaching the false alarm. Refer to the link for more information http://nayakamitarup.blogspot.in/2011/06/gesture-recognition-using-haar.html

2. Shaurik KMar 19, 2012 04:36 PM Hi @mit, I've been Haar training for almost 3 days now, and its on stage 7 of 30. And thanks for the link, I understand now. Hopefully I can get my hands on some sort of super computer so i dont have to wait till the end of time :P :P Thanks SK again!

You might also like