How we used free + open source tools to build a facial recognition app

Featured

Here’s a post I’ve been meaning to write for a while, but just never got around to it.

As a Waterloo Engineering student, we have to complete a final year design project under the guidance of a supervisor in groups of three.

What have I been making?

My project is called Peekaboo. It uses facial detection and recognition to allow a user to find out more about people around them.

I’ve been working on this for about 5 months now with my group members. We’ve made sufficient progress and are now at a stage where I can talk about our systems architecture and some of the design decisions that we made.

System Overview

Peekaboo is comprised of 3 components, shown in the diagram below.

System Overview

iOS App + Face Detection + Parse

We have a native iOS app that uses Parse for it’s mobile backend. We’ve been very happy with Parse, and it was painless to set up. Face detection is performed on the device itself through Face.com’s API. We went with Face.com over OpenCV’s Face detection due to higher accuracy and the availability of more data. Face.com is able to give us JSON back with eye positions, mouth posiitions, head tilt and yaw, gender, and more.

I strongly believe user experience should be important to any project. We use a lot of great open source iOS UI components available at Cocoa Controls.

Web Server on Heroku

We wrote a web server in Node.js deployed over Heroku to act as an API. Once again, Heroku makes this painless to set up and deploy. The web server is the mediator used by the iOS app and the desktop C++ code. The web server talks to Parse to get information and feeds it to the C++ code (and vice versa). It uses standard authentication.

The web server has a MySQL database which we usually query instead of directly querying Parse. This way, we stay below the rate limits. Parse is only queried when there is a face that needs recognition.

  • We found Sequelize to be a good ORM for MySQL (available as an npm module)
  • We found Request to be a useful module for simplifying HTTP Requests.

Facial Recognition Algorithms

I won’t lie, I’m not the best at C++. I find web languages and Objective-C a lot easier to code in. Nonetheless, when doing Face Recognition, we had to use C++. It’s mainstream and fast. Of course, we went with OpenCV, one of the most popular computer vision libraries out there (See my post on how to set up OpenCV on Mac).

Like I said, we don’t use OpenCV for Face detection. We use it to work with images, do some basic image processing (histogram equalization) and use OpenCV’s Eigenfaces implementation. We have also implemented another Facial Recognition algorithm known as LDA (Linear Discriminant Analysis). This was done mostly through reading research papers and contacting people smarter than us. Google Scholar is a useful resource. LDA gives us a 10-15% improved recognition rate over EigenFaces.

Communication Layer

Since I’m not a big C++ guy, I wrote a pretty thin Objective-C layer on top of the C++ code that communicates with the web server and figures out when to execute the facial recognition code. I was able to leverage a lot of my iOS knowledge for this, using standard libraries such as ASIHTTPRequest and SBJSON (ASIHTTPRequest has recently been stopped being maintained, but for our project, it does the job).

System diagram of how facial recognition works

Final Thoughts

Some parts of our code are a little hacked together. This is partly due to the time crunch we are under and partly due to the fact that we are still learning. However, I am pretty proud of what we have achieved so far. We can accurately (over 70%) recognize a person who has been trained by our system (database size is 70 people currently). We’ve used $0 on this project, which is a testament to how open and amazing our software engineering industry is. We have learned a lot through out this process. I’ll put another post up with a live demo sometime soon.

If you have questions, ask them below!

Get your Facebook Cover Photo using FQL

Facebook’s Graph API doesn’t have a method to easily retrieve a cover photo. However, provided that you (or a user) has given your app access to their Photo Albums, you can get the photo pretty easily.

The best way to get it through a single request is using the following FQL query.

SELECT cover_object_id from album where aid IN (SELECT aid, name FROM album WHERE owner=me() AND name="Cover Photos")

Note that you will need an access token to use this query. It definitely works for me, but I’m still testing with permissions to see what’s needed to make it work with other users. Will post an update if anything changes.

Update: Yep, this works. Have it running on my iPhone right now. Remember to change “me()” with the Facebook ID of the user. “me()” will only work from the FQL console.

Cool little Objective-C Method for grouping Facebook likes

Firstly, I just want to say I’m pretty proud of myself for writing compilable code at 4:30am. Go me.

I am making an iOS app as part of my 4th year design project, and we are leveraging Facebook for it. Basically, our app allows you to take a picture of someone’s face and we use facial recognition to pull up their Facebook profile.

Anyway, I was fooling around with the Facebook iOS SDK and one of the things I’ve been wanting to do is to group one’s likes by category. Facebook seems to just return a large array of likes, with no grouping. This little method below groups them so I can have a table view with headings like “Favorite Books”, “Favorite Movies”, etc. Check it out.

An Update on TopForty.it

Figured I would give a quick update as to how TopForty.it been doing. We’re about 2 weeks in, and the response has been great. People seem to love the idea, the execution and the UI/UX, which is heartwarming although I still think it’s rough around the edges (won’t tell you where because then you will notice it =D ).

Some stats: In 2 weeks, people have listened to over 5000 songs (5194 as of last night), totalling in a little under 25,000 minutes. Our visitor return rate is over 60% and the average time spent on site is around 30 minutes. These numbers seem to be increasing, which is good considering the fact that we haven’t really promoted it much (blogs, reddit, HN, etc.).The growth seems to be organic with 50% traffic from Facebook. As we add features, we plan to hit Hacker News and some blogs.

Update: Techvibes did cover us from the demo we gave at Velocity’s end-of-term Demo Day. Since them, we’ve also been featured on GigaOm and The Next Web

Update: Here are the photos from Velocity’s Demo Day.

It’s been really fun to work on this project. I think where we are at now is interesting as we are trying get it out of the “hack” stage and into the “software” stage. Up until a certain point, you don’t give much importance to architecture, focussing on getting features out as quickly as possible. This is changing as the LOC grows. Since all the application logic is in JS, I have to spend a few days really cleaning that out. The good news is that YUI does a lot of the work for you, so I won’t have to refactor too much. I will take that time to pull in some of the new Y.App stuff that’s in 3.5.0.

I really do wish to write a blog post at some stage detailing how the UI works – as much as for my own records as for other people’s understanding. I have exams going on now, so it’ll probably have to wait till mid-December. I have started, but don’t want to publish something that’s half-baked.