1 import sys
2
3 import cv
4
6 image_size = cv.GetSize(image)
7
8
9 grayscale = cv.CreateImage(image_size, 8, 1)
10 cv.CvtColor(image, grayscale, cv.BGR2GRAY)
11
12
13 storage = cv.CreateMemStorage(0)
14 cv.ClearMemStorage(storage)
15
16
17 cv.EqualizeHist(grayscale, grayscale)
18
19
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
39 cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE)
40
41
42 device = 0
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
48 if not capture:
49 print "Error opening capture device"
50 sys.exit(1)
51
52 while 1:
53
54
55
56 frame = cv.QueryFrame(capture)
57 if frame is None:
58 break
59
60
61 cv.Flip(frame, None, 1)
62
63
64 detect(frame)
65
66
67 cv.ShowImage('Camera', frame)
68
69
70 k = cv.WaitKey(10)
71
72 if k == 0x1b:
73 print 'ESC pressed. Exiting ...'
74 break
75