xmlopencvhaar-classifier

How to create your own Haar Cascade Classifier for custom object detection?


I want to detect cars, but the available xml file is not quite strong for accurate detection.How can I create a custom xml file(which i can create for any object)?

Any help would be appreciated.


Solution

  • There are some steps about how to create your own cascade.

    My references:

    1- Data Collection

    You need positive(which means include the object you want to train) images and negative(which your object doesnt exist in the image) images. According to people experiences the number of positive images should be double of negative images

    Note: In my experiences and also documents, 800 negative - 1600 positive images give generally good results.

    2- Labeling

    For your each positive image, you should create label file like data.lst. This list file's each line should consist:

    This step is a painful step. To handle it opencv_createsamples create that list file for you. This is not efficient comparing to prepare by yourself but this also works fine. It simply uses negative images for background and copy your positive image(s) to that backgrounds randomly. Then creates a list file.

    You can run opencv_createsample command via on terminal:

    opencv_createsamples -img your_image.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1800

    In above command you give and get:

    3- Creating Vector file

    In this step again by using, opencv_createsample command we get the vector file of our positive images. Here is example of command:

    opencv_createsamples -info info/info.lst -num 1800 -w 50 -h 50 -vec positives.vec

    You just give your prepared list file,width-height and desired vector file name. You can learn more about width and height part from documents.

    4- Training

    This step is the last step. opencv_traincascade command is used lastly. We give as input:

    Here is the sample command:

    opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1800 -numNeg 900 -numStages 10 -w 50 -h 50

    At the end we get an xml file.

    Note-1: OpenCV 3.4... versions have a problem about the opencv_createsamples, when I searched people are saying there is a bug. I recommend you to use a different version.

    Note-2: Last step is the longest step taking time(if you don't make labeling by yourself). I tried even in some bad computers and took 4 hours to finish(for the command values in above command)