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

Running samples


Running a few sample scripts is a good way to test that OpenCV is correctly set up. The samples are included in OpenCV's source code archive.

On Windows, we should have already downloaded and unzipped OpenCV's self-extracting ZIP. Find the samples in <unzip_destination>/opencv/samples.

On Unix-like systems, including Mac, download the source code archive from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download and unzip it to any location (if we have not already done so). Find the samples in <unzip_destination>/OpenCV-2.4.3/samples.

Some of the sample scripts require command-line arguments. However, the following scripts (among others) should work without any arguments:

  • python/camera.py: This displays a webcam feed (assuming a webcam is plugged in).

  • python/drawing.py: This draws a series of shapes, like a screensaver.

  • python2/hist.py: This displays a photo. Press A, B, C, D, or E to see variations of the photo, along with a corresponding histogram of color or grayscale values.

  • python2/opt_flow.py (missing from the Ubuntu package): This displays a webcam feed with a superimposed visualization of optical flow (direction of motion). For example, slowly wave your hand at the webcam to see the effect. Press 1 or 2 for alternative visualizations.

To exit a script, press Esc (not the window's close button).

If we encounter the message, ImportError: No module named cv2.cv, then we are running the script from a Python installation that does not know anything about OpenCV. There are two possible explanations:

  • Some steps in the OpenCV installation might have failed or been missed. Go back and review the steps.

  • If we have multiple Python installations on the machine, we might be using the wrong Python to launch the script. For example, on Mac, it might be the case that OpenCV is installed for MacPorts Python but we are running the script with the system's Python. Go back and review the installation steps about editing the system path. Also, try launching the script manually from the command line using commands such as:

    $ python python/camera.py
    

    You can also use the following command:

    $ python2.7 python/camera.py
    

    As another possible means of selecting a different Python installation, try editing the sample script to remove #! lines. These lines might explicitly associate the script with the wrong Python installation (for our particular setup).