Book Image

OpenCV Computer Vision with Python

By : Joseph Howse
Book Image

OpenCV Computer Vision with Python

By: Joseph Howse

Overview of this book

<p>OpenCV Computer Vision with Python shows you how to use the Python bindings for OpenCV. By following clear and concise examples, you will develop a computer vision application that tracks faces in live video and applies special effects to them. If you have always wanted to learn which version of these bindings to use, how to integrate with cross-platform Kinect drivers, and how to efficiently process image data with NumPy and SciPy, then this book is for you.</p> <p>This book has practical, project-based tutorials for Python developers and hobbyists who want to get started with computer vision with OpenCV and Python. It is a hands-on guide that covers the fundamental tasks of computer vision, capturing, filtering, and analyzing images, with step-by-step instructions for writing both an application and reusable library classes.</p>
Table of Contents (14 chapters)
OpenCV Computer Vision with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the training sets and cascade


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:

  1. Manually create a text file that describes the set of negative training images. We will refer to this file as <negative_description>.

  2. Manually create a text file that describes the set of positive training images. We will refer to this file as <positive_description>.

  3. Run <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>.

  4. Run <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.

Creating <negative_description>

<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\negative 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

Creating <positive_description>

<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.

Creating <binary_description> by running <opencv_createsamples>

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.

Creating <cascade> by running <opencv_traincascade>

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.

Note

Vocalizations

For good luck, make an imitative sound when running <opencv_traincascade>. For example, say "Moo!" if the positive training images are cows.