DIFFICULTY: MEDIUM Human Detection With SimpleCV and Python PANGOLINPAD.YOLASITE.COM
SimpleCV
Python ‘wrapper’ for the Open Computer Vision (OpenCV) system
Simple interface for complicated image processing Importable into a python program
#!/usr/bin/python Import time Import SimpleCV
Installation
SimpleCV is not included in the Raspbian distribution
It also relies a set of python packages to work Install them all with:
$ sudo apt-get install ipython python-opencv python-scipy python-numpy python-pygame python-setuptools python-pip
Installation
Use pip (you just installed it) to get the latest version of SimpleCV
And finally make sure everything is up-to-date
$ sudo pip install https://github.com/ingenuitas/SimpleCV/zipball/master
$ sudo add-apt-repository ppa:gijzelaar/opencv2.3 $ sudo apt-get update
The SimpleCV Shell
A ‘shell’ is a user interface SimpleCV has a dedicated one you can start just by
typing:
The following command may be necessary the 1st time:
$ simplecv
$ python -m SimpleCV.__init__.
USB camera
Designed for USB cameras, not the Raspberry Pi’s camera module
If you have a USB webcam, make a python program like this:
from SimpleCV import Camera # Initialize the camera cam = Camera() # Capture and image and display it cam.getImage().show()
Camera module
You can set things up so that the camera module appears as a USB webcam
Or you can use the raspistill command to save an image, then get SimpleCV to look at that
Example code
This takes & displays a 500x500 pixel image named ‘image.jpg’ on the screen
from SimpleCV import show, Image import os # Capture an image os.system(‘raspistill –n –w 500 –h 500 –o image.jpg’) # Save the image in a variable img = Image(‘image.jpg’) # Display it on screen img.show()
Continuous loop
from SimpleCV import Display, Image Import time display = Display() print "I launched a window" # Loop until window is closed while not display.isDone(): os.system(‘raspistill –n –w 500 –h 500 –o image.jpg’) img = Image(‘image.jpg’) # Here’s where you’d put any image processing img.save(display) time.sleep(0.1) print "You closed the window"
Haar-like features
Haar-like features are used to compare portions of an image
Using the relative brightness of two adjacent blocks of pixels, distinct blocks are found in an image
Human eyes are generally darker than their cheeks If an image has two dark Haar features above two
light ones, it’s probably a face
findHaarFeatures()
This function built into SimpleCV can be used to find 8 different things: Forward-looking faces Profile faces Eyes Noses Mouths Ears Upper bodies Lower bodies
from SimpleCV import Display, Image display = Display() print "I launched a window" # Loop until window is closed while not display.isDone(): os.system(‘raspistill –n –w 500 –h 500 –o image.jpg’) img = Image(‘image.jpg’) # Look for a face faces = img.findHaarFeatures('face') if faces is not None: # Draw a box around the face faces.draw() # Say how many faces were found print ‘%s faces detected” % len(faces) img.save(display)
Drawing on an image
Be default, draw() places a green square at the specified coordinates
You can change this to anything you like with the following code
Include this in place of the line:
In the code on the previous slide
icon = Image(‘iconName.jpg’) img.dl().blit(icon, coordinates=faces)
faces.draw()
A fun example
[video/image]
I’ll have this running in real-time after the talk, so if you’ve ever wondered what you’d look like with a moustache…
More practical applications
Security camera that only records human activity Want to keep an eye on your sports car But don’t want a hard-drive full of the neighbour's cat
Robot vision Identify people to greet them Robotic ‘pet’ that follows you around
Performance
Smaller features are harder to identify Greater distance = smaller features Can’t detect eyes across the room
Smaller image resolution = smaller features But high resolution = more image to work through = slower
detection
Try different features for different situations