From 5171484957618566ecdbb189c1f5f5f8076883c8 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Wed, 19 Jan 2022 12:07:57 +0300 Subject: [PATCH] simplified code --- deepface/detectors/MediapipeWrapper.py | 94 +++++++++----------------- 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/deepface/detectors/MediapipeWrapper.py b/deepface/detectors/MediapipeWrapper.py index bcdf3cf..5cce678 100644 --- a/deepface/detectors/MediapipeWrapper.py +++ b/deepface/detectors/MediapipeWrapper.py @@ -1,77 +1,49 @@ from deepface.detectors import FaceDetector - # Link - https://google.github.io/mediapipe/solutions/face_detection def build_model(): - import mediapipe as mp + import mediapipe as mp #this is not a must dependency. do not import it in the global level. mp_face_detection = mp.solutions.face_detection - # Build a face detector - # min_detection_confidence - "A filter to analyse the training photographs" - face_detection = mp_face_detection.FaceDetection( min_detection_confidence=0.6) + face_detection = mp_face_detection.FaceDetection( min_detection_confidence=0.7) return face_detection -def detect_face(face_detector, img, align=True): - import mediapipe as mp - import re - #mp_face_detection = mp.solutions.face_detection - #mp_drawing = mp.solutions.drawing_utils +def detect_face(face_detector, img, align = True): + import mediapipe as mp #this is not a must dependency. do not import it in the global level. resp = [] + + img_width = img.shape[1]; img_height = img.shape[0] + results = face_detector.process(img) - original_size = img.shape - target_size = (300, 300) - # First face , than eye - #print(results.detections) - if results.detections: + + if results.detections: for detection in results.detections: - #mp_drawing.draw_detection(img, detection) - #print(detection) - # detected_face is the cropped image that is then passed forward to the Regognizer - ''' - DETECTION - - Collection of detected faces, where each face is represented as a detection proto message that contains - a bounding box and 6 key points (right eye, left eye, nose tip, mouth center, right ear tragion, and left - ear tragion). The bounding box is composed of xmin and width (both normalized to [0.0, 1.0] by the - image width) and ymin and height (both normalized to [0.0, 1.0] by the image height). Each key point - is composed of x and y, which are normalized to [0.0, 1.0] by the image width and height - respectively. - ''' - # Bounding Box - x = re.findall('xmin: (..*)',str(detection)) - y = re.findall('ymin: (..*)',str(detection)) - h = re.findall('height: (..*)',str(detection)) - w = re.findall('width: (..*)',str(detection)) - # Eye Locations - reye_x = re.findall('x: (..*)',str(detection))[0] - leye_x = re.findall('x: (..*)',str(detection))[1] - reye_y = re.findall('y: (..*)', str(detection))[0] - leye_y = re.findall('y: (..*)', str(detection))[1] - # Detections are normalized by the mediapipe API, thus they need to be multiplied - # Extra tweaking done to improve accuracy - x = (float(x[0]) * original_size[1]) - y = (float(y[0]) * original_size[0]-15) - h = (float(h[0]) * original_size[0]+10) - w = (float(w[0]) * original_size[1]+10) - reye_x = (float(reye_x) * original_size[1]) - leye_x = (float(leye_x) * original_size[1]) - reye_y = (float(reye_y) * original_size[0]) - leye_y = (float(leye_y) * original_size[0]) - if float(x) and float(y) > 0: - detected_face = img[int(y):int(y + h), int(x):int(x + w)] - img_region = [int(x), int(y), int(w), int(h)] + + confidence = detection.score + + bounding_box = detection.location_data.relative_bounding_box + landmarks = detection.location_data.relative_keypoints + + x = int(bounding_box.xmin * img_width) + w = int(bounding_box.width * img_width) + y = int(bounding_box.ymin * img_height) + h = int(bounding_box.height * img_height) + + right_eye = (int(landmarks[0].x * img_width), int(landmarks[0].y * img_height)) + left_eye = (int(landmarks[1].x * img_width), int(landmarks[1].y * img_height)) + #nose = (int(landmarks[2].x * img_width), int(landmarks[2].y * img_height)) + #mouth = (int(landmarks[3].x * img_width), int(landmarks[3].y * img_height)) + #right_ear = (int(landmarks[4].x * img_width), int(landmarks[4].y * img_height)) + #left_ear = (int(landmarks[5].x * img_width), int(landmarks[5].y * img_height)) + + if x > 0 and y > 0: + detected_face = img[y:y+h, x:x+w] + img_region = [x, y, w, h] + if align: - left_eye=(leye_x,leye_y) - right_eye=(reye_x,reye_y) - #print(left_eye) - #print(right_eye) detected_face = FaceDetector.alignment_procedure(detected_face, left_eye, right_eye) + resp.append((detected_face,img_region)) - else: - continue - - #print("Yahoo") + return resp - - -#face_detector = FaceDetector.build_model('mediapipe')