Training the Robomow rs612 Haar Cascade Object Detector
I created a separate repo that stores all the videos, positive, negative images, scripts to help create positive and negative images, scripts to train the model and build the xml files. Lets take a look at it.
The directory structure breaks down as follows
- videos - contains the videos used to train the different cascade models
- train - contains the scripts, images, used to train, generate, and test the cascade models. Each Object has its own directory. In this case we have only trained the rs612 mower. To train one of the other mowers. Simply copy the rs612 directory and upload new videos and repeat the same process. All the scripts are in place and should work out of the box. Lets look at the training of rs612 in more detail.
- The directory in the rs612 directory contains the work "run" and represents a training session that will yield a trained cascade model. -
- neg - contains the negative images. Typically the background
- pos - contains the positive images. The images of the robomow
- model - the xml files used by opencv to execute the trained cascade.
- The scripts
- annotate-pos-images.sh - will go to the pos directory and run the opencv_annotation tool to display all the positive images so that you can draw a box around the object you are training. This tool is tricky. I have given you instructions and tips on how to use this tool in the article Using opencv_annotation program to annotate positive images. Annotate is simply recording the coordinates of a rectangle that encompases the object you are trying to identify on a positive image to a file.
- create-vec-file.sh - calls the opencv_createsamples program to coalesce all of the positive images into a single file called rs612.vec. It uses the annotations file generated in the previous step to locate each positive image and the bounding rectangular coordinates of the object we are trying to detect in the image and combines all of the images into this single file. Here is where you can specify the number of positive images you would like to have included in the vec file. You can preview all of the positiv images in the vec file by calling view-vec-file.sh to make sure the vec file looks reasonable.
- train-cascade.sh - calls opencv_traincascade program with the appropriate arguments to include the background/negative images your vec file and the number of cascade/stages to generate. The results will be stored in the model directory as xml files. Here is a link to that describes how the opencv_traincascade program works.
- detect-robomow.py - python script to test out your new trained cascade model. It accepts a video file as input and will execute the model you trained and draw a bounding box around your object once the cascade model detects your object. Finally the results of our hard work!
Lets check out the results after I did several training runs for the robomow rs612.