import requests
import base64
BASE_URL = "https://stg.kyc.legaltalent.ai"
TOKEN = "YOUR_TOKEN"
headers = {
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
}
def encode_image(file_path):
"""Encode image file to base64."""
with open(file_path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
def verify_faces(source_path, target_path, threshold=0.7):
"""Verify if two face images match."""
response = requests.post(
f"{BASE_URL}/kyc/facematch/verify",
headers=headers,
json={
"source_image": encode_image(source_path),
"target_image": encode_image(target_path),
"threshold": threshold,
"image_format": "base64"
}
)
return response.json()
def register_face(image_path, session_id, metadata=None):
"""Register a face in the collection."""
response = requests.post(
f"{BASE_URL}/kyc/facematch/register",
headers=headers,
json={
"image": encode_image(image_path),
"session_id": session_id,
"metadata": metadata or {},
"image_format": "base64"
}
)
return response.json()
def search_face(image_path, threshold=0.8, max_results=10):
"""Search for matching faces in collection."""
response = requests.post(
f"{BASE_URL}/kyc/facematch/search",
headers=headers,
json={
"image": encode_image(image_path),
"threshold": threshold,
"max_results": max_results,
"image_format": "base64"
}
)
return response.json()
# Example usage
if __name__ == "__main__":
# 1:1 Verification
result = verify_faces("id_photo.jpg", "selfie.jpg", threshold=0.7)
print(f"Match: {result['data']['is_match']}")
print(f"Similarity: {result['data']['similarity_score']}")
# Register face
register_result = register_face(
"id_photo.jpg",
"sess_123456",
metadata={"full_name": "John Doe", "document_type": "passport"}
)
print(f"Face ID: {register_result['data']['face_id']}")
# Search for face
search_result = search_face("new_photo.jpg", threshold=0.85)
print(f"Found {search_result['data']['total_matches']} matches")
for match in search_result['data']['matches']:
if match['is_blacklisted']:
print(f"⚠️ BLACKLISTED: {match['face_id']} - {match['blacklist_reason']}")
else:
print(f"Match: {match['face_id']} ({match['similarity']:.2%})")