If you want to include facial recognition in your app it is a relatively straightforward process to ensure that any faces in your end users’ photos are successfully detected, and once particular faces are enrolled, recognized, with a high degree of accuracy. This blog post aims to explain the steps involved in successful facial recognition.
There are three simple steps which you can take that will ensure that this process runs smoothly.
Step 1: Enroll Photos
Before Kairos can begin putting names to faces in photos it needs to already know who particular people are and what they look like.
This is no different to how we as human beings operate. Until we have been introduced to someone, they are still a stranger to us. The process of enrolling photos into Kairos, is effectively the “introduction” to Kairos of the people depicted in those photos. Until that point, the people in the photos are as much of a “stranger” to Kairos, as anyone else in the world is.
So, when an end user enrolls a photo with Kairos, what is actually happening is that your app sends a photo to Kairos that includes a particular person’s face.
Each photo submitted to Kairos contains the location of the photo, the subject ID and a gallery name.
Obviously to begin this process successfully Kairos needs to know where the photo is located. Your app needs to submit to the Kairos API either a URL of a web address where the photo is situated, or alternatively it can submit a Base64 encoded photo.
Your app also sends to Kairos a subject ID. From Kairos’ point of view, it doesn't matter what the subject ID actually is. This does have to be a unique identifier to the person in the photo, though, so it would often be that person’s name (if nobody else has the same name), or some other wording that uniquely identifies them.
As your app’s end users build up enrollment photos of people they may have a need to group them in various ways. For instance, if your app is a security app, there may be a need to set up different groups of people depending on different security clearances, or for an education app a school might like to group their enrolled faces by classes. This can be easily done by grouping photos into galleries. Therefore each photo that is submitted to Kairos for enrollment needs to have a gallery name attached to it.
Here are some detailed code snippets demonstrating the enrollment process at Kairos Docs.
include("Kairos.php");
// Instantiate a new Kairos object
$app_id = 'myappid';
$api_key = 'myapikey12345678910111213';
$Kairos = new Kairos($app_id, $api_key);
// Enroll an image
$path = '/images/myphotos/elizabeth1.jpg';
$subject_id = 'elizabeth';
$gallery_id = 'friends1';
$response = $Kairos->enrollImageWithPath($path, $gallery_id, $subject_id);
Kairos makes a copy of the photo, doing any necessary preparation work first. This includes things like doing any required rotation of the photo, repairing any corruption in the photo, and stripping out the EXIF information.
It then goes through the process of face detection. Face detection simply involves finding faces in a photo, and this is done by scanning over the photo looking for patterns that match a typical face. To help with this there is an optional parameter that you could have sent through with the photo called minHeadScale. This basically tells Kairos what the smallest face that they should be looking for in a photo is. If this parameter is not specified, it is set to .125 (1:8 scale).
Once Kairos has detected any faces in the photo, it will create a face template. This is a numeric representation of the features found for each specific face in the photo. It will enroll the faces into the system, using the subject ID as the “name”, placing them in the appropriate gallery.
At this point, the enrollment process is complete, for that photo, and the photo is discarded. For this reason, it is not possible for us to return photos at a later stage.
One final tip in relation to photo enrollment. Your end user can enroll multiple pictures of the same person to improve the matching accuracy. They should use the same subject ID and gallery for each one. We find that six to eight pictures is optimal, but it works fine with just one.
Step 2: Recognize Photos
For facial recognition to work your app’s end user needs to have already enrolled photos as shown in Step 1. If they haven’t done this, everybody in their photos will be strangers to the Kairos API.
Again, here are detailed code snippets demonstrating the recognition process at Kairos Docs.
// Recognize an image
$path = '/images/myphotos/elizabeth2.jpg';
$gallery_id = 'friends1';
$response = $Kairos->recognizeImageWithPath($path, $gallery_id);
Your app will submit a JPG or PNG photo to Kairos, specifying a particular gallery name, against which it needs to be compared. As with the photos that have been used for enrollment, when photos used for recognition are submitted you need to include either a url stating the location of the photo, or you submit the photo in Base64 encoding.
Kairos scans the photo, detecting faces and creating a face template (just as it did with the enrollment photos).
Each face template created from the photo is then compared to the templates that have been created when faces were enrolled in the particular gallery that has been specified.
Kairos then returns a list back to your app showing the subject IDs (names if you have set them up that way) of people that it believes are shown in the submitted photo.
Step 3: Understanding Results
For each face there may be a series of possible subject IDs along with the probability of it being that person depicted. For instance it might return the following for a particular face: “Bob .95, Fred .70, Mary .40”, indicating that it is probably a picture of Bob, with a 95% probability.
In many ways “threshold” of recognition is very important. In the above example, Bob’s 95% probability means that there is a very good chance that it is Bob in the photo.
Kairos has set a default threshold of 80%. This tells the API to report a successful match for a photo if the software determines that there is a match at a confidence level of 80% or higher. If all confidence levels are lower than 80% then the API would say that there is no match, and return no list of potential subjects (names).
Threshold is actually another optional parameter that can be entered into the API, if the end user of your app wants it set at a level other than 80%.
Be aware, though, that if it is set too high, perfectly good matches are likely to be rejected - known in the industry as false rejects. Conversely setting the threshold too low may result in candidates “matching” that aren't correct. These are known as false positives or false accepts.
Where to set the threshold is entirely dependent on what your app is being used for. For high security use it is likely you would set a high threshold, as it is probably better to require someone to test a few photos before one is accepted, than it is to accidentally let a bad guy into the building. For some uses that are less critical it might be better to set the threshold lower if it isn’t as worrisome if a false positive is found. For other use cases it doesn't matter at all, such as “Which star of the Love Boat do I resemble the most?”.
Next steps
If you are considering using facial detection and facial recognition in your app, incorporating the Kairos API, hopefully you will have seen how easy the process is. Once you have incorporated the API in your app all it takes is for your end users to initially enroll photos, with their name or other subject ID, and then later to submit other photos to compare against the facial templates that have been created.
There are example coding snippets for all stages of the process in our developer documentation.
Contact us today if you need any additional help or assistance, or sign up to start developing with Kairos.
Cole Calistra
Cole is the CTO at Kairos, a Human Analytics startup that radically changes how companies understand people. He loves all things cloud and making great products come to life.