Update services/facial_processing.py
Browse files
services/facial_processing.py
CHANGED
@@ -2,7 +2,6 @@ import numpy as np
|
|
2 |
import os
|
3 |
import torch
|
4 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
5 |
-
import cv2
|
6 |
import logging
|
7 |
|
8 |
logger = logging.getLogger(__name__)
|
@@ -10,8 +9,6 @@ logger = logging.getLogger(__name__)
|
|
10 |
|
11 |
class FacialProcessing:
|
12 |
def __init__(self):
|
13 |
-
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
14 |
-
self.model = cv2.dnn.readNetFromTorch('openface.nn4.small2.v1.t7')
|
15 |
# Set the cache directory to a writable location
|
16 |
os.environ['TORCH_HOME'] = '/tmp/.cache/torch'
|
17 |
|
@@ -21,33 +18,6 @@ class FacialProcessing:
|
|
21 |
self.resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
22 |
|
23 |
|
24 |
-
def extract_embeddings(self, image_path):
|
25 |
-
try:
|
26 |
-
image = cv2.imread(image_path)
|
27 |
-
if image is None:
|
28 |
-
logger.error(f"Failed to load image: {image_path}")
|
29 |
-
return None
|
30 |
-
|
31 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
32 |
-
faces = self.face_cascade.detectMultiScale(gray, 1.3, 5)
|
33 |
-
|
34 |
-
if len(faces) == 0:
|
35 |
-
logger.warning(f"No face detected in image: {image_path}")
|
36 |
-
return None
|
37 |
-
|
38 |
-
(x, y, w, h) = faces[0]
|
39 |
-
face = image[y:y+h, x:x+w]
|
40 |
-
|
41 |
-
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False)
|
42 |
-
self.model.setInput(faceBlob)
|
43 |
-
vec = self.model.forward()
|
44 |
-
|
45 |
-
return vec.flatten().tolist()
|
46 |
-
except Exception as e:
|
47 |
-
logger.error(f"An error occurred while extracting embeddings: {e}")
|
48 |
-
return None
|
49 |
-
|
50 |
-
|
51 |
def extract_embeddings_vgg(self, image):
|
52 |
try:
|
53 |
# Preprocess the image
|
|
|
2 |
import os
|
3 |
import torch
|
4 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
|
|
5 |
import logging
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
|
|
9 |
|
10 |
class FacialProcessing:
|
11 |
def __init__(self):
|
|
|
|
|
12 |
# Set the cache directory to a writable location
|
13 |
os.environ['TORCH_HOME'] = '/tmp/.cache/torch'
|
14 |
|
|
|
18 |
self.resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
19 |
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def extract_embeddings_vgg(self, image):
|
22 |
try:
|
23 |
# Preprocess the image
|