Face.com API released

Wow, another cool API was just released today. Face.com has released an API for facial recognition. Not only will it do facial recognition, it will also do training (so it can help learn people) and integration with Facebook and Twitter. I played around with the API today and it’s easy to use. You can see the docs for yourself, and that’s without having to register. (Sorry, I get ticked off at API providers that require you to give over your personal information before deigning to tell you what their API looks like.) I whipped up a super quick ColdFusion sample to show you how it works. (And again, this is just an example of their facial recognition, they do more.)

<cfset apikey = "yeah, you need to sign in for this"> <cfset apisecret = "see above"> <cfset apiUrl = "http://api.face.com/faces/detect.json"> <cfset sourceimage = "c:\users\raymond\dropbox\photos\two starbucks.jpg"> <cfhttp url="#apiurl#?api_key=#apikey#&api_secret=#apisecret#" method="post"> <cfhttpparam name="body" type="file" file="#sourceimage#"> </cfhttp> <cfset resultStr = cfhttp.filecontent> <cfif isJSON(resultStr)> <cfset result = deserializeJSON(resultStr)> <cfif result.status is "success" and arraylen(result.photos)> <!--- read in our image ---> <cfset img = imageRead(sourceimage)> <!--- API supports N photos at once, but we assume 1 photo ---> <cfset photo = result.photos[1]> <cfloop index="tag" array="#photo.tags#"> <!--- to draw our square, we will need to get upper left corner, we are given a center+w,h ---> <!--- tag.center.x/y == % of total size ---> <cfset centerReal = { x=tag.center.x/100 * photo.width, y=tag.center.y/100*photo.height}> <cfset widthReal = tag.width/100*photo.width> <cfset heightReal = tag.height/100*photo.height> <cfset upperLeft = { x=centerReal.x-(widthReal/2), y=centerReal.y-(heightReal/2)}> <cfset imageDrawRect(img, upperLeft.x, upperLeft.y, widthReal, heightReal)> <cfdump var="#tag.attributes#" label="Tag attributes"> </cfloop> </cfif> <cfimage action="writeToBrowser" source="#img#"> </cfif>

In the template above, I create a URL request based on my API key and secret (which took all of two minutes to get when I signed up) and pass in a photo from my hard drive. They also support URLs as well as sending multiple images at once. Even better, if you are concerned about the speed of the response, you can provide a call back url and their service will ping you back with the results. Finally - and this I really dig - they will also tell you how many API calls you have left and how soon it will reset. I love how thorough they seem to be here.

Ok, so after I make the call, I start to work with the result. You get an array of photo results back, one for each you sent. Since my code is working with one I just assume the array has one item. Within that struct you have an array of tags. The tags contain data about your matches. Oddly - even though they know the size of your image, they return everything in percentages. That was the only part of this API I really didn't care for. You can see where I do a bit of math to convert these into values that make sense for my photo. Once I have that though it is relatively easy to draw a rectangle. I also dump the tag attributes. This is a pretty cool feature where it tells you how confident it is and things like gender, glasses, and if the person is smiling!

Finally, I use a quick writeToBrowser to test out the results. Here are a few samples.

Now check out this final copy. It correctly gets that Scott and I have glasses. it correctly says Dave isn't smiling. It gets my 'smile' factor wrong, but do note how lose the confidence is.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments