Skip to content

Instantly share code, notes, and snippets.

@mrodem
Created November 30, 2011 20:17
Show Gist options
  • Save mrodem/1410628 to your computer and use it in GitHub Desktop.
Save mrodem/1410628 to your computer and use it in GitHub Desktop.
python-opencv-mrodem
import opencv
#this is important for capturing/displaying images
from opencv import highgui
import pygame
import sys
video = highgui.cvCreateFileCapture("/home/magrode/out.avi")
def get_image():
im = highgui.cvQueryFrame(video)
if im == None:
return None
# Add the line below if you need it (Ubuntu 8.04+)
im = opencv.cvGetMat(im)
return im
#convert Ipl image to PIL image
#return opencv.adaptors.Ipl2PIL(im)
# The detection routine:
def detect(image):
# Find out how large the file is, as the underlying C-based code
# needs to allocate memory in the following steps
image_size = opencv.cvGetSize(image)
# create grayscale version - this is also the point where the allegation about
# facial recognition being racist might be most true. A caucasian face would have more
# definition on a webcam image than an African face when greyscaled.
# I would suggest that adding in a routine to overlay edge-detection enhancements may
# help, but you would also need to do this to the training images as well.
grayscale = opencv.cvCreateImage(image_size, 8, 1)
opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY)
# create storage (It is C-based so you need to do this sort of thing)
storage = opencv.cvCreateMemStorage(0)
opencv.cvClearMemStorage(storage)
# equalize histogram
opencv.cvEqualizeHist(grayscale, grayscale)
# detect objects - Haar cascade step
# In this case, the code uses a frontal_face cascade - trained to spot faces that look directly
# at the camera. In reality, I found that no bearded or hairy person must have been in the training
# set of images, as the detection routine turned out to be beardist as well as a little racist!
cascade = opencv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', opencv.cvSize(1,1))
faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50))
if faces:
for face in faces:
# Hmm should I do a min-size check?
# Draw a Chartreuse rectangle around the face - Chartruese rocks
opencv.cvRectangle(image, opencv.cvPoint( int(face.x), int(face.y)),
opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)),
opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
fps = 30.0
pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("Demo")
screen = pygame.display.get_surface()
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
sys.exit(0)
im = get_image()
if im != None:
detect(im)
im = opencv.adaptors.Ipl2PIL(im)
pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
screen.blit(pg_img, (0,0))
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment