easy_install pyobjc==2.2 brew install pil brew install opencv
python facedetect-example.py
have fun
| #!/usr/bin/env /usr/local/bin/python | |
| import glob, os, sys, uuid | |
| import cv | |
| from Foundation import * | |
| from objc import * | |
| from AppKit import * | |
| from PIL import * | |
| class FaceDetect(): | |
| def __init__(self): | |
| NSApplication.sharedApplication().activateIgnoringOtherApps_(True) | |
| cv.NamedWindow ("FaceDetect", 1) | |
| self.capture = cv.CaptureFromCAM( 0 ) | |
| self.writer = None | |
| def detect(self): | |
| faces = [] | |
| cv.CvtColor(self.frame, self.grayscale, cv.CV_RGB2GRAY) | |
| #equalize histogram | |
| cv.EqualizeHist(self.grayscale, self.grayscale) | |
| # detect objects | |
| for cascade in self.cascades: | |
| faces.extend( | |
| cv.HaarDetectObjects( | |
| image=self.frame, | |
| cascade=cascade, | |
| storage=self.storage, | |
| scale_factor=1.2, | |
| min_neighbors=2, | |
| flags=cv.CV_HAAR_DO_CANNY_PRUNING) | |
| ) | |
| for (x,y,w,h),n in faces: | |
| if n > 10: | |
| cv.Circle(self.frame, (int(2*x+w)/2,int(2*y+h)/2), int(w+h+h*0.5)/4, (128, 255, 128)) | |
| cv.Rectangle(self.frame, (x,y), (x+w,y+h), 255) | |
| def procKey(self, k): | |
| if k == 0x1b: # ESC | |
| print 'ESC pressed. Exiting ...' | |
| sys.exit(1) | |
| return False | |
| elif k == ord('s'): | |
| currentFrame = cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_MSEC ) | |
| cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_MSEC , abs(currentFrame+1000) ) | |
| elif k == ord('b'): | |
| currentFrame = cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_MSEC ) | |
| cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_POS_MSEC , abs(currentFrame-1000) ) | |
| elif k == ord('o'): | |
| op = NSOpenPanel.openPanel() | |
| op.runModalForTypes_(None) | |
| if len(op.filenames()): | |
| self.capture = cv.CreateFileCapture(op.filenames()[0]) | |
| self.writer = cv.CreateVideoWriter( | |
| str(uuid.uuid1())+".avi", | |
| cv.CV_FOURCC('D','I','V','X'), | |
| cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS), | |
| (int(cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH)), int(cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT))), | |
| True) | |
| op.setReleasedWhenClosed_(True) | |
| op.close() | |
| elif k >= ord('0') and k <= ord('9'): | |
| self.capture = cv.CaptureFromCAM( k-48 ) | |
| self.writer = None | |
| def run(self): | |
| # check if capture device is OK | |
| if not self.capture: | |
| print "Error opening capture device" | |
| sys.exit(1) | |
| if self.capture: | |
| self.frame = cv.QueryFrame(self.capture) | |
| if self.frame: | |
| self.image_size = cv.GetSize(self.frame) | |
| if self.writer: | |
| cv.WriteFrame(self.writer, self.frame) | |
| # create grayscale version | |
| self.grayscale = cv.CreateImage(self.image_size, 8, 1) | |
| # create storage | |
| self.storage = cv.CreateMemStorage(1024) | |
| self.cascades = [] | |
| self.cascades.append(cv.Load('haarcascade_frontalface_alt2.xml')) | |
| while 1: | |
| # do forever | |
| if self.capture: | |
| self.frame = cv.QueryFrame(self.capture) | |
| if self.frame: | |
| self.image_size = cv.GetSize(self.frame) | |
| if self.image_size: | |
| self.grayscale = cv.CreateImage(self.image_size, 8, 1) | |
| if self.frame is None: | |
| break | |
| # face detection | |
| self.detect() | |
| if self.writer: | |
| cv.WriteFrame(self.writer, self.frame) | |
| # display webcam image | |
| if self.frame: | |
| cv.ShowImage('FaceDetect', self.frame) | |
| # handle events | |
| k = cv.WaitKey(10) | |
| if self.procKey(k) == False: | |
| break | |
| if __name__ == "__main__": | |
| print "Press ESC to exit ..." | |
| face_detect = FaceDetect() | |
| face_detect.run() |