Top Banner
DIFFICULTY: MEDIUM Human Detection With SimpleCV and Python PANGOLINPAD.YOLASITE.COM
18

Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

Aug 06, 2018

Download

Documents

lyhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

DIFFICULTY: MEDIUM

Human Detection With SimpleCV and Python

PANGOLINPAD.YOLASITE .COM

Page 2: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · 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

Page 3: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 4: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 5: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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__.

Page 6: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .
Page 7: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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()

Page 8: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 9: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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()

Page 10: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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"

Page 11: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 12: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 13: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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)

Page 14: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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()

Page 15: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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…

Page 16: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 17: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

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

Page 18: Human Detection With SimpleCV and Python - Yolapangolinpad.yolasite.com/resources/documents/1 piHuman.pdf · Human Detection With SimpleCV and Python . PANGOLINPAD.YOLASITE.COM .

Useful Links

SimpleCV beginner’s guide: http://homepage.cem.itesm.mx/carbajal/EmbeddedSystems/

SLIDES/Computer%20Vision/Computer%20Vision%20using%20SimpleCV%20and%20the%20Raspberry%20Pi.pdf

Camera module as a camera SimpleCV recognises: http://www.raspberrypi.org/forums/viewtopic.php?t=57788