-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (58 loc) · 2.13 KB
/
app.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from fastapi import FastAPI, File, UploadFile
import shutil
import os
import cv2
import face_recognition
import numpy as np
from fastapi.middleware.cors import CORSMiddleware
from utils.face_confidence.face_confidence import face_confidence
from utils.encode_faces.encode_faces import encode_faces
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://super-croquembouche-3f9cf3.netlify.app"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/face_recognition")
async def face_recognition_api(image: UploadFile = File(...)):
known_face_encodings, known_face_names = await encode_faces()
# Save the uploaded image to disk
filename = image.filename
with open(filename, "wb") as buffer:
shutil.copyfileobj(image.file, buffer)
# Load the image
frame = cv2.imread(filename)
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
confidence = '???'
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
confidence = face_confidence(face_distances[best_match_index])
face_names.append(f'{name}')
results = []
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
results.append({
"name": name,
"confidence": confidence,
# "top": top,
# "right": right,
# "bottom": bottom,
# "left": left
})
# Delete the uploaded image file
os.remove(filename)
return {"results": results}