/
/cap
1 from __future__ import division
2 import opencv.adaptors
3 import PIL.Image
4 import time, pygame
5
6 import sys, os
7 sys.path.append("/my/proj/cuisine/build/lib.linux-i686-2.4")
8 from cuisine.libdv import Decoder, decodedframesize
9 from cuisine.pour.deck import Dv1394device
10 from cuisine import pyxbuf
11
12 # Parameters for haar detection
13 # From the API:
14 # The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned
15 # for accurate yet slow object detection. For a faster operation on real video
16 # images the settings are:
17 # scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
18 # min_size=<minimum possible face size
19 min_size = opencv.cvSize(20*3,20*3)
20 image_scale = 1.3
21 haar_scale = 1.2
22 min_neighbors = 2
23 haar_flags = 0
24
25 def face(storage, cascade, img, surf):
26 gray = opencv.cvCreateImage( opencv.cvSize(img.width,img.height), 8, 1 );
27 small_img = opencv.cvCreateImage(opencv.cvSize(
28 opencv.cvRound(img.width/image_scale),
29 opencv.cvRound (img.height/image_scale)), 8, 1 )
30 opencv.cvCvtColor( img, gray, opencv.CV_BGR2GRAY )
31 opencv.cvResize( gray, small_img, opencv.CV_INTER_LINEAR );
32
33 opencv.cvEqualizeHist( small_img, small_img )
34
35 opencv.cvClearMemStorage( storage )
36
37 t = opencv.cvGetTickCount();
38 faces = opencv.cvHaarDetectObjects(small_img, cascade, storage,
39 haar_scale, min_neighbors,
40 haar_flags, min_size )
41 t = opencv.cvGetTickCount() - t;
42 print "detection time = %gms" % (t/(opencv.cvGetTickFrequency()*1000.));
43
44 for r in faces:
45 print "found"
46 pygame.draw.ellipse(surf, (255,255,0),
47 (int(r.x*image_scale), int(r.y*image_scale),
48 int(r.width*image_scale), r.height*image_scale),
49 3)
50
51
52
53
54 dv = os.open('/dev/dv1394/0', os.O_RDONLY | os.O_NONBLOCK)
55
56 pixels = pyxbuf.getbuffer(decodedframesize('rgb'))
57
58 dec = Decoder(0, 1, 1)
59 dec.quality = 4
60
61 screen = pygame.display.set_mode((720, 480))
62
63
64
65 # the OpenCV API says this function is obsolete, but we can't
66 # cast the output of cvLoad to a HaarClassifierCascade, so use this anyways
67 # the size parameter is ignored
68 cascade_name = "/my/proj/headtrack/opencv-1.0.0/data/haarcascades/haarcascade_frontalface_alt.xml"
69 cascade = opencv.cvLoadHaarClassifierCascade( cascade_name, opencv.cvSize(1,1) )
70 storage = opencv.cvCreateMemStorage(0)
71
72
73 while 1:
74 try:
75 frame = os.read(dv, 120000)
76 except (OSError, IOError), e:
77 time.sleep(.01)
78 continue
79 dec.parseheader(frame)
80 dec.decodeframe('rgb', frame, pixels)
81
82 pilFrame = PIL.Image.fromstring("RGB", (720,480), str(pixels))
83 ipl = opencv.adaptors.PIL2Ipl(pilFrame)
84
85
86 surf = pygame.image.fromstring(str(pixels), (720, 480), "RGB")
87 screen.blit(surf, (0,0))
88
89 face(storage, cascade, ipl, screen)
90
91
92 pygame.display.update()
93 time.sleep(1/30)