Get started with FacePing's face recognition API using Python
First, create a new Python project and install the required dependencies:
mkdir faceping-python
cd faceping-python
pip install requests python-dotenv
Create a new file create_group.py
:
import os
import requests
from dotenv import load_dotenv
load_dotenv()
def create_group(group_name):
try:
response = requests.post(
f"https://api.faceping.ai/groups/{group_name}",
headers={
'Authorization': f'Bearer {os.getenv("FACEPING_API_KEY")}'
}
)
response.raise_for_status()
print('Group created successfully')
return response.json()
except Exception as error:
print('Error creating group:', error)
return None
# Usage
result = create_group('my-group')
print(result)
Create a new file upload_face.py
:
import os
import requests
from dotenv import load_dotenv
load_dotenv()
def upload_face(group_name, image_path):
try:
with open(image_path, 'rb') as image_file:
response = requests.post(
f"https://api.faceping.ai/groups/{group_name}/faces",
headers={
'Authorization': f'Bearer {os.getenv("FACEPING_API_KEY")}'
},
files={'image': image_file}
)
response.raise_for_status()
print('Face uploaded successfully')
return response.json()
except Exception as error:
print('Error uploading face:', error)
return None
# Usage
result = upload_face('my-group', 'path/to/face.jpg')
Create a new file search_faces.py
:
import os
import requests
from dotenv import load_dotenv
load_dotenv()
def search_faces(group_name, image_path):
try:
with open(image_path, 'rb') as image_file:
response = requests.post(
f"https://api.faceping.ai/groups/{group_name}/search",
headers={
'Authorization': f'Bearer {os.getenv("FACEPING_API_KEY")}'
},
files={'image': image_file}
)
response.raise_for_status()
data = response.json()
matches = data.get('matches', [])
print(f'Found {len(matches)} matches')
return matches
except Exception as error:
print('Error searching faces:', error)
return []
# Usage
matches = search_faces('my-group', 'path/to/search_face.jpg')
for match in matches:
print(f"Match Score: {match['score']}")