Spaces:
Sleeping
Sleeping
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 | |
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) | |
def greet_json(): | |
return {"Hello": "World!"} |