In this article, we will demonstrate how to use the Luxand.cloud Face Recognition API with Python. We will show you how to add faces to a database, and then call the recognition method to identify them.
Setting up the environment
First, you need to create an account on Luxand.cloud and obtain an API key. Install the required libraries by running the following command in your terminal:
pip install requests
Adding faces to the database
Create a Python file and import the required libraries:
#!/usr/bin/env python3
import requests
import json
Define a function to add faces to the database:
def add_face(api_key, image_path, name):
url = "https://api.luxand.cloud/subject/v2"
headers = {"token": api_key}
data = {"name": name}
files = {"photo": open(image_path, "rb")}
response = requests.post(url, headers=headers, data=data, files=files)
result = json.loads(response.text)
if response.status_code == 200:
print(f"Face added: {name}, ID: {result['id']}")
return result['id']
else:
print(f"Error adding face: {result['error']}")
api_key = "your_api_key"
image_path = "path_to_image"
name = "person_name"
subject_id = add_face(api_key, image_path, name)
Replace your_api_key, path_to_image, and person_name with your actual API key, image file path, and the person's name, respectively.
Recognizing faces
Define a function to recognize faces:
def recognize_face(api_key, image_path):
url = "https://api.luxand.cloud/photo/search"
headers = {"token": api_key}
files = {"photo": open(image_path, "rb")}
response = requests.post(url, headers=headers, files=files)
result = json.loads(response.text)
if response.status_code == 200:
if result['status'] == "success":
print(f"Recognized face: {result['name']}, ID: {result['id']}, Confidence: {result['confidence']}")
else:
print("No match found")
else:
print(f"Error recognizing face: {result['error']}")
image_path_recognition = "path_to_image_for_recognition"
recognize_face(api_key, image_path_recognition)
Replace path_to_image_for_recognition with the actual image file path for recognition.
Conclusion
This article demonstrated how to use Luxand.cloud Face Recognition API with Python.
We showed you how to add faces to a database and call the recognition method to identify them. This can be used in various applications, such as security systems, access control, and more.