Testys commited on
Commit
b36b72b
1 Parent(s): 4cf1daa

Testing out FastAPI for FaceEmbeddings extraction

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -0
  2. main.py +73 -0
  3. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10.12
2
+
3
+ COPY . .
4
+
5
+ WORKDIR /
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
8
+
9
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
10
+
main.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ from fastapi.param_functions import File
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from typing import List
6
+ import io
7
+ from facenet_pytorch import MTCNN, InceptionResnetV1
8
+ import torch
9
+ import io
10
+ from PIL import Image
11
+
12
+ app = FastAPI()
13
+
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"],
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
23
+
24
+ mtcnn = MTCNN(keep_all=True, device=device)
25
+ resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
26
+
27
+
28
+ @app.get("/", tags=["Home"])
29
+ def read_root():
30
+ return {"message": "Welcome to the face embeddings API!"}
31
+
32
+ @app.get("/health", tags=["Health"])
33
+ def health_check():
34
+ return {"status": "ok"}
35
+
36
+
37
+
38
+ @app.post("/extract", tags=["Extract Embeddings"])
39
+ async def extract_embeddings(file: UploadFile = File(...)):
40
+ # Load the image
41
+ contents = await file.read()
42
+ image = Image.open(io.BytesIO(contents)).convert('RGB')
43
+
44
+ # Preprocess the image
45
+ preprocessed_image = mtcnn(image)
46
+
47
+ # Extract the face embeddings
48
+ embeddings = resnet(preprocessed_image.unsqueeze(0)).detach().cpu().numpy().tolist()
49
+
50
+ return JSONResponse(content={"embeddings": embeddings})
51
+
52
+
53
+ # @app.post("/extract")
54
+ # async def extract_embeddings(file: UploadFile = File(...)):
55
+ # # Load the image
56
+ # contents = await file.read()
57
+ # image = face_recognition.load_image_file(io.BytesIO(contents))
58
+
59
+ # # Find all the faces in the image
60
+ # face_locations = face_recognition.face_locations(image)
61
+
62
+ # # Initialize an empty list to store the face embeddings
63
+ # embeddings = []
64
+
65
+ # # Loop through each face location
66
+ # for face_location in face_locations:
67
+ # # Extract the face encoding
68
+ # face_encoding = face_recognition.face_encodings(image, [face_location])[0]
69
+
70
+ # # Append the face encoding to the embeddings list
71
+ # embeddings.append(face_encoding.tolist())
72
+
73
+ # return JSONResponse(content={"embeddings": embeddings})
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ facenet_pytorch==2.5.2
2
+ torch==1.8.1
3
+ torchvision==0.9.1
4
+ numpy==1.19.5
5
+ pandas==1.2.4
6
+ Pillow==8.2.0
7
+ fastapi
8
+ uvicorn
9
+ python-multipart