XRDS

Crossroads The ACM Magazine for Students

Sign In

Association for Computing Machinery

XRDS: Hello World (May 2010)

The code for the article Real-time detection with webcam (XRDS, June 2010) (download)

| More
01: ## The face detection demo for XRDS Hello World.
02: ## Adapted from OpenCV examples.
03: ##
04: ## Instructions: the detection will start automatically from
05: ## an available camera. If more than one camera is present, change the
06: ## index at line 16. Press ESC to quit.
07:
08: import sys
09: import cv
10:
11: storage = cv.CreateMemStorage(0)
12: image_scale = 1.3
13: haar_scale = 1.2
14: min_neighbors = 1
15: haar_flags = 0
16: camera_index = 0 # can change index if more than one camera
17: cascade_name = "./haarcascade_frontalface_alt_tree.xml"
18:
19: def detect_and_draw( img ):
20: # allocate temporary images
21: gray = cv.CreateImage( (img.width,img.height), 8, 1 )
22: small_img = cv.CreateImage(
23: (cv.Round(img.width/image_scale),
24: cv.Round (img.height/image_scale)
25: ), 8, 1 )
26:
27: # convert color input image to grayscale
28: cv.CvtColor( img, gray, cv.CV_BGR2GRAY )
29:
30: # scale input image for faster processing
31: cv.Resize( gray, small_img, cv.CV_INTER_NN )
32: cv.EqualizeHist( small_img, small_img )
33:
34: # start detection
35: if( cascade ):
36: faces = cv.HaarDetectObjects(
37: small_img, cascade,
38: storage, haar_scale,
39: min_neighbors, haar_flags )
40: if faces:
41: for (x,y,w,h),n in faces:
42: # the input to cvHaarDetectObjects was resized,
43: # so scale the bounding box of each face
44: # and convert it to two CvPoints
45: pt1 = (
46: int(x*image_scale),
47: int(y*image_scale)
48: )
49: pt2 = (
50: int((x+w)*image_scale),
51: int((y+h)*image_scale)
52: )
53: # Draw the rectangle on the image
54: cv.Rectangle( img, pt1, pt2,
55: cv.CV_RGB(255,0,0), 3, 8, 0 )
56: cv.ShowImage( "result", img )
57:
58: if __name__ == '__main__':
59:
60: # Load the Haar cascade
61: cascade = cv.Load(cascade_name)
62:
63: # Start capturing
64: capture = cv.CaptureFromCAM(camera_index)
65:
66: # Create the output window
67: cv.NamedWindow("result",1)
68:
69: frame_copy=None
70: while True:
71: frame = cv.QueryFrame( capture )
72:
73: # make a copy of the captured frame
74: if not frame_copy:
75: frame_copy = cv.CreateImage(
76: (frame.width,frame.height),
77: cv.IPL_DEPTH_8U,
78: frame.nChannels
79: )
80: cv.Copy( frame, frame_copy )
81:
82: detect_and_draw(frame_copy)
83:
84: c = cv.WaitKey(7)
85: if c==27: # Escape pressed
86: break
87:

Additional Resources

Object identification links

Viola-Jones algorithm

Haar training tutorial

Haar cascades repository

HCI projects using OpenCV
HandVu – gesture recognition

ehci: head tracking

PyEyes: eye tracking

CCV/touchlib: multitouch library

Other HCI/CV toolkits
TUIO: common API for tangible multitouch surfaces (list of implementations)

Trackmate: do-it-yourself tangible tracking system

Sphinx: speech recognition toolkit

vxl: versatile computer vision libraries

Integrating Vision Toolkit