mirror of
https://github.com/serengil/deepface.git
synced 2025-06-06 11:35:21 +00:00
raising legible error message
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.
This commit is contained in:
parent
c1f60ba3d6
commit
fff6481431
@ -4,23 +4,27 @@ from deepface.detectors import FaceDetector
|
||||
# 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.
|
||||
@ -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 = []
|
||||
@ -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):
|
||||
|
@ -4,7 +4,14 @@ from deepface.detectors import FaceDetector
|
||||
|
||||
|
||||
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)
|
||||
|
@ -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
|
||||
@ -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:
|
||||
@ -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
|
||||
@ -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()
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user