Humanizing your iOS application with Face Detection API, Kairos.

James Rochabrun
4 min readMar 4, 2018

With iOS 11 Apple provide new very interesting API’s, one of them is the Vision Framework that enables image analysis and face detection, although face detection is not new for Apple and has been available since iOS 5 with tools like CIDetector, Vision framework provides a more accurate and detailed information about a “detected face”.

The future is very promising for what Apple offers but for now still has its limitations, that’s why in this post I am going to use one API that takes the face recognition to the next level giving us interesting insights about the “detected face” like gender, age, race, are there glasses on that face?, is it smiling?

Imagine how you can improve your apps providing a more customized user experience based on this information if you have the budget for it, of course, unfortunately, this services may not be free but for now, let’s just take advantage of their free trial to see its power :).

The API selected is Kairos, I choose this tool for multiple reasons but mostly because I like what they are doing, the documentation is awesome and is very straightforward to implement, check their example if you are not convinced yet.

First things first, let’s go to their website and create a FREE API Key, yes, you will have to register but you don’t need to add any bank information, once you have your account created and you have your API Key and ID we are ready to start, you have to register an application and you will see something like this….

Fetch this starter project it has a master branch with the starter project that contains a basic picker already implemented so you don’t need to worry about the UI and also a kairosAPI branch with the full implementation of what we will do now, run the app and you will see this.

They have a wrapper on Github that I took as a starting point for this, feel free to check it out here, create a new file and call it KairosAPI, then copy and paste this gist…

This code has a KairosConfig struct and there you just need to replace the id and API Key for yours, the rest is just standard Network calls, I hope you are familiar with this kind of implementations if not check this blog.

We will use this to construct our different calls to Kairos API if you go to their docs you will see the different calls that you can make, we will start with “detect” for this call we need to send a media, in this case, the image that we want to analyze, create a new empty file and copy and paste this gist again…

I parsed this response using different images, people with glasses, smiling and not etc, and checked for the keys and values displayed, so if you are more curious I suggest to do it as well and see the data available that can be valuable for your app.

Ok, now let’s analyze some faces! open the DetectionVC and inside the extension of DetectionVC replace what you see inside of it for this…

func manager(_ manager: PhotoPickerManager, didPickImage image: UIImage) { 
self.photoImageView.image = image
KairosAPI.sharedInstance.detect(image) { [unowned self] result in
switch result {
case .success(let analyzis):
self.setUIForResponse(.analyzed(analyzis))
case .error(let error):
self.setUIForResponse(.error(error))
}
}
manager.dismissPhotoPicker(animated: true, completion: nil)
}

Now run the app and select a photo from your library or take a selfie if you want :) you will see that now all the data from the response is displayed in the app like this…

In my personal opinion, I think does a pretty great job, this is for sure just the tip of the iceberg for what Kairos can do if you are interested in how to do a feature like tagging a face so your app knows that it belongs to somebody leave your comments and I will do the second part using the “Verify” and “Enroll” request of this API, what is your favorite Face recognition API? how far did you get just using Vision from Apple? I will love to know, thank you!

More where this came from

This story is published in Noteworthy, where thousands come every day to learn about the people & ideas shaping the products we love.

Follow our publication to see more product & design stories featured by the Journal team.

--

--