Skip to main content

Posts

Showing posts with the label pycam

Bring your hat!

So thought I would make a really simple example of how pygame can be used with a webcam. This example uses opencv to detect a face, then pygame to draw a "hat". #!/usr/bin/python from pycam import VideoCapturePlayer from pycam import pygameFaceDetect import pygame from pygame.locals import * def process(surf): faces = pygameFaceDetect.getFaces(surf) if faces: s = pygameFaceDetect.faceDetect.image_scale for face in faces: pointsInHat = [ (face.x*s, face.y*s), (face.x*s + face.width*s, face.y*s), (face.x*s + face.width*s/2, face.y*s - face.height*s/2 ) ] pygame.draw.polygon(surf, Color("red"), pointsInHat) pygame.draw.polygon(surf, Color("black"), pointsInHat, 10) return surf if __name__ == "__main__": vcp = VideoCapturePlayer(processFunction=process) vcp.main() pygame.quit() And the obligatory screen shot: ...

Greenscreen

Something used heaps in the film industry is the " Greenscree n" I thought I would take a quick look at how to make a greenscreen that works fast enough to run on a live webcam stream. And infact one that works with any coloured background. It has many many limitations, but was a fun experiment! To run this example you will need OpenCV with the SWIG Python bindings installed. You can get this code from my SVN repository here . Firstly the background I started with: Adding an object to the scene, and carrying out back ground subtraction: So anyhow the code: #!/usr/bin/env python from VideoCapturePlayer import VideoCapturePlayer as VCP from opencv import cv def threshold_image ( image , n = []): """Record the first 5 images to get a background, then diff current frame with the last saved frame. """ if len ( n ) < 5 : # n[4] will be our background # First capture a few images ...

Windows + Pygame + VidCap

So I thought I would try out the pycam module on windows. First of all it seems windows comes with nothing, not even python. So after installing python I still needed this: http://videocapture.sourceforge.net/ and this: http://pygame.org/ftp/pygame-1.9.1.win32-py2.5.msi Vid cap itself is so easy: from VideoCapture import Device cam = Device() cam.saveSnapshot('image.jpg') To get just the most basic stuff going, for some reason the VidCap module wouldn't let me open a camera unless the size was set at (160, 120) and the Camera class seemed to change going to windows... anyhow if you want to play with this grab it from the repo here . This is using the pygame edge detect, just tried the scipy one - appears to be a problem mapping surface to numpy array... from memory there was a hack to get around something so maybe thats no longer required. The "greenscreen" example and the simple threshold. Couldn't immediatly see how to get the python wrappings for swig going...

Camera Histogram Demo

There is a really cool demo for opencv in C called camshiftdemo. I decided to write a version in python... The only one I could find online used opencv with ctypes , so this was a simple enough modification to get it running in more pure "swigged" python. It's in my pycam repository if anyone is interested:  http://code.google.com/p/pycam/source/browse/trunk/pycam/cam-histo.py Here are some screenshots. This is the python cam shift demo tracking the orange on the XO's logo. Now tracking the green section, you can see the histogram for the entire image shown. It is a really simple demo, and quite fun! At the Hitlab open night this was what I used to keep the kids occupied! Unlike the previous computer vision post - this doesn't use pygame at all. It would be a really good example for the XO pippy computer vision package idea however.