diff --git a/.gitignore b/.gitignore index 7daf24a..fadaca5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ deepface/commons/__pycache__/* deepface/basemodels/__pycache__/* deepface/extendedmodels/__pycache__/* deepface/subsidiarymodels/__pycache__/* +deepface/detectors/__pycache__/* tests/dataset/*.pkl .DS_Store deepface/.DS_Store diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index a8eb95f..0d44d03 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -774,7 +774,7 @@ def stream(db_path = '', model_name ='VGG-Face', distance_metric = 'cosine', ena realtime.analysis(db_path, model_name, distance_metric, enable_face_analysis , source = source, time_threshold = time_threshold, frame_threshold = frame_threshold) -def detectFace(img_path, detector_backend = 'mtcnn'): +def detectFace(img_path, detector_backend = 'mtcnn', enforce_detection = True): """ This function applies pre-processing stages of a face recognition pipeline including detection and alignment @@ -790,7 +790,8 @@ def detectFace(img_path, detector_backend = 'mtcnn'): functions.initialize_detector(detector_backend = detector_backend) - img = functions.preprocess_face(img = img_path, detector_backend = detector_backend)[0] #preprocess_face returns (1, 224, 224, 3) + img = functions.preprocess_face(img = img_path, detector_backend = detector_backend + , enforce_detection = enforce_detection)[0] #preprocess_face returns (1, 224, 224, 3) return img[:, :, ::-1] #bgr to rgb #--------------------------- diff --git a/deepface/commons/functions.py b/deepface/commons/functions.py index 59abde9..c993cf4 100644 --- a/deepface/commons/functions.py +++ b/deepface/commons/functions.py @@ -2,18 +2,10 @@ import os import numpy as np import pandas as pd import cv2 -from pathlib import Path -import gdown -import hashlib -import math -from PIL import Image -import copy import base64 -import multiprocessing -import subprocess -import bz2 -from deepface.commons import distance -from mtcnn import MTCNN #0.1.0 +from pathlib import Path + +from deepface.detectors import FaceDetector import tensorflow as tf tf_version = int(tf.__version__.split(".")[0]) @@ -52,92 +44,7 @@ def initialize_input(img1_path, img2_path = None): def initialize_detector(detector_backend): global face_detector - - home = str(Path.home()) - - #eye detector is common for opencv and ssd - if detector_backend == 'opencv' or detector_backend == 'ssd': - opencv_path = get_opencv_path() - eye_detector_path = opencv_path+"haarcascade_eye.xml" - - if os.path.isfile(eye_detector_path) != True: - raise ValueError("Confirm that opencv is installed on your environment! Expected path ",eye_detector_path," violated.") - - global eye_detector - eye_detector = cv2.CascadeClassifier(eye_detector_path) - - #------------------------------ - #face detectors - if detector_backend == 'opencv': - opencv_path = get_opencv_path() - face_detector_path = opencv_path+"haarcascade_frontalface_default.xml" - - if os.path.isfile(face_detector_path) != True: - raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.") - - face_detector = cv2.CascadeClassifier(face_detector_path) - - elif detector_backend == 'ssd': - - #check required ssd model exists in the home/.deepface/weights folder - - #model structure - if os.path.isfile(home+'/.deepface/weights/deploy.prototxt') != True: - - print("deploy.prototxt will be downloaded...") - - url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt" - - output = home+'/.deepface/weights/deploy.prototxt' - - gdown.download(url, output, quiet=False) - - #pre-trained weights - if os.path.isfile(home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel') != True: - - print("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...") - - url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel" - - output = home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel' - - gdown.download(url, output, quiet=False) - - face_detector = cv2.dnn.readNetFromCaffe( - home+"/.deepface/weights/deploy.prototxt", - home+"/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel" - ) - - elif detector_backend == 'dlib': - import dlib #this is not a must library within deepface. that's why, I didn't put this import to a global level. version: 19.20.0 - - global sp - - face_detector = dlib.get_frontal_face_detector() - - #check required file exists in the home/.deepface/weights folder - if os.path.isfile(home+'/.deepface/weights/shape_predictor_5_face_landmarks.dat') != True: - - print("shape_predictor_5_face_landmarks.dat.bz2 is going to be downloaded") - - url = "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2" - output = home+'/.deepface/weights/'+url.split("/")[-1] - - gdown.download(url, output, quiet=False) - - zipfile = bz2.BZ2File(output) - data = zipfile.read() - newfilepath = output[:-4] #discard .bz2 extension - open(newfilepath, 'wb').write(data) - - sp = dlib.shape_predictor(home+"/.deepface/weights/shape_predictor_5_face_landmarks.dat") - - elif detector_backend == 'mtcnn': - face_detector = MTCNN() - - elif detector_backend == 'retinaface': - from retinaface import RetinaFace - face_detector = RetinaFace.build_model() + face_detector = FaceDetector.build_model(detector_backend) def initializeFolder(): @@ -157,16 +64,6 @@ def loadBase64Img(uri): img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) return img -def get_opencv_path(): - opencv_home = cv2.__file__ - folders = opencv_home.split(os.path.sep)[0:-1] - - path = folders[0] - for folder in folders[1:]: - path = path + "/" + folder - - return path+"/data/" - def load_image(img): exact_image = False @@ -192,280 +89,25 @@ def load_image(img): def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_detection = True): - home = str(Path.home()) - img_region = [0, 0, img.shape[0], img.shape[1]] #if functions.preproces_face is called directly, then face_detector global variable might not been initialized. if not "face_detector" in globals(): initialize_detector(detector_backend = detector_backend) - if detector_backend == 'opencv': - - faces = [] - - try: - faces = face_detector.detectMultiScale(img, 1.3, 5) - except: - pass - - if len(faces) > 0: - x,y,w,h = faces[0] #focus on the 1st face found in the image - detected_face = img[int(y):int(y+h), int(x):int(x+w)] - return detected_face, [x, y, w, h] - - else: #if no face detected - - if enforce_detection != True: - return img, img_region - - else: - raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") - - elif detector_backend == 'ssd': - - ssd_labels = ["img_id", "is_face", "confidence", "left", "top", "right", "bottom"] - - target_size = (300, 300) - - base_img = img.copy() #we will restore base_img to img later - - original_size = img.shape - - img = cv2.resize(img, target_size) - - aspect_ratio_x = (original_size[1] / target_size[1]) - aspect_ratio_y = (original_size[0] / target_size[0]) - - imageBlob = cv2.dnn.blobFromImage(image = img) - - face_detector.setInput(imageBlob) - detections = face_detector.forward() - - detections_df = pd.DataFrame(detections[0][0], columns = ssd_labels) - - detections_df = detections_df[detections_df['is_face'] == 1] #0: background, 1: face - detections_df = detections_df[detections_df['confidence'] >= 0.90] - - detections_df['left'] = (detections_df['left'] * 300).astype(int) - detections_df['bottom'] = (detections_df['bottom'] * 300).astype(int) - detections_df['right'] = (detections_df['right'] * 300).astype(int) - detections_df['top'] = (detections_df['top'] * 300).astype(int) - - if detections_df.shape[0] > 0: - - #TODO: sort detections_df - - #get the first face in the image - instance = detections_df.iloc[0] - - left = instance["left"] - right = instance["right"] - bottom = instance["bottom"] - top = instance["top"] - - detected_face = base_img[int(top*aspect_ratio_y):int(bottom*aspect_ratio_y), int(left*aspect_ratio_x):int(right*aspect_ratio_x)] - - return detected_face, [int(left*aspect_ratio_x), int(top*aspect_ratio_y), int(right*aspect_ratio_x) - int(left*aspect_ratio_x), int(bottom*aspect_ratio_y) - int(top*aspect_ratio_y)] - - else: #if no face detected - - if enforce_detection != True: - img = base_img.copy() - return img, img_region - - else: - raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") - - elif detector_backend == 'dlib': - - detections = face_detector(img, 1) - - if len(detections) > 0: - - for idx, d in enumerate(detections): - left = d.left(); right = d.right() - top = d.top(); bottom = d.bottom() - - detected_face = img[top:bottom, left:right] - - return detected_face, [left, top, right - left, bottom - top] - - else: #if no face detected - - if enforce_detection != True: - return img, img_region - - else: - raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") - - elif detector_backend == 'mtcnn': - - img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #mtcnn expects RGB but OpenCV read BGR - detections = face_detector.detect_faces(img_rgb) - - if len(detections) > 0: - detection = detections[0] - x, y, w, h = detection["box"] - detected_face = img[int(y):int(y+h), int(x):int(x+w)] - return detected_face, [x, y, w, h] - - else: #if no face detected - if not enforce_detection: - return img, img_region - - else: - raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") - - elif detector_backend == 'retinaface': - img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #retinaface expects RGB but OpenCV read BGR - - from retinaface import RetinaFace - faces = RetinaFace.extract_faces(img_rgb, align = True) - - if len(faces) > 0: - face = faces[0] - return face, img_region - - else: #if no face detected - if not enforce_detection: - return img, img_region - else: - raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") - + detected_face, img_region = FaceDetector.detect_face(face_detector, detector_backend, img) + if (isinstance(detected_face, np.ndarray)): + return detected_face, img_region else: - detectors = ['opencv', 'ssd', 'dlib', 'mtcnn'] - raise ValueError("Valid backends are ", detectors," but you passed ", detector_backend) - -def alignment_procedure(img, left_eye, right_eye): - - #this function aligns given face in img based on left and right eye coordinates - - left_eye_x, left_eye_y = left_eye - right_eye_x, right_eye_y = right_eye - - #----------------------- - #find rotation direction - - if left_eye_y > right_eye_y: - point_3rd = (right_eye_x, left_eye_y) - direction = -1 #rotate same direction to clock - else: - point_3rd = (left_eye_x, right_eye_y) - direction = 1 #rotate inverse direction of clock - - #----------------------- - #find length of triangle edges - - a = distance.findEuclideanDistance(np.array(left_eye), np.array(point_3rd)) - b = distance.findEuclideanDistance(np.array(right_eye), np.array(point_3rd)) - c = distance.findEuclideanDistance(np.array(right_eye), np.array(left_eye)) - - #----------------------- - - #apply cosine rule - - if b != 0 and c != 0: #this multiplication causes division by zero in cos_a calculation - - cos_a = (b*b + c*c - a*a)/(2*b*c) - angle = np.arccos(cos_a) #angle in radian - angle = (angle * 180) / math.pi #radian to degree - - #----------------------- - #rotate base image - - if direction == -1: - angle = 90 - angle - - img = Image.fromarray(img) - img = np.array(img.rotate(direction * angle)) - - #----------------------- - - return img #return img anyway - -def align_face(img, detector_backend = 'opencv'): - - home = str(Path.home()) - - if (detector_backend == 'opencv') or (detector_backend == 'ssd'): - - detected_face_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #eye detector expects gray scale image - - eyes = eye_detector.detectMultiScale(detected_face_gray) - - if len(eyes) >= 2: - - #find the largest 2 eye - - base_eyes = eyes[:, 2] - - items = [] - for i in range(0, len(base_eyes)): - item = (base_eyes[i], i) - items.append(item) - - df = pd.DataFrame(items, columns = ["length", "idx"]).sort_values(by=['length'], ascending=False) - - eyes = eyes[df.idx.values[0:2]] #eyes variable stores the largest 2 eye - - #----------------------- - #decide left and right eye - - eye_1 = eyes[0]; eye_2 = eyes[1] - - if eye_1[0] < eye_2[0]: - left_eye = eye_1; right_eye = eye_2 + if detected_face == None: + if enforce_detection != True: + return img, img_region else: - left_eye = eye_2; right_eye = eye_1 - - #----------------------- - #find center of eyes - - left_eye = (int(left_eye[0] + (left_eye[2] / 2)), int(left_eye[1] + (left_eye[3] / 2))) - right_eye = (int(right_eye[0] + (right_eye[2]/2)), int(right_eye[1] + (right_eye[3]/2))) - - img = alignment_procedure(img, left_eye, right_eye) - - return img #return img anyway - - elif detector_backend == 'dlib': - - import dlib #this is not a must dependency in deepface - - detections = face_detector(img, 1) - - if len(detections) > 0: - detected_face = detections[0] - img_shape = sp(img, detected_face) - img = dlib.get_face_chip(img, img_shape, size = img.shape[0]) - - return img #return img anyway - - elif detector_backend == 'mtcnn': - - img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #mtcnn expects RGB but OpenCV read BGR - detections = face_detector.detect_faces(img_rgb) - - if len(detections) > 0: - detection = detections[0] - - keypoints = detection["keypoints"] - left_eye = keypoints["left_eye"] - right_eye = keypoints["right_eye"] - - img = alignment_procedure(img, left_eye, right_eye) - - return img #return img anyway - elif detector_backend == 'retinaface': - #we used extract_faces function of retinaface. it applies alignment anyway. - return img #return img anyway + raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.") def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_detection = True, detector_backend = 'opencv', return_region = False): - #img_path = copy.copy(img) - #img might be path, base64 or numpy array. Convert it to numpy whatever it is. img = load_image(img) base_img = img.copy() @@ -474,10 +116,7 @@ def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_dete #-------------------------- - if img.shape[0] > 0 and img.shape[1] > 0: - img = align_face(img = img, detector_backend = detector_backend) - else: - + if img.shape[0] == 0 or img.shape[1] == 0: if enforce_detection == True: raise ValueError("Detected face shape is ", img.shape,". Consider to set enforce_detection argument to False.") else: #restore base image diff --git a/deepface/detectors/DlibWrapper.py b/deepface/detectors/DlibWrapper.py new file mode 100644 index 0000000..a23612b --- /dev/null +++ b/deepface/detectors/DlibWrapper.py @@ -0,0 +1,55 @@ +from pathlib import Path +import gdown +import bz2 +import os + +def build_model(): + + home = str(Path.home()) + + import dlib #this requirement is not a must that's why imported here + + #check required file exists in the home/.deepface/weights folder + if os.path.isfile(home+'/.deepface/weights/shape_predictor_5_face_landmarks.dat') != True: + + print("shape_predictor_5_face_landmarks.dat.bz2 is going to be downloaded") + + url = "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2" + output = home+'/.deepface/weights/'+url.split("/")[-1] + + gdown.download(url, output, quiet=False) + + zipfile = bz2.BZ2File(output) + data = zipfile.read() + newfilepath = output[:-4] #discard .bz2 extension + open(newfilepath, 'wb').write(data) + + face_detector = dlib.get_frontal_face_detector() + return face_detector + +def detect_face(face_detector, img): + + import dlib #this requirement is not a must that's why imported here + + home = str(Path.home()) + + sp = dlib.shape_predictor(home+"/.deepface/weights/shape_predictor_5_face_landmarks.dat") + + detected_face = None + img_region = [0, 0, img.shape[0], img.shape[1]] + + detections = face_detector(img, 1) + + if len(detections) > 0: + + for idx, d in enumerate(detections): + left = d.left(); right = d.right() + top = d.top(); bottom = d.bottom() + detected_face = img[top:bottom, left:right] + img_region = [left, top, right - left, bottom - top] + break #get the first one + + img_shape = sp(img, detections[0]) + detected_face = dlib.get_face_chip(img, img_shape, size = detected_face.shape[0]) + + return detected_face, img_region diff --git a/deepface/detectors/FaceDetector.py b/deepface/detectors/FaceDetector.py new file mode 100644 index 0000000..ff30f76 --- /dev/null +++ b/deepface/detectors/FaceDetector.py @@ -0,0 +1,96 @@ +from deepface.detectors import OpenCvWrapper, SsdWrapper, DlibWrapper, MtcnnWrapper, RetinaFaceWrapper +from PIL import Image +import math +import numpy as np +from deepface.commons import distance + +def build_model(detector_backend): + + if detector_backend == 'opencv': + face_detector = OpenCvWrapper.build_model() + + elif detector_backend == 'ssd': + face_detector = SsdWrapper.build_model() + + elif detector_backend == 'dlib': + face_detector = DlibWrapper.build_model() + + elif detector_backend == 'mtcnn': + face_detector = MtcnnWrapper.build_model() + + elif detector_backend == 'retinaface': + face_detector = RetinaFaceWrapper.build_model() + + else: + raise ValueError("invalid detector_backend passed - " + detector_backend) + + return face_detector + +def detect_face(face_detector, detector_backend, img): + + if detector_backend == 'opencv': + face, region = OpenCvWrapper.detect_face(face_detector, img) + + elif detector_backend == 'ssd': + face, region = SsdWrapper.detect_face(face_detector, img) + + elif detector_backend == 'dlib': + face, region = DlibWrapper.detect_face(face_detector, img) + + elif detector_backend == 'mtcnn': + face, region = MtcnnWrapper.detect_face(face_detector, img) + + elif detector_backend == 'retinaface': + face, region = RetinaFaceWrapper.detect_face(face_detector, img) + + else: + raise ValueError("invalid detector_backend passed - " + detector_backend) + + return face, region + +def alignment_procedure(img, left_eye, right_eye): + + #this function aligns given face in img based on left and right eye coordinates + + left_eye_x, left_eye_y = left_eye + right_eye_x, right_eye_y = right_eye + + #----------------------- + #find rotation direction + + if left_eye_y > right_eye_y: + point_3rd = (right_eye_x, left_eye_y) + direction = -1 #rotate same direction to clock + else: + point_3rd = (left_eye_x, right_eye_y) + direction = 1 #rotate inverse direction of clock + + #----------------------- + #find length of triangle edges + + a = distance.findEuclideanDistance(np.array(left_eye), np.array(point_3rd)) + b = distance.findEuclideanDistance(np.array(right_eye), np.array(point_3rd)) + c = distance.findEuclideanDistance(np.array(right_eye), np.array(left_eye)) + + #----------------------- + + #apply cosine rule + + if b != 0 and c != 0: #this multiplication causes division by zero in cos_a calculation + + cos_a = (b*b + c*c - a*a)/(2*b*c) + angle = np.arccos(cos_a) #angle in radian + angle = (angle * 180) / math.pi #radian to degree + + #----------------------- + #rotate base image + + if direction == -1: + angle = 90 - angle + + img = Image.fromarray(img) + img = np.array(img.rotate(direction * angle)) + + #----------------------- + + return img #return img anyway diff --git a/deepface/detectors/MtcnnWrapper.py b/deepface/detectors/MtcnnWrapper.py new file mode 100644 index 0000000..41981ed --- /dev/null +++ b/deepface/detectors/MtcnnWrapper.py @@ -0,0 +1,29 @@ +from mtcnn import MTCNN +import cv2 +from deepface.detectors import FaceDetector + +def build_model(): + face_detector = MTCNN() + return face_detector + +def detect_face(face_detector, img): + + detected_face = None + img_region = [0, 0, img.shape[0], img.shape[1]] + + img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #mtcnn expects RGB but OpenCV read BGR + detections = face_detector.detect_faces(img_rgb) + + if len(detections) > 0: + detection = detections[0] + x, y, w, h = detection["box"] + detected_face = img[int(y):int(y+h), int(x):int(x+w)] + img_region = [x, y, w, h] + + keypoints = detection["keypoints"] + left_eye = keypoints["left_eye"] + right_eye = keypoints["right_eye"] + + detected_face = FaceDetector.alignment_procedure(detected_face, left_eye, right_eye) + + return detected_face, img_region diff --git a/deepface/detectors/OpenCvWrapper.py b/deepface/detectors/OpenCvWrapper.py new file mode 100644 index 0000000..a759030 --- /dev/null +++ b/deepface/detectors/OpenCvWrapper.py @@ -0,0 +1,97 @@ +import cv2 +import os +import pandas as pd +from deepface.detectors import FaceDetector + +def build_model(model_name = 'haarcascade'): + opencv_path = get_opencv_path() + + if model_name == 'haarcascade': + + face_detector_path = opencv_path+"haarcascade_frontalface_default.xml" + + if os.path.isfile(face_detector_path) != True: + raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.") + + + face_detector = cv2.CascadeClassifier(face_detector_path) + return face_detector + elif model_name == 'haarcascade_eye': + eye_detector_path = opencv_path+"haarcascade_eye.xml" + + if os.path.isfile(eye_detector_path) != True: + raise ValueError("Confirm that opencv is installed on your environment! Expected path ",eye_detector_path," violated.") + + eye_detector = cv2.CascadeClassifier(eye_detector_path) + return eye_detector + + +def detect_face(face_detector, img): + + detected_face = None + img_region = [0, 0, img.shape[0], img.shape[1]] + + faces = [] + try: + faces = face_detector.detectMultiScale(img, 1.3, 5) + except: + pass + + if len(faces) > 0: + x,y,w,h = faces[0] #focus on the 1st face found in the image + detected_face = img[int(y):int(y+h), int(x):int(x+w)] + + detected_face = align_face(detected_face) + img_region = [x, y, w, h] + + return detected_face, img_region + +def align_face(img): + + eye_detector = build_model(model_name = 'haarcascade_eye') + + detected_face_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #eye detector expects gray scale image + + eyes = eye_detector.detectMultiScale(detected_face_gray) + + if len(eyes) >= 2: + + #find the largest 2 eye + + base_eyes = eyes[:, 2] + + items = [] + for i in range(0, len(base_eyes)): + item = (base_eyes[i], i) + items.append(item) + + df = pd.DataFrame(items, columns = ["length", "idx"]).sort_values(by=['length'], ascending=False) + + eyes = eyes[df.idx.values[0:2]] #eyes variable stores the largest 2 eye + + #----------------------- + #decide left and right eye + + eye_1 = eyes[0]; eye_2 = eyes[1] + + if eye_1[0] < eye_2[0]: + left_eye = eye_1; right_eye = eye_2 + else: + left_eye = eye_2; right_eye = eye_1 + + #----------------------- + #find center of eyes + left_eye = (int(left_eye[0] + (left_eye[2] / 2)), int(left_eye[1] + (left_eye[3] / 2))) + right_eye = (int(right_eye[0] + (right_eye[2]/2)), int(right_eye[1] + (right_eye[3]/2))) + img = FaceDetector.alignment_procedure(img, left_eye, right_eye) + return img #return img anyway + +def get_opencv_path(): + opencv_home = cv2.__file__ + folders = opencv_home.split(os.path.sep)[0:-1] + + path = folders[0] + for folder in folders[1:]: + path = path + "/" + folder + + return path+"/data/" diff --git a/deepface/detectors/RetinaFaceWrapper.py b/deepface/detectors/RetinaFaceWrapper.py new file mode 100644 index 0000000..2cdcf97 --- /dev/null +++ b/deepface/detectors/RetinaFaceWrapper.py @@ -0,0 +1,20 @@ +from retinaface import RetinaFace +import cv2 + +def build_model(): + face_detector = RetinaFace.build_model() + return face_detector + +def detect_face(face_detector, img): + + face = None + img_region = [0, 0, img.shape[0], img.shape[1]] + + img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #retinaface expects RGB but OpenCV read BGR + + faces = RetinaFace.extract_faces(img_rgb, align = True) + + if len(faces) > 0: + face = faces[0][:, :, ::-1] + + return face, img_region diff --git a/deepface/detectors/SsdWrapper.py b/deepface/detectors/SsdWrapper.py new file mode 100644 index 0000000..17a0c14 --- /dev/null +++ b/deepface/detectors/SsdWrapper.py @@ -0,0 +1,92 @@ +import gdown +from pathlib import Path +import os +import cv2 +import pandas as pd + +from deepface.detectors import OpenCvWrapper + +def build_model(): + + home = str(Path.home()) + + #model structure + if os.path.isfile(home+'/.deepface/weights/deploy.prototxt') != True: + + print("deploy.prototxt will be downloaded...") + + url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt" + + output = home+'/.deepface/weights/deploy.prototxt' + + gdown.download(url, output, quiet=False) + + #pre-trained weights + if os.path.isfile(home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel') != True: + + print("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...") + + url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel" + + output = home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel' + + gdown.download(url, output, quiet=False) + + face_detector = cv2.dnn.readNetFromCaffe( + home+"/.deepface/weights/deploy.prototxt", + home+"/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel" + ) + + return face_detector + +def detect_face(face_detector, img): + + detected_face = None + img_region = [0, 0, img.shape[0], img.shape[1]] + + ssd_labels = ["img_id", "is_face", "confidence", "left", "top", "right", "bottom"] + + target_size = (300, 300) + + base_img = img.copy() #we will restore base_img to img later + + original_size = img.shape + + img = cv2.resize(img, target_size) + + aspect_ratio_x = (original_size[1] / target_size[1]) + aspect_ratio_y = (original_size[0] / target_size[0]) + + imageBlob = cv2.dnn.blobFromImage(image = img) + + face_detector.setInput(imageBlob) + detections = face_detector.forward() + + detections_df = pd.DataFrame(detections[0][0], columns = ssd_labels) + + detections_df = detections_df[detections_df['is_face'] == 1] #0: background, 1: face + detections_df = detections_df[detections_df['confidence'] >= 0.90] + + detections_df['left'] = (detections_df['left'] * 300).astype(int) + detections_df['bottom'] = (detections_df['bottom'] * 300).astype(int) + detections_df['right'] = (detections_df['right'] * 300).astype(int) + detections_df['top'] = (detections_df['top'] * 300).astype(int) + + if detections_df.shape[0] > 0: + + #TODO: sort detections_df + + #get the first face in the image + instance = detections_df.iloc[0] + + left = instance["left"] + right = instance["right"] + bottom = instance["bottom"] + top = instance["top"] + + detected_face = base_img[int(top*aspect_ratio_y):int(bottom*aspect_ratio_y), int(left*aspect_ratio_x):int(right*aspect_ratio_x)] + img_region = [int(left*aspect_ratio_x), int(top*aspect_ratio_y), int(right*aspect_ratio_x) - int(left*aspect_ratio_x), int(bottom*aspect_ratio_y) - int(top*aspect_ratio_y)] + + detected_face = OpenCvWrapper.align_face(detected_face) + + return detected_face, img_region diff --git a/tests/unit_tests.py b/tests/unit_tests.py index c5b0b0d..080ba95 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -166,7 +166,8 @@ dataset = [ ['dataset/img6.jpg', 'dataset/img9.jpg', False], ] -models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace'] +#models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace'] +models = ['VGG-Face', 'Facenet', 'Dlib', 'ArcFace'] #those are robust models metrics = ['cosine', 'euclidean', 'euclidean_l2'] passed_tests = 0; test_cases = 0