init
Browse files- __pycache__/handler.cpython-310.pyc +0 -0
- handler.py +28 -0
- requirements.txt +3 -0
- test.py +0 -0
__pycache__/handler.cpython-310.pyc
ADDED
|
Binary file (1.52 kB). View file
|
|
|
handler.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 9 |
+
|
| 10 |
+
class EndpointHandler():
|
| 11 |
+
def __init__(self, path=""):
|
| 12 |
+
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 13 |
+
self.mtcnn = MTCNN(device=self.device)
|
| 14 |
+
self.resnet = InceptionResnetV1(pretrained='vggface2', device=self.device).eval()
|
| 15 |
+
|
| 16 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 17 |
+
imageData = data['image']
|
| 18 |
+
image = Image.open(BytesIO(base64.b64decode(imageData)))
|
| 19 |
+
face_batch = self.mtcnn([image])
|
| 20 |
+
face_batch = [i for i in face_batch if i is not None]
|
| 21 |
+
if face_batch:
|
| 22 |
+
aligned = torch.stack(face_batch)
|
| 23 |
+
if self.device.type == "cuda":
|
| 24 |
+
aligned = aligned.to(self.device)
|
| 25 |
+
|
| 26 |
+
embeddings = self.resnet(aligned).detach().cpu()
|
| 27 |
+
return embeddings.tolist()
|
| 28 |
+
else: return None
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
facenet-pytorch
|
| 2 |
+
torch
|
| 3 |
+
datasets
|
test.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|