topamax once a day

OpenCV + Python

Here are some of the more interesting OpenCV demos using the Python wrapper.  The installation process on Ubuntu is covered in my previous post, OpenCV + Ubuntu. The demo scripts are located in samples/python within the OpenCV release.

The cam shift sample below demonstrates the color/object detection capability of OpenCV. To set the object/color to track, click and drag a box on the video using the mouse.  The histogram window will display the target for OpenCV to locate and track within the field of view.  The more distinct the color from the background of the scene, the better it works.  The trailing 0 at the end means to use camera input device 0.

python ./camshift.py 0

To run the face detection demo, you will need to use the -c command line option to specify the classifier to use. Different classifiers detect using different algorithms and so are better suited to certain detection condition, angles, etc. compared to others.  The classifier used in the demo works best with faces directly facing the camera, and doesn’t do as well with tilted, rotated, or otherwise not direct faces.  This is why some of the  titled faces in the Google image search part of the video fail to be recognized by OpenCV.

python ./facedetect.py -c /usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml 0

If you don’t have a camera connected, and still want to verify that the Python wrapper is working correctly, you can run the previous examples  using an image or video file, otherwise try out the following examples (also in samples/python):

python ./minarea.py

python ./delaunay.py

python ./dmtx.py

python ./drawing.py

  • Great tutorial. I also recently started a blog on OpenCV in Python. Please check it out and let me know your opinion please.

    • Nice work Shambool, keep up the great work!

  • Why do you suggest building OpenCV 2.3.1 from source for this? Can’t I just install the package libopencv-dev (http://packages.ubuntu.com/precise/libopencv-dev) ?

    • Hi eljondo, depending on how you want to use OpenCV, the pre-built packages may or may not be sufficient. I wanted OpenCV to work with ffmpeg, x264, and OpenNI/PrimeSense, so needed to manually compile for this support.

  • Scott

    This is EXACTLY what I’ve been looking for to get a project started, thanks. One question though, do you know of a way to increase the window size of the video feed? I have an HD camera connected but the window is tiny, maybe 200×200 so selecting the colors to track isn’t very precise right now.

    • Scott

      I got it sorted:

      Where the example uses: self.capture = cv.CaptureFromCAM(0)
      I had to change it to: self.capture = cv.CreateCameraCapture(0)
      and add:

      cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_WIDTH,1280)
      cv.SetCaptureProperty(self.capture,cv.CV_CAP_PROP_FRAME_HEIGHT,720)

  • i am new to OpenCV from where i can start. just give me guide. like books tutorials etc… from scratch to pro 🙂

  • Hey Michael, I wrote a script in python using opencv for the same purpose but that doesn’t works as good as this ! .. I want to make some alteration in the code .. your script can be very helpful ! if u can please share your code 🙂

  • Tom

    Wow! I’ve just found out about opencv-python and am very excited. I’ve been playing around with a few examples running linux mint and opencv 2.4.8. Most of it looks amazing, but a few of the samples are not allowing me to draw selections over the video (specifically like the camshift example). The sample from /samples/python is working, but not /samples/python2

    All the dependencies seem to be satisfied, and no errors are thrown, but its not working. Im out of ideas…anyo

  • Pingback: Windows 용 Python-OpenCV 설치 및 얼굴인식 테스트 | Hard Copy Arduino()