Face recognition in Ruby Using Kairos also Finding your celebrity-look-alike

19 December 2014 by siddharth No comments

Humans have an innate need to be identified with a group, that drives us to be an important part of something bigger than us. This implies a relationship that is greater than familiarity or acquaintances. “Facial structure” plays a big part in that identity, making online services like find-your-celebrity-look-alike a guaranteed success.

Today, we are going to look under the figurative hood: How this technology works. We will be building a sample find-your-celebrity-look-alike application on Ruby using “Kairos”, a Third party face recognition api. With a simple example, we will discover how face recognition works, how we use data from kairos and briefly also touch on collecting and cleaning up celebrity facial data. The other alternative to using Kairos api includes face++, Animetrics.com or rekognition.com.

The first step would be building a cache of celebrity faces. Before the advent of publicly available face data on the internet, this tedious job had to be done manually but thanks to crowdsourcing, now a lot of public repositories are available with “facial” data, to use for non-commercial purposes.

One such repository, FaceScrub is a database of 1,00,000 photos of 530 celebrities classified by gender and name.

A snippet of it looks like this

source http://vintage.winklerbros.net/Images/facescrub.jpg 

 

So to begin, first download the data set using the following command.

$ curl http://vintage.winklerbros.net/faceScrub.zip

Unzip the file.

$ unzip faceScrub.zip

The compressed file is password protected and you can get the password by filling out this form . This would yield a file with the name: facescrub_actors.txt . You can view the content of the file by using vim.

$ vim facescrub_actors.txt

$ vim facescrub_actresses.txt

Now you have a file that acts as an index to the final cache of face images of celebrities we need. So now we have to write a ruby script to read this file and create a cache in a new directory.

$gem install kairos-api

$gem install typhoeus

Unified code to upload the data and match your facebook profile photo to the celebrities

#ex: facebook_user_name : shashank.singh
facebook_user_name = "put_your_facebook_user_name_here" 
urls_hash = []
begin
#Repeat same code for facescrub_actresses.txt
file = File.new("facescrub_actors.txt", "r")
while (line = file.gets)
content = line.gsub(/s+/m, ' ').strip.split(" ")
urls_hash << content[4]
# puts "#{counter}: #{content[4]}"
end
file.close
rescue => err
puts "Exception: #{err}"
end
#Counter is used to set subject_idr
counter = 1
result = ""
require 'socket'
require 'kairos'
require 'typhoeus'
#Get App Id and App Key from https://developer.kairos.com/admin
client = Kairos::Client.new(:app_id => 'put_your_app_id_here', :app_key => 'put_your_app_key_here')
urls_hash.each do |url| 
begin
result = client.enroll(
:url => url, 
:subject_id => counter, 
:gallery_name => 'celebrities')
rescue => e
puts "Error importing this url because of " , e
end
counter = counter + 1
puts "[#{counter}] #{result}" 
end
#You can list all files Enrolled
# puts client.gallery_view(:gallery_name => 'celebrities')
#Lets get your facebook profile photo and match to 530 celebrities we have
graph_url = "http://graph.facebook.com/#{facebook_user_name}/picture?type=large"
#Lets print which celebrities face matches our’s
puts client.recognize(
:url => graph_url, 
:gallery_name => 'celebrities', 
:threshold => '.2', 
:max_num_results => '5')

In my next post, we will take the concepts discussed here and create a full blown web application in Ruby on Rails .

 

siddharth

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)