Over the last several months, I have shown you all kinds of cool ways you can use OpenCV with C# to create some really cool applications. Today, at the request of a reader, I want to begin showing you how to create applications using OpenCV and Python. If you’ve ever taken a look at the OpenCV documentation with Python, you will know that it’s lacking in many ways. So, I want to take a few minutes and give you a crash course with getting OpenCV and Python to play nicely. Let’s begin.
Before working with OpenCV and Python, you have an extra Python library you’ll need to download and install if you don’t have it already. I’m talking about “Numerical Python” or “NumPy” as it’s more commonly known. If you already have NumPy installed, make sure you are running at least version 1.6 as that is what our version of OpenCV for Python was compiled against. If you
don’t already have NumPy, open a browser and head over to http://sourceforge.net/projects/numpy/files/NumPy/1.6.1rc3/. There you will find a list of downloads for numpy-1.6. Since I’m running on 32bit Windows and using Python 2.7, I will be downloading numpy-1.6.1rc3-win32-superpack-python2.7.exe. Once you have downloaded NumPy, double-click the installer and follow the instruction screens. Note: You can usually keep clicking the Next button to fly right thru the installer.
After you have installed NumPy, you are ready to begin playing with OpenCV and Python. If you don’t already have OpenCV installed, you can find links to download it from my new website http://www.learncomputervision.com/resources/. Again, since we’re working with Windows, we will click the Windows link under OpenCV at the “Learn Computer Vision” website and that take us to a page on SourceForge where we can click to download OpenCV. However, if you do not want to download and install OpenCV at this time, it’s not required for this example to work. Besides, the current version of OpenCV is 214MB. So, it will take some time to download. If you choose not to install OpenCV at this time, make sure you download the zipped example at the end of this article as it contains the required OpenCV binary for Python.
Now that you have everything you need to use OpenCV with Python, it’s time to write some code. For this example, we are going to keep it very simple by demonstrating how to capture video from a standard webcam and display it in a window. To do that, we begin by adding a reference to the OpenCV binary that’s included with the zipped example at the end of this article.
import cv2.cv as cv
Next, we need to setup our capture device by calling the “CaptureFromCAM” function in our cv2 library. The parameter passed is the index for the camera you will be capturing video from. This is helpful when you have multiple cameras connected to the same computer you’ll be running your application from. In my case, I only have 1 camera attached. So, I simply pass 0 (zero) as my parameter.
cap = cv.CaptureFromCAM(0)
The next thing we need to do is to tell OpenCV that we want a window created. For that, we will call the “NamedWindow” function and pass it a reference name and some flags that tell our window how to behave. As you can see below, I’m passing 1 (one) as my second parameter which tells my window to act as a normal window with normal behavior and size.
Now you’re ready to begin capturing video from your camera and display it in the window you just created. For that, you will need to create an endless loop. Why an endless loop? Well, in order for OpenCV to have the opportunity to process your video feed, each frame of the video needs to be captured and passed one frame at a time. Most readers think that OpenCV processes video in its entirety. But, like I said, it doesn’t work that way. Video processing is done one frame at a time. That’s why we need our endless loop.
However, what happens when you want the app to stop capturing frames from your video and exit? For that, we will tell OpenCV to listen for the Escape key to be pressed. Once the Escape key has been detected, OpenCV will stop capturing your video and will exit from the endless while-loop. Once you’re outside of the while-loop, you will need to tell OpenCV to close all windows that were created within the scope of your app.
Here is the complete code that makes all of this possible.
import cv2.cv as cv cap = cv.CaptureFromCAM(0) cv.NamedWindow("Camera", 1) while True: img = cv.QueryFrame(cap) cv.ShowImage("Camera", img) if cv.WaitKey(10) == 27: break cv.DestroyAllWindows()
You can download this example, including the compiled OpenCV binary from here.
PayPal will open in a new tab.