Installing OpenCV For Python
To install OpenCV for Python, all you have to do is use apt-get like below:
sudo apt-get install python-opencv
To test the installation of OpenCV, run this Python script, it will switch on your camera for video streaming if it is working.
3 | cv.NamedWindow( "w1" , cv.CV_WINDOW_AUTOSIZE) |
5 | capture = cv.CaptureFromCAM(camera_index) |
10 | frame = cv.QueryFrame(capture) |
11 | cv.ShowImage( "w1" , frame) |
15 | capture = cv.CaptureFromCAM(camera_index) |
18 | capture = cv.CaptureFromCAM(camera_index) |
Simple Example of Raspberry Pi Face Recognition
This example is a demonstration for
Raspberry Pi face recognition using haar-like features. it finds faces in the camera and puts a red square around it. I am surprised how fast the detection is given the limited capacity of the Raspberry Pi (about 3 to 4 fps). Although it’s still much slower than a laptop, but it would still be useful in some robotics applications.
You will need to download this trained face file:
3 | The program finds faces in a camera image or video stream and displays a red box around them. |
7 | from optparse import OptionParser |
15 | def detect_and_draw(img, cascade): |
17 | gray = cv.CreateImage((img.width,img.height), 8 , 1 ) |
18 | small_img = cv.CreateImage((cv. Round (img.width / image_scale), |
19 | cv. Round (img.height / image_scale)), 8 , 1 ) |
22 | cv.CvtColor(img, gray, cv.CV_BGR2GRAY) |
25 | cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) |
26 | cv.EqualizeHist(small_img, small_img) |
30 | faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage( 0 ), |
31 | haar_scale, min_neighbors, haar_flags, min_size) |
32 | t = cv.GetTickCount() - t |
33 | print "time taken for detection = %gms" % (t / (cv.GetTickFrequency() * 1000. )) |
35 | for ((x, y, w, h), n) in faces: |
38 | pt1 = ( int (x * image_scale), int (y * image_scale)) |
39 | pt2 = ( int ((x + w) * image_scale), int ((y + h) * image_scale)) |
40 | cv.Rectangle(img, pt1, pt2, cv.RGB( 255 , 0 , 0 ), 3 , 8 , 0 ) |
42 | cv.ShowImage( "video" , img) |
44 | if __name__ = = '__main__' : |
46 | parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]" ) |
47 | parser.add_option( "-c" , "--cascade" , action = "store" , dest = "cascade" , type = "str" , help = "Haar cascade file, default %default" , default = "../data/haarcascades/haarcascade_frontalface_alt.xml" ) |
48 | (options, args) = parser.parse_args() |
50 | cascade = cv.Load(options.cascade) |
57 | if input_name.isdigit(): |
58 | capture = cv.CreateCameraCapture( int (input_name)) |
62 | cv.NamedWindow( "video" , 1 ) |
69 | width = int (cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)) |
71 | cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width) |
74 | height = int (cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)) |
76 | cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) |
82 | frame = cv.QueryFrame(capture) |
87 | frame_copy = cv.CreateImage((frame.width,frame.height), |
88 | cv.IPL_DEPTH_8U, frame.nChannels) |
90 | if frame.origin = = cv.IPL_ORIGIN_TL: |
91 | cv.Copy(frame, frame_copy) |
93 | cv.Flip(frame, frame_copy, 0 ) |
95 | detect_and_draw(frame_copy, cascade) |
97 | if cv.WaitKey( 10 ) > = 0 : |
100 | image = cv.LoadImage(input_name, 1 ) |
101 | detect_and_draw(image, cascade) |
104 | cv.DestroyWindow( "video" ) |
To run this program, type in this command in your VNC Viewer’s terminal:
python facedetect.py --cascade=face.xml 0
The number at the end represents the number of your video device.
No comments:
Post a Comment