Kairos Facial Recognition
In this project we introduce the Kairos face recognition service, and then link you to a project where you can use it with an ARTIK board and camera.
For this tutorial, you will "pre-enroll" (encode and upload) photos of faces to the Kairos cloud service. Then you will encode and post a photo to the Kairos face recognition service to check against the pre-enrolled faces. One of three results will be returned to the system:
-
Success – at least one of the faces is recognized
-
Fail – no faces are recognized
-
No face detected – need to try another picture.
Setting Up Kairos Account
The important part of this project is the face recognition provided by Kairos. To get started:
-
Register at www.kairos.com for a free personal trial.
-
Create an app and get the corresponding
app_id
andapp_key
. Copy these to a text file for use in the next sections.
Setting up Image Enrollment
You'll need a way to "enroll" a new image of a person to Kairos. We'll assume you have a JPEG photo file available to test with.
-
Prepare by installing a couple of Python utilities.
apt install python-pip
pip install pybase64
-
Copy the code below to a file named
enroll.py
on your ARTIK board. -
Replace the
app_id
andapp_key
values with your own.
enroll.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import base64 import sys from urllib2 import Request, urlopen import json from pprint import pprint with open('./image_vga.jpg', "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) values = """ { "image":" """+encoded_string+""" " , "subject_id": "abc123", "gallery_name": "KP" } """ headers = { 'Content-Type': 'application/json', 'app_id': 'c7c6axxa', 'app_key': 'ac4705bdac94ffxx686db9f8532b9453' } request = Request('https://api.kairos.com/enroll', data=values, headers=headers) response_body = urlopen(request).read() target = open('./Enroll.txt', 'w') target.truncate() target.write(response_body) target.write("\n") target.close() print(str(response_body)) |
Try it out: Copy a JPEG photo of someone you know to your ARTIK board, and name it image_vga.jpg
. Then run:
python enroll.py
Do the results accurately describe the person in the photo?
Setting up Image Validation Requests
Using a procedure similar to enrollment, you'll send an image to Kairos that you want checked to determine whether it contains any known (previously enrolled) faces.
-
Copy the code to a file named
validate.py
on your ARTIK board. -
Replace the
app_id
andapp_key
values with your own.
validate.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import base64 import sys from urllib2 import Request, urlopen import json from pprint import pprint detected = "" with open('./image_vga.jpg', "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) # print(encoded_string) values = """ { "image":" """+encoded_string+""" " , "gallery_name": "KP" } """ headers = { 'Content-Type': 'application/json', 'app_id': 'cxx6a80a', 'app_key': 'ac4705bdac94ffxx686db9f8532b9453' } request = Request('https://api.kairos.com/recognize', data=values, headers=headers) response_body = urlopen(request).read() target = open('./Result.txt', 'w') target.truncate() target.write(response_body) target.write("\n") target.close() with open('./Result.txt') as data_file: data = json.load(data_file) target = open('./Detection.txt', 'w') target.truncate() # print(str(data)) if str(data).find("Errors") == -1: target.write(data["images"][0]["transaction"]["status"]) detected = data["images"][0]["transaction"]["status"] # target.write(data["Errors"][0]["Message"]) else: target.write("no face detected") detected = "no face detected" target.write("\n") target.close() print(detected) |
Try it out: Copy a group image you want to validate to your ARTIK board under the name image_vga.jpg
containing the person from your enrolled photo along with others. Then run:
python ./validate.py
Was it able to identify your known person?
Next step: Build a project board with camera to take pictures and send them to Kairos for enrollment and validation.