Visual information: facial recognition

Once again, this will be short and sweet. The code here is the most simple, it does not save any of the processed images. If people are interested in that I can post code that will save images of the faces with their features labeled, but I am excluding that now for clarity. Here is the basic code:

def recognizeFace():

	# Take picture and save to webserver
	filename = "/var/www/temp.jpg"
	subprocess.call("sudo raspistill -w "+ str(saveWidth) +" -h "+ str(saveHeight) + " -t 1 -n -vf -e " + fileType + " -q 15 -o %s" % filename, shell=True)
	print "Captured image: %s" % filename

	# Send the picture to the SkyBio Api
	theFileLocation =  "http://YOURPUBLICIP/temp.jpg"
	client = FaceClient(YOURSKYBIOAPI,YOURSKYBIOAPISECRET)
	photo = client.faces_recognize('all',theFileLocation, namespace = 'YOURSKYBIONAMESPACE')

	# Number of people in photo
	numFaces = len(photo['photos'][0]['tags'])
	print "Detected " + str(numFaces) + " faces."

	# Go through each face
	theSpeech = ""
	while iii2: # proxy for if something happened
		print theSpeech
		saySomething(theSpeech,"en")

Now for the explanation. Firstly, this code uses this API which gives 5000 calls/month for free for facial recognition. It works well if you train it well. I will defer the explanation of the training to others who wrote beautifully on this.

For this API to work, though, you have to send the picture from a location on the Web. There are a few ways to do this. If you already have a webserver you can just SCP it over your VPN network. I went for a simpler solution. I hosted the image on my RPi using an apache webserver – instructions are here. Don’t forget to forward port 80. You can get your public IP address here. Now you can use the /var/www/ as your web server folder, and you can pass those images to SkyBio. Note: I suggest password protecting your folder using .htaccess

This code just finds each face. However, SkyBio tries to find all sorts of other things like whether you have glasses or not. You can probe all these things (and their confidence levels) with the following:

skybio_metrics = ['eyes','sadness','mood','glasses']
for metric in skybio_metrics:
	val = photo['photos'][0]['tags'][iii]['attributes'][metric]['value']
	conf = photo['photos'][0]['tags'][iii]['attributes'][metric]['confidence']
	if conf > 20:
		# do something with metric

Does this work? See for yourself (this output comes from a slightly modified version of above that prints the metric over the face):
pgm-20140130195702

It seems to always think I’m sad 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: