forked from tinkerhub/toast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (48 loc) · 1.99 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
from typing import List
import torch
from fastapi import FastAPI, File, Body
from facenet_pytorch import MTCNN, InceptionResnetV1
from pydantic import BaseModel
from utils import get_faces
from db.operations import (
create_embeddings_table,
insert_embedding,
find_most_similar_embedding,
delete_user
)
app = FastAPI()
# Initialize the MTCNN face detection model and InceptionResnetV1 face recognition model
mtcnn = MTCNN()
resnet = InceptionResnetV1(pretrained='vggface2').eval()
create_embeddings_table()
class UserResponse(BaseModel):
user_id: str
@app.post("/image_to_user", response_model=List[str])
async def image_to_user(image: bytes = File(...)):
# Extract faces from the image
faces = get_faces(image)
user_ids = []
for face in faces:
# Convert the face data from a numpy array to a PyTorch tensor
face_tensor = torch.from_numpy(face).permute(2, 0, 1).float()
# Compute the embedding for the face using the InceptionResnetV1 model
embedding = resnet(face_tensor.unsqueeze(0)).detach().numpy().reshape(-1)
most_similar_id = find_most_similar_embedding(embedding)
user_ids.append(UserResponse(user_id=most_similar_id))
return user_ids
@app.post("/user", response_model=UserResponse)
async def create_user(image: bytes = File(...), user_id: str = Body(...)):
# Extract faces from the image
faces = get_faces(image)
for face in faces:
# Convert the face data from a numpy array to a PyTorch tensor
face_tensor = torch.from_numpy(face).permute(2, 0, 1).float()
# Compute the embedding for the face using the InceptionResnetV1 model
embedding = resnet(face_tensor.unsqueeze(0)).detach().squeeze().numpy()
insert_embedding(embedding, user_id)
# Your code to create the user profile
return {"user_id": user_id}
@app.delete("/user/{user_id}", response_model=UserResponse)
async def delete_user(user_id: str):
delete_user(user_id)
return {"user_id": user_id}