Spaces:
Build error
Build error
| 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"] | |
| 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} | |