3/9/2023
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
#!/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.
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)
import requests
import json
import sys
HEADERS = {
"token": 'XXXXXXXXXXXXXXXXXX'
}
def add_face_to_service(headers, image_path, image_name):
url = "https://api.luxand.cloud/subject/v2"
data = {
"name": image_name
}
files = {
"photo": open(image_path, "rb")
}
response = requests.post(url, headers=headers, data=data, files=files)
result = json.loads(response.text)
if response.ok:
print(f"Ok adding face: {result['id']}")
return result['id']
else:
print(f"Error adding face: {result['error']}")
return None
def recognize_face_from_file(headers, image_path):
url = "https://api.luxand.cloud/photo/search"
files = {
"photo": open(image_path, "rb")
}
response = requests.post(url, headers=headers, files=files)
result = json.loads(response.text)
if response.ok:
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']}")
if __name__ == '__main__':
#Step1 - Prepare our image database to search faces within it
file_to_base_path = 'base_file.png'
file_to_base_name = 'Mr.Test'
id = add_face_to_service(HEADERS, file_to_base_path, file_to_base_name)
if not id:
sys.exit()
#Step2 - Recognize particular face from image file
file_to_recognize_path = 'recognize_file.png'
recognize_face_from_file(HEADERS, file_to_recognize_path)
sys.exit()
Replace path_to_image_for_recognition with the actual image file path for recognition.