Spaces:
Build error
Build error
File size: 1,060 Bytes
24c5686 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from fastapi import FastAPI, File, UploadFile
import face_recognition
import pickle
from fastapi.responses import JSONResponse
import numpy as np
app = FastAPI()
# Load known face encodings and names
with open('encodings.pickle', 'rb') as f:
data = pickle.load(f)
known_encodings = data["encodings"]
known_names = data["names"]
@app.post("/recognize/")
async def recognize_face(file: UploadFile = File(...)):
contents = await file.read()
image = face_recognition.load_image_file(contents)
encodings = face_recognition.face_encodings(image)
if not encodings:
return JSONResponse(content={"error": "No faces found in the image"}, status_code=400)
# Compare with known encodings
matches = face_recognition.compare_faces(known_encodings, encodings[0])
matched_names = []
if True in matches:
matched_indices = [i for i, match in enumerate(matches) if match]
matched_names = [known_names[i] for i in matched_indices]
return {"matched_names": matched_names}
|