DEV Community

Cover image for How I Implemented Facial Recognition using Python
Matt
Matt

Posted on

How I Implemented Facial Recognition using Python

I recently implemented Luxand.cloud's face recognition API into my project, and it's been working great. Below you'll find the steps how to integrate it.

Setting up the environment

Install the required libraries by running the following command in your terminal:

pip3 install requests
Enter fullscreen mode Exit fullscreen mode

Adding people to the database

Create a Python file and import the necessary libraries:

#!/usr/bin/env python3

import requests
import json

API_TOKEN = "c9d85df66aba4f1085682a53b00a0cbf"
Define a function to the person to the database:

def add_person(name, image_path, collections = ""):
    if image_path.startswith("https://"):
        files = {"photos": image_path}
    else:
        files = {"photos": open(image_path, "rb")}

    response = requests.post(
        url="https://api.luxand.cloud/v2/person",
        headers={"token": API_TOKEN},
        data={"name": name, "store": "1", "collections": collections},
        files=files,
    )

    if response.status_code == 200:
        person = response.json()

        print("Added person", name, "with UUID", person["uuid"])
        return person["uuid"]
    else:
        print("Can't add person", name, ":", response.text)
        return None
Now you can add people to the database one by one:

person_name = "person name"

# path to image can be local file name or URL
path_to_image = "path_to_image"

# enter the collection name to create the collection and add person to it
collection_name = ""

person_uuid = add_person(person_name, path_to_image, collection_name)
Improving accuracy of recognition
If you upload more than one image of a person, the face recognition engine will be able to recognize people with better accuracy. To do that, create a function that can add faces to a person.

def add_face(person_uuid, image_path):
    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}


    response = requests.post(
        url="https://api.luxand.cloud/v2/person/%s" % person_uuid,
        headers={"token": API_TOKEN},
        data={"store": "1"},
        files=files
    )
Enter fullscreen mode Exit fullscreen mode

Now, add more photos to this person to improve face recognition accuracy. While having a single image of a person is acceptable, adding 3-5 more images will significantly improve face recognition accuracy.

add_face(person_uuid, "path_to_another_image")
Enter fullscreen mode Exit fullscreen mode

Recognizing faces

def recognize_face(image_path):
    url = "https://api.luxand.cloud/photo/search/v2"
    headers = {"token": API_TOKEN}

    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}

    response = requests.post(url, headers=headers, files=files)
    result = json.loads(response.text)

    if response.status_code == 200:
        return response.json()
    else:
        print("Can't recognize people:", response.text)
        return None


image_path_recognition = "path_to_image_for_recognition"
recognize_face(image_path_recognition)

Enter fullscreen mode Exit fullscreen mode

Replace the path_to_image_for_recognition with the actual image file path for recognition.

Complete code

Here you can find the complete version of the code I used above. You can just copy and paste it into your file, replace parameters, and it will work.

#!/usr/bin/env python3
import requests
import json

API_TOKEN = "c9d85df66aba4f1085682a53b00a0cbf"

# This function adds a person to the database
def add_person(name, image_path, collections = ""):
    if image_path.startswith("https://"):
        files = {"photos": image_path}
    else:
        files = {"photos": open(image_path, "rb")}

    response = requests.post(
        url="https://api.luxand.cloud/v2/person",
        headers={"token": API_TOKEN},
        data={"name": name, "store": "1", "collections": collections},
        files=files,
    )

    if response.status_code == 200:
        person = response.json()

        print("Added person", name, "with UUID", person["uuid"])
        return person["uuid"]
    else:
        print("Can't add person", name, ":", response.text)
        return None


# This function recognizes a face in an image
def recognize_face(image_path):
    url = "https://api.luxand.cloud/photo/search/v2"
    headers = {"token": API_TOKEN}

    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}

    response = requests.post(url, headers=headers, files=files)
    result = json.loads(response.text)

    if response.status_code == 200:
        return response.json()
    else:
        print("Can't recognize people:", response.text)
        return None


if __name__ == "__main__":
    # Adding a person
    person_uuid = add_person("John Smith", "face1.jpg", "Employees")
    if person_uuid == None:
        exit(1)

    # Recognizing a face
    recognized_people = recognize_face("face2.jpg")

    print("Recognized people:", recognized_people)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)