Skip to content

Commit

Permalink
raising legible error message
Browse files Browse the repository at this point in the history
some libraries are optional. if an user tries to import these
before installation, then not legible message was gotten.
now, we will throw legible message.
  • Loading branch information
serengil committed Dec 8, 2023
1 parent c1f60ba commit fff6481
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
29 changes: 18 additions & 11 deletions deepface/detectors/FastMtcnnWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
# Link -> https://github.com/timesler/facenet-pytorch
# Examples https://www.kaggle.com/timesler/guide-to-mtcnn-in-facenet-pytorch


def build_model():
# Optional dependency
# this is not a must dependency. do not import it in the global level.
try:
from facenet_pytorch import MTCNN as fast_mtcnn
except ModuleNotFoundError as e:
raise ImportError("This is an optional detector, ensure the library is installed. \
Please install using 'pip install facenet-pytorch' ") from e

raise ImportError(
"FastMtcnn is an optional detector, ensure the library is installed."
"Please install using 'pip install facenet-pytorch' "
) from e

face_detector = fast_mtcnn(image_size=160,
thresholds=[0.6, 0.7, 0.7], # MTCNN thresholds
post_process=True,
device='cpu',
select_largest=False, # return result in descending order
)
face_detector = fast_mtcnn(
image_size=160,
thresholds=[0.6, 0.7, 0.7], # MTCNN thresholds
post_process=True,
device="cpu",
select_largest=False, # return result in descending order
)
return face_detector


def xyxy_to_xywh(xyxy):
"""
Convert xyxy format to xywh format.
Expand All @@ -30,6 +34,7 @@ def xyxy_to_xywh(xyxy):
h = xyxy[3] - y + 1
return [x, y, w, h]


def detect_face(face_detector, img, align=True):

resp = []
Expand All @@ -38,7 +43,9 @@ def detect_face(face_detector, img, align=True):
img_region = [0, 0, img.shape[1], img.shape[0]]

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # mtcnn expects RGB but OpenCV read BGR
detections = face_detector.detect(img_rgb, landmarks=True) # returns boundingbox, prob, landmark
detections = face_detector.detect(
img_rgb, landmarks=True
) # returns boundingbox, prob, landmark
if len(detections[0]) > 0:

for detection in zip(*detections):
Expand Down
9 changes: 8 additions & 1 deletion deepface/detectors/MediapipeWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@


def build_model():
import mediapipe as mp # this is not a must dependency. do not import it in the global level.
# this is not a must dependency. do not import it in the global level.
try:
import mediapipe as mp
except ModuleNotFoundError as e:
raise ImportError(
"MediaPipe is an optional detector, ensure the library is installed."
"Please install using 'pip install mediapipe' "
) from e

mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.7)
Expand Down
20 changes: 14 additions & 6 deletions deepface/detectors/YoloWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ def build_model():
import os

# Import the Ultralytics YOLO model
from ultralytics import YOLO
try:
from ultralytics import YOLO
except ModuleNotFoundError as e:
raise ImportError(
"Yolo is an optional detector, ensure the library is installed. \
Please install using 'pip install ultralytics' "
) from e

from deepface.commons.functions import get_deepface_home

weight_path = f"{get_deepface_home()}{PATH}"

# Download the model's weights if they don't exist
Expand All @@ -38,8 +45,7 @@ def detect_face(face_detector, img, align=False):
resp = []

# Detect faces
results = face_detector.predict(
img, verbose=False, show=False, conf=0.25)[0]
results = face_detector.predict(img, verbose=False, show=False, conf=0.25)[0]

# For each face, extract the bounding box, the landmarks and confidence
for result in results:
Expand All @@ -48,7 +54,7 @@ def detect_face(face_detector, img, align=False):
confidence = result.boxes.conf.tolist()[0]

x, y, w, h = int(x - w / 2), int(y - h / 2), int(w), int(h)
detected_face = img[y: y + h, x: x + w].copy()
detected_face = img[y : y + h, x : x + w].copy()

if align:
# Tuple of x,y and confidence for left eye
Expand All @@ -57,8 +63,10 @@ def detect_face(face_detector, img, align=False):
right_eye = result.keypoints.xy[0][1], result.keypoints.conf[0][1]

# Check the landmarks confidence before alignment
if (left_eye[1] > LANDMARKS_CONFIDENCE_THRESHOLD and
right_eye[1] > LANDMARKS_CONFIDENCE_THRESHOLD):
if (
left_eye[1] > LANDMARKS_CONFIDENCE_THRESHOLD
and right_eye[1] > LANDMARKS_CONFIDENCE_THRESHOLD
):
detected_face = FaceDetector.alignment_procedure(
detected_face, left_eye[0].cpu(), right_eye[0].cpu()
)
Expand Down

0 comments on commit fff6481

Please sign in to comment.