Merge pull request #346 from blueprintparadise/master

Add MediapipeWrapper as a detector
This commit is contained in:
Sefik Ilkin Serengil 2022-01-12 21:47:24 +03:00 committed by GitHub
commit 12f34aa53f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 4 deletions

View File

@ -1,4 +1,4 @@
from deepface.detectors import OpenCvWrapper, SsdWrapper, DlibWrapper, MtcnnWrapper, RetinaFaceWrapper
from deepface.detectors import OpenCvWrapper, SsdWrapper, DlibWrapper, MtcnnWrapper, RetinaFaceWrapper,MediapipeWrapper
from PIL import Image
import math
import numpy as np
@ -13,7 +13,8 @@ def build_model(detector_backend):
'ssd': SsdWrapper.build_model,
'dlib': DlibWrapper.build_model,
'mtcnn': MtcnnWrapper.build_model,
'retinaface': RetinaFaceWrapper.build_model
'retinaface': RetinaFaceWrapper.build_model,
'mediapipe': MediapipeWrapper.build_model
}
if not "face_detector_obj" in globals():
@ -50,7 +51,8 @@ def detect_faces(face_detector, detector_backend, img, align = True):
'ssd': SsdWrapper.detect_face,
'dlib': DlibWrapper.detect_face,
'mtcnn': MtcnnWrapper.detect_face,
'retinaface': RetinaFaceWrapper.detect_face
'retinaface': RetinaFaceWrapper.detect_face,
'mediapipe': MediapipeWrapper.detect_face
}
detect_face = backends.get(detector_backend)

View File

@ -0,0 +1,77 @@
from deepface.detectors import FaceDetector
# Link - https://google.github.io/mediapipe/solutions/face_detection
def build_model():
import mediapipe as mp
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)
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
resp = []
results = face_detector.process(img)
original_size = img.shape
target_size = (300, 300)
# First face , than eye
#print(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)]
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')

View File

@ -11,4 +11,5 @@ Flask>=1.1.2
mtcnn>=0.1.0
lightgbm>=2.3.1
dlib>=19.20.0
retina-face>=0.0.1
retina-face>=0.0.1
mediapipe>=0.8.7.3