| from fastapi import FastAPI, File, UploadFile
|
| import face_recognition
|
| import pickle
|
| from fastapi.responses import JSONResponse
|
| import numpy as np
|
|
|
| app = FastAPI()
|
|
|
|
|
| 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)
|
|
|
|
|
| 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}
|
|
|