How to Integrate Face Recognition API into a Python Application
Step-by-step Python tutorial for face recognition API integration — register faces, recognize people, verify identities, and detect liveness.
Setup
Install the requests library if you don't have it:
pip install requests
Set your API key:
import requestsAPI_KEY = "your-api-key-here"
BASE_URL = "https://faceapi.arsa.technology/api/v1"
HEADERS = {"x-key-secret": API_KEY}
Register a Face
def register_face(face_uid, image_path):response = requests.post(
f"{BASE_URL}/face_recognition/register_face",
headers={*HEADERS, "x-face-uid": face_uid},
files={"face_image": open(image_path, "rb")}
)
return response.json()
result = register_face("employee_001", "photos/alice.jpg")
print(result) # {"status": "success", "message": "employee_001 registered"}
Recognize a Face
def recognize_face(image_path):response = requests.post(
f"{BASE_URL}/face_recognition/recognize_face",
headers=HEADERS,
files={"face_image": open(image_path, "rb")}
)
return response.json()
result = recognize_face("photos/unknown.jpg")
for face in result.get("faces", []):
print(f"Identity: {face['recognition_uidresult']}")
print(f"Confidence: {face['recognition_confidence']}")
print(f"Real face: {face['passive_liveness']['is_real_face']}")
print(f"Age: {face.get('age')}, Gender: {face.get('gender')}")
Verify Two Faces (1:1)
def verify_faces(image1_path, image2_path):response = requests.post(
f"{BASE_URL}/face_recognition/validate_faces",
headers=HEADERS,
files={
"image1": open(image1_path, "rb"),
"image2": open(image2_path, "rb")
}
)
return response.json()
result = verify_faces("selfie.jpg", "id_photo.jpg")
print(f"Match: {result['match_result']}")
print(f"Similarity: {result['similarity_score']}")
Face Analytics (Age & Gender)
def analyze_face(image_path):response = requests.post(
f"{BASE_URL}/face_analytics",
headers=HEADERS,
files={"face_image": open(image_path, "rb")}
)
return response.json()
result = analyze_face("photo.jpg")
for face in result.get("faces", []):
print(f"Age: {face['age']}, Gender: {face['gender']}")
Error Handling
def safe_api_call(func, args):try:
result = func(*args)
if result.get("status") == "fail":
print(f"API error: {result.get('message')}")
return result
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
return None