This is the code repository for OpenCV 4 for Secret Agents - Second Edition, published by Packt.
Use OpenCV 4 in secret projects to classify cats, reveal the unseen, and react to rogue drivers.
OpenCV 4 is a grand collection of image processing functions and computer vision algorithms. It is open source, it supports many programming languages and platforms, and it is fast enough for many real-time applications. What a lot of gadgets we can build with such a handy library! OpenCV 4 for Secret Agents is a broad selection of projects based on computer vision, machine learning, and several application frameworks. To target diverse desktop systems and Raspberry Pi, the book supports multiple Python versions (from 2.7 to 3.7). To target Android, the book supports Java in Android Studio, as well as C# in the Unity game engine.
This book covers the following exciting features:
- Detect motion and recognize gestures to control a smartphone game
- Detect car headlights and estimate distances to them
- Detect and recognize human and cat faces to trigger an alarm
- Amplify motion in real-time video to show heartbeats and breaths
- Make a physics simulation that detects shapes in a real-world drawing
- Build OpenCV 4 projects in Python 3 for desktops and Raspberry Pi
- Build OpenCV 4 Android applications in Android Studio and Unity
If you feel this book is for you, get your copy today!
If you are an experienced software developer who is new to computer vision or machine learning, and wants to study these topics through creative projects, then this book is for you. The book will also help existing OpenCV users who want upgrade their projects to OpenCV 4 and new versions of other libraries, languages, tools, and operating systems. General familiarity with object-oriented programming, application development, and usage of operating systems (OS), developer tools, and the command line is required.
The code is organized into folders, such as Chapter002
, each corresponding to a chapter in the book. The following list shows the software dependencies for each chapter's code.
Chapter | Software required | OS required |
---|---|---|
2 | OpenCV or 4.x*, Python 2.7 or 3.x, NumPy, SciPy, Requests, WxPython, PyInstaller (optional) | Windows, Mac OS X, or Linux (Any) |
3 | OpenCV or 4.x* (plus optional cascade training tools from OpenCV 3.4), Python 2.7 or 3.x, NumPy, WxPython, PyInstaller (optional) | Windows, Mac OS X, or Linux (Any) |
4 | Android Studio, OpenCV 4.x* for Android | Windows, Mac OS X, and Linux (Any) |
5 | OpenCV 4.x*, Python 2.7 or 3.x, NumPy, WxPython | Windows, Mac OS X, and Linux (Any) |
6 | Unity, OpenCV 4.x* for Unity | Windows, Mac OS X, and Linux (Any) |
7 | OpenCV 4.x*, Python 2.7 or 3.x, NumPy, SciPy, WxPython, PyFFTW | Windows, Mac OS X, and Linux (Any) |
8 | OpenCV 4.x*, Python 2.7 or 3.x, NumPy, SciPy, WxPython, PyFFTW, Spinnaker SDK (optional) plus PySpin (optional) | Windows, Mac OS X, or Linux (Any) |
* Most of the book's code should also work with OpenCV 3.4.
The data-description script at Chapter003/cascade_training/describe.py
depends on datasets that are not part of this repository. Moreeover, the cascade-training script at Chapter003/cascade_training/train.bat
(for Windows) or Chapter003/cascade_training/train.sh
(for Mac or Linux) depends on tools that are not yet part of OpenCV 4.
Several third-party datasets provide us with sample images of cats and other subjects. We use them as positive and negative training sets for a cat-face detector. The datasets are large downloads. They can be obtained in any of the following ways:
- On Windows, download and unzip the datasets manually, as described in the following steps:
- Download
https://archive.org/download/CAT_DATASET/CAT_DATASET_01.zip
. Unzip it asChapter003\cascade_training\CAT_DATASET_01
. - Download
https://archive.org/download/CAT_DATASET/CAT_DATASET_01.zip
. Unzip it asChapter003\cascade_training\CAT_DATASET_02
. - Download
http://www.vision.caltech.edu/Image_Datasets/faces/faces.tar
. Unzip it asChapter003\cascade_training\faces
. - Download
http://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
. Unzip it. From the unzipped contents, move theVOC2007
folder so that it becomesChapter003\cascade_training\VOC2007
. - Download
https://github.com/sonots/tutorial-haartraining/archive/master.zip
. Unzip it. From the unzipped contents, move thedata\negatives
folder so that it becomesChapter003\cascade_training\urtho_negatives
.
- Download
- On Mac or Linux, go to the
Chapter003/cascade_training
directory and rundownload_datasets.sh
. This script depends on thewget
andsvn
command line tools (which you can install from a package manager, if you do not already have them), and it must be run fromChapter003/cascade_training
as the working directory. It downloads the datasets and extracts all the datasets into subfolders wheredescribe.py
expects to find them.
OpenCV 3 contains tools for training Haar and LBP cascades. These tools are command-line executables named opencv_createsamples.exe
and opencv_traincascades.exe
(on Windows), or opencv_createsamples
and opencv_traincascades
(on Mac or Linux). The source code for these tools has not yet (as of OpenCV 4.1.0) been updated to become part of OpenCV 4. Fortunately, though, the OpenCV 3 tools produce cascades that are forward-compatible with OpenCV 4. For the timebeing, the recommended way to train cascades for OpenCV 4 is to use the tools from an OpenCV 3 build. These tools can be obtained in any of the following ways:
- For Windows, find the tools in this repository under the
OpenCVTools\Windows/x64
folder (for 64-bit Windows) orOpenCVTools/Windows\x86
(for 32-bit Windows). Append the relevant folder's absolute path to the system'sPath
variable so that our cascade-training script can find the tools. - For Mac, install OpenCV 3 with MacPorts. The tools will be installed to
/opt/local/bin
, which should already be in the system'sPATH
variable. - For Mac, install OpenCV 3 with Homebrew. The tools will be installed to
/opt/local/bin
oropt/local/sbin
, which should already be in the system'sPATH
variable. - For Linux, install OpenCV 3 with your system's package manager. The tools will be installed to
/usr/bin
or/usr/local/bin
, which should already be in the system'sPATH
variable. - For any system, build OpenCV 3 from source, find the tools among the built binary files, and add their folder to the system's
Path
variable (on Windows) orPATH
variable (on Mac or Linux).
- Icons by Tatyana Suhodolska (www.artdesigncat.com)
- Images from various sources, to train and test classifiers
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.
- Learn OpenCV 4 By Building Projects - Second Edition [Packt] [Amazon]
- Mastering OpenCV 4 - Third Edition [Packt] [Amazon]
Joseph Howse lives in a Canadian fishing village with four cats; the cats like fish, but they prefer chicken.
Joseph provides computer vision expertise through his company, Nummist Media. His books include OpenCV 4 for Secret Agents, OpenCV 3 Blueprints, Android Application Programming with OpenCV 3, iOS Application Development with OpenCV 3, Learning OpenCV 3 Computer Vision with Python, and Python Game Programming by Example, published by Packt.
Joseph Howse is the author or co-author of the following books on OpenCV 3:
- Learning OpenCV 3 Computer Vision with Python
- Android Application Programming with OpenCV 3
- iOS Application Development with OpenCV 3
- OpenCV 3 Blueprints
He is also the author or co-author of the following books on OpenCV 2:
- OpenCV Computer Vision with Python
- Android Application Programming with OpenCV
- OpenCV for Secret Agents
- Python Game Programming By Example
Click here if you have any feedback or suggestions.