RetinaFaceApi / app.py
Edvards's picture
detect faces
ee7103e
raw
history blame
1.02 kB
from fastapi import FastAPI, File, UploadFile
from retinaface import RetinaFace
import cv2
import numpy as np
from typing import Any, Dict
app = FastAPI()
def convert_to_python_types(obj: Any) -> Any:
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, dict):
return {key: convert_to_python_types(value) for key, value in obj.items()}
elif isinstance(obj, list):
return [convert_to_python_types(item) for item in obj]
else:
return obj
@app.post("/detect_faces")
async def detect_faces(file: UploadFile = File(...)):
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Detect faces
faces = RetinaFace.detect_faces(image)
return convert_to_python_types(faces)
@app.get("/")
def greet_json():
return {"Hello": "World!"}