-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #865 from haddyadnan/facenet_pytorch
include faster implementation of mtcnn
- Loading branch information
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import cv2 | ||
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 | ||
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 | ||
|
||
|
||
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. | ||
""" | ||
x, y = xyxy[0], xyxy[1] | ||
w = xyxy[2] - x + 1 | ||
h = xyxy[3] - y + 1 | ||
return [x, y, w, h] | ||
|
||
def detect_face(face_detector, img, align=True): | ||
|
||
resp = [] | ||
|
||
detected_face = None | ||
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 | ||
if len(detections[0]) > 0: | ||
|
||
for detection in zip(*detections): | ||
x, y, w, h = xyxy_to_xywh(detection[0]) | ||
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)] | ||
img_region = [x, y, w, h] | ||
confidence = detection[1] | ||
|
||
if align: | ||
left_eye = detection[2][0] | ||
right_eye = detection[2][1] | ||
detected_face = FaceDetector.alignment_procedure(detected_face, left_eye, right_eye) | ||
|
||
resp.append((detected_face, img_region, confidence)) | ||
|
||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
opencv-contrib-python>=4.3.0.36 | ||
mediapipe>=0.8.7.3 | ||
dlib>=19.20.0 | ||
ultralytics>=8.0.122 | ||
ultralytics>=8.0.122 | ||
facenet-pytorch>=2.5.3 |