Package imagizer :: Module faces |
[hide private]
[frames] | no frames]

# Source Code for Module imagizer.faces

     1  import sys
     2  #from CVtypes import cv
     3  import cv
     4
     5 -def detect(image):
     6      image_size = cv.GetSize(image)
     7
     8      # create grayscale version
     9      grayscale = cv.CreateImage(image_size, 8, 1)
    10      cv.CvtColor(image, grayscale, cv.BGR2GRAY)
    11
    12      # create storage
    13      storage = cv.CreateMemStorage(0)
    14      cv.ClearMemStorage(storage)
    15
    16      # equalize histogram
    17      cv.EqualizeHist(grayscale, grayscale)
    18
    19      # detect objects
    20      cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1))
    21      faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50))
    22
    23      if faces:
    24          print 'face detected!'
    25          for i in faces:
    26              cv.Rectangle(image, cv.Point(int(i.x), int(i.y)),
    27                           cv.Point(int(i.x + i.width), int(i.y + i.height)),
    28                           cv.RGB(0, 255, 0), 3, 8, 0)
    29
    30  if __name__ == "__main__":
    31      print "OpenCV version: %s (%d, %d, %d)" % (cv.VERSION,
    32                                                 cv.MAJOR_VERSION,
    33                                                 cv.MINOR_VERSION,
    34                                                 cv.SUBMINOR_VERSION)
    35
    36      print "Press ESC to exit ..."
    37
    38      # create windows
    39      cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE)
    40
    41      # create capture device
    42      device = 0 # assume we want first device
    43      capture = cv.CreateCameraCapture(0)
    44      cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640)
    45      cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480)
    46
    47      # check if capture device is OK
    48      if not capture:
    49          print "Error opening capture device"
    50          sys.exit(1)
    51
    52      while 1:
    53          # do forever
    54
    55          # capture the current frame
    56          frame = cv.QueryFrame(capture)
    57          if frame is None:
    58              break
    59
    60          # mirror
    61          cv.Flip(frame, None, 1)
    62
    63          # face detection
    64          detect(frame)
    65
    66          # display webcam image
    67          cv.ShowImage('Camera', frame)
    68
    69          # handle events
    70          k = cv.WaitKey(10)
    71
    72          if k == 0x1b: # ESC
    73              print 'ESC pressed. Exiting ...'
    74              break
    75