Raspberry Pi

view

Make A Snapchat lens for your webcam?

Ever use a Snapchat lens and just wonder “How’d they do that?”. Well, even if not, I’m going to take a look at some of the face tracking and computer vision technologies that ma...

Step 1

Install Software

In order to track faces using a webcam on the Raspberry Pi, you need to install “Computer Vision” software. There’s an easy to use python-based software called “SimpleCV” that is free to install on Windows, Mac and Linux. My instructions below are for how to install it on a Raspberry Pi.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install ipython python-opencv python-scipy

sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master

sudo pip install pyparsing svgwrite

sudo modprobe bcm2835-v4l2

Step 2

Writing The Program



Here’s some sample python code that I used to track a mustache onto faces that the webcam recognizes:

#!/usr/bin/python

from SimpleCV import *
display = Display()
cam = Camera()
mustache = Image("stache4.png")
mask = mustache.hueDistance(color=Color.GREEN).binarize()
result = (mask.invert() - mustache)

face_cascade = HaarCascade("face.xml")
nose_cascade = HaarCascade("nose.xml")

while display.isNotDone():
 image = cam.getImage().flipHorizontal()
 faces = image.findHaarFeatures(face_cascade)
 if faces:
 faces = faces.sortArea()
 face = faces[-1]
 myFace = face.crop()
 noses = myFace.findHaarFeatures(nose_cascade)
 if noses:
 noses = noses.sortArea()
 nose = noses[0]
 xf = face.x - (face.width()/2)
 yf = face.y - (face.height()/2)
 xm = nose.x - (nose.width()/2)
 ym = nose.y - (nose.width()/2)

xstache = xf+xm-(mustache.width/2)+(nose.width()/2)
 ystache = yf+ym+(2*nose.height()/3)
 image = image.blit(mustache,pos=(xstache,ystache), mask=mask.invert())
 image.show()