Created
December 9, 2010 21:09
-
-
Save jsnyder/735343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import cv | |
from opencv import highgui | |
def detect(image): | |
image_size = cv.GetSize(image) | |
# create grayscale version | |
grayscale = cv.CreateImage(image_size, 8, 1) | |
cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) | |
# create storage | |
storage = cv.CreateMemStorage(0) | |
#cv.ClearMemStorage(storage) | |
# equalize histogram | |
cv.EqualizeHist(grayscale, grayscale) | |
# detect objects | |
cascade = cv.Load('/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_alt.xml') | |
faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50, 50)) | |
if faces: | |
print 'face detected!' | |
for (x,y,w,h),n in faces: | |
cv.Rectangle(image, ( x, y), | |
(x + w, y + h), | |
cv.RGB(0, 255, 0), 3, 8, 0) | |
if __name__ == "__main__": | |
print "Press ESC to exit ..." | |
# create windows | |
highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) | |
# create capture device | |
device = 0 # assume we want first device | |
capture = cv.CreateCameraCapture(0) | |
#highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_WIDTH, 640) | |
#highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 480) | |
# check if capture device is OK | |
if not capture: | |
print "Error opening capture device" | |
sys.exit(1) | |
while 1: | |
# do forever | |
# capture the current frame | |
frame = cv.QueryFrame(capture) | |
if frame is None: | |
break | |
# mirror | |
cv.Flip(frame, None, 1) | |
# face detection | |
detect(frame) | |
# display webcam image | |
cv.ShowImage('Camera', frame) | |
# handle events | |
k = cv.WaitKey(10) | |
if k == 0x1b: # ESC | |
print 'ESC pressed. Exiting ...' | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment