-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
40 lines (32 loc) · 1005 Bytes
/
utils.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
from models.mtcnn import MTCNN
from models.inception_resnet_v1 import InceptionResnetV1
import torch
import os
import pickle
import cv2
import matplotlib.pyplot as plt
from PIL import Image
workers = 0 if os.name == 'nt' else 4
import warnings
warnings.filterwarnings("ignore")
# Load Models
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Running on device: {}'.format(device))
mtcnn = MTCNN(
image_size=160, margin=0, min_face_size=20,
thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
device=device
)
resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
# Get Embeddings
def get_embeddings(image):
name = image.split('/')[-1].split('.')[0]
try:
face = mtcnn(Image.open(image))
except:
print("Couldn't read the image ", name)
try:
return resnet(face.unsqueeze(0).to(device)).detach(), name
except Exception:
print("Couldn't get the embeddings of image ", name)
return None, None