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']['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']['tags'][iii]['attributes'][metric]['value'] conf = photo['photos']['tags'][iii]['attributes'][metric]['confidence'] if conf > 20: # do something with metric