Hereafter, we will refer to the two executables as <opencv_createsamples>
and <opencv_traincascade>
. Remember to substitute the path and filename that are appropriate to your system and setup.
These executables have certain data files as inputs and outputs. Following is a typical approach to generating these data files:
<negative_description>
.<positive_description>
.<opencv_createsamples>
with <negative_description>
and <positive_description>
as arguments. The executable creates a binary file describing the training data. We will refer to the latter file as <binary_description>
.<opencv_traincascade>
with <binary_description>
as an argument. The executable creates the binary cascade file, which we will refer to as <cascade>
.The actual names and paths of <negative_description>
, <positive_description>
, <binary_description>
, and <cascade>
may be anything we choose.
Now, let's look at each of the three steps in detail.
<negative_description>
is a text file listing the relative paths to all negative training images. The paths should be separated by line breaks. For example, suppose we have the following directory structure, where <negative_description>
is negative/desc.txt
:
negative desc.txt images negative 0.png negative 1.png
Then, the contents of negative/desc.txt
could be as follows:
"images/negative 0.png" "images/negative 1.png"
For a small number of images, we can write such a file by hand. For a large number of images, we should instead use the command line to find relative paths matching a certain pattern and to output these matches to a file. Continuing our example, we could generate negative/desc.txt
by running the following commands on Windows in Command Prompt:
> cd negative > forfiles /m images*.png /c "cmd /c echo @relpath" > desc.txt
Note that in this case, relative paths are formatted as .images
egative 0.png
, which is acceptable.
Alternatively, in a Unix-like shell, such as Terminal on Mac or Ubuntu, we could run the following commands:
$ cd negative $ find images/*.png | sed -e "s/^/"/g;s/$/"/g" > desc.txt
<positive_description>
is needed if we have more than one positive training image. Otherwise, proceed to the next section. <positive_description>
is a text file listing the relative paths to all positive training images. After each path, <positive_description>
also contains a series of numbers indicating how many instances of the target are found in the image and which sub-rectangles contain those instances of the target. For each sub-rectangle, the numbers are in this order: x, y, width, and height. Consider the following example:
"images/positive 0.png" 1 120 160 40 40 "images/positive 1.png" 2 200 120 40 60 80 60 20 20
Here, images/positive 0.png
contains one instance of the target in a sub-rectangle whose upper-left corner is at (120, 160) and whose lower-right corner is at (160, 200). Meanwhile, images/positive 1.png
contains two instances of the target. One instance is in a sub-rectangle whose upper-left corner is at (200, 120) and whose lower-right corner is at (240, 180). The other instance is in a sub-rectangle whose upper-left corner is at (80, 60) and whose lower-right corner is at (100, 80).
To create such a file, we can start by generating the list of image paths in the same manner as for <negative_description>
. Then, we must manually add data about target instances based on an expert (human) analysis of the images.
Assuming we have multiple positive training images and, thus, we created <positive_description>
, we can now generate <binary_description>
by running the following command:
$ <opencv_createsamples> -vec <binary_description> -info <positive_description> -bg <negative_description>
Alternatively, if we have a single positive training image, which we will refer to as <positive_image>
, we should run the following command instead:
$ <opencv_createsamples> -vec <binary_description> -image <positive_image> -bg <negative_description>
For other (optional) flags of <opencv_createsamples>
, see the official documentation at http://docs.opencv.org/doc/user_guide/ug_traincascade.html.
Finally, we can generate <cascade>
by running the following
command:
$ <opencv_traincascade> -data <cascade> -vec <binary_description> -bg <negative_description>
For other (optional) flags of <opencv_traincascade>
, see the official documentation at http://docs.opencv.org/doc/user_guide/ug_traincascade.html.