from fastapi import APIRouter, UploadFile import cv2 import numpy as np import uuid from app.core.detector import detect_faces from app.core.embedder import get_embedding from app.core.matcher import match_embedding from app.storage import save_raw_photo, save_processed_photo router = APIRouter() @router.post("/upload") async def upload_photo(file: UploadFile): # Leggi immagine img_bytes = await file.read() img = np.frombuffer(img_bytes, np.uint8) img = cv2.imdecode(img, cv2.IMREAD_COLOR) # Salva raw filename = f"{uuid.uuid4().hex}.jpg" save_raw_photo(filename, img) # Detection boxes = detect_faces(img) results = [] # Disegna bounding box processed = img.copy() for box in boxes: x1, y1, x2, y2 = box face = img[y1:y2, x1:x2] emb = get_embedding(face) match = match_embedding(emb) # Disegna box color = (0, 255, 0) if match else (0, 0, 255) cv2.rectangle(processed, (x1, y1), (x2, y2), color, 2) if match: cv2.putText(processed, match["name"], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2) results.append({ "box": box, "match": match }) # Salva immagine processata save_processed_photo(filename, processed) return { "file": filename, "faces": results }