def detect(self, image): size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create and clear storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect faces faces = cv.HaarDetectObjects(grayscale, self.face_cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, self.face_size) if faces: # faces detected for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) detected = True is_face = True else: # detect body bodies = cv.HaarDetectObjects(grayscale, self.body_cascade, storage, 1.1, 3, 0, self.body_size) if bodies: # body detected for i in bodies: cv.Rectangle( image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) detected = True is_face = False else: detected = False is_face = False # release resources we don't need any more cv.ReleaseImage(grayscale) cv.ReleaseMemStorage(storage) return (detected, is_face)
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0)
FACE_UD_STATE_CHANGE_THRESH = 1 FACE_ALTERNATION_THRESH = 2 FACE_ONE_DIMENSION_THRESH = 2 FACE_STILL_THRESHOLD = 3 FACE_ALTERNATIONS_EXPIRE = 6 #Face movement enumeration OTHER = 0 STILL = 1 LEFT = 2 RIGHT = 3 UP = 4 DOWN = 5 #Color donstant definitions RED = cv.RGB(255,0,0) GREEN = cv.RGB (0,220,0) BLUE = cv.RGB (0,0,255) YELLOW = cv.RGB(255,255,0); ORANGE = cv.RGB(255,127,0); MAGENTA = cv.RGB(255,0,255); # other constants scale = 1 cascade = None storage = cv.CreateMemStorage(0) cascade_name = "xml/haarcascade_frontalface_alt.xml" min_size = cv.Size(FACE_MIN_SIZE,FACE_MIN_SIZE) image_scale = 1.3 haar_scale = 1.2 min_neighbors = 2
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, 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