mirror of
https://github.com/serengil/deepface.git
synced 2025-06-10 13:27:08 +00:00
align argument
This commit is contained in:
parent
73d1950133
commit
1812391a3c
@ -59,7 +59,7 @@ def build_model(model_name):
|
||||
else:
|
||||
raise ValueError('Invalid model_name passed - {}'.format(model_name))
|
||||
|
||||
def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
|
||||
def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn', align = True):
|
||||
|
||||
"""
|
||||
This function verifies an image pair is same person or different persons.
|
||||
@ -153,11 +153,13 @@ def verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric =
|
||||
#img_path, model_name = 'VGG-Face', model = None, enforce_detection = True, detector_backend = 'mtcnn'
|
||||
img1_representation = represent(img_path = img1_path
|
||||
, model_name = model_name, model = custom_model
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend
|
||||
, align = align)
|
||||
|
||||
img2_representation = represent(img_path = img2_path
|
||||
, model_name = model_name, model = custom_model
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend
|
||||
, align = align)
|
||||
|
||||
#----------------------
|
||||
#find distances between embeddings
|
||||
@ -458,7 +460,7 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race'] , models =
|
||||
|
||||
return resp_obj
|
||||
|
||||
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
|
||||
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'mtcnn', align = True):
|
||||
|
||||
"""
|
||||
This function applies verification several times and find an identity in a database
|
||||
@ -569,7 +571,8 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
||||
|
||||
representation = represent(img_path = employee
|
||||
, model_name = model_name, model = custom_model
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend
|
||||
, align = align)
|
||||
|
||||
instance.append(representation)
|
||||
|
||||
@ -610,7 +613,8 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
||||
|
||||
target_representation = represent(img_path = img_path
|
||||
, model_name = model_name, model = custom_model
|
||||
, enforce_detection = enforce_detection, detector_backend =detector_backend)
|
||||
, enforce_detection = enforce_detection, detector_backend = detector_backend
|
||||
, align = align)
|
||||
|
||||
for k in metric_names:
|
||||
distances = []
|
||||
@ -701,7 +705,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
||||
|
||||
return None
|
||||
|
||||
def represent(img_path, model_name = 'VGG-Face', model = None, enforce_detection = True, detector_backend = 'mtcnn'):
|
||||
def represent(img_path, model_name = 'VGG-Face', model = None, enforce_detection = True, detector_backend = 'mtcnn', align = True):
|
||||
|
||||
"""
|
||||
This function represents facial images as vectors.
|
||||
@ -737,7 +741,8 @@ def represent(img_path, model_name = 'VGG-Face', model = None, enforce_detection
|
||||
img = functions.preprocess_face(img = img_path
|
||||
, target_size=(input_shape_y, input_shape_x)
|
||||
, enforce_detection = enforce_detection
|
||||
, detector_backend = detector_backend)
|
||||
, detector_backend = detector_backend
|
||||
, align = align)
|
||||
|
||||
#represent
|
||||
embedding = model.predict(img)[0].tolist()
|
||||
|
@ -87,7 +87,7 @@ def load_image(img):
|
||||
|
||||
return img
|
||||
|
||||
def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_detection = True):
|
||||
def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_detection = True, align = True):
|
||||
|
||||
img_region = [0, 0, img.shape[0], img.shape[1]]
|
||||
|
||||
@ -95,7 +95,7 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
||||
if not "face_detector" in globals():
|
||||
initialize_detector(detector_backend = detector_backend)
|
||||
|
||||
detected_face, img_region = FaceDetector.detect_face(face_detector, detector_backend, img)
|
||||
detected_face, img_region = FaceDetector.detect_face(face_detector, detector_backend, img, align)
|
||||
|
||||
if (isinstance(detected_face, np.ndarray)):
|
||||
return detected_face, img_region
|
||||
@ -106,13 +106,13 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
||||
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.")
|
||||
|
||||
def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_detection = True, detector_backend = 'opencv', return_region = False):
|
||||
def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_detection = True, detector_backend = 'opencv', return_region = False, align = True):
|
||||
|
||||
#img might be path, base64 or numpy array. Convert it to numpy whatever it is.
|
||||
img = load_image(img)
|
||||
base_img = img.copy()
|
||||
|
||||
img, region = detect_face(img = img, detector_backend = detector_backend, grayscale = grayscale, enforce_detection = enforce_detection)
|
||||
img, region = detect_face(img = img, detector_backend = detector_backend, grayscale = grayscale, enforce_detection = enforce_detection, align = align)
|
||||
|
||||
#--------------------------
|
||||
|
||||
|
@ -32,7 +32,7 @@ def build_model():
|
||||
detector["sp"] = sp
|
||||
return detector
|
||||
|
||||
def detect_face(detector, img):
|
||||
def detect_face(detector, img, align = True):
|
||||
|
||||
import dlib #this requirement is not a must that's why imported here
|
||||
|
||||
@ -55,7 +55,8 @@ def detect_face(detector, img):
|
||||
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])
|
||||
if align:
|
||||
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
|
||||
|
@ -23,7 +23,7 @@ def build_model(detector_backend):
|
||||
|
||||
return face_detector
|
||||
|
||||
def detect_face(face_detector, detector_backend, img):
|
||||
def detect_face(face_detector, detector_backend, img, align = True):
|
||||
|
||||
backends = {
|
||||
'opencv': OpenCvWrapper.detect_face,
|
||||
@ -36,7 +36,7 @@ def detect_face(face_detector, detector_backend, img):
|
||||
detect_face = backends.get(detector_backend)
|
||||
|
||||
if detect_face:
|
||||
face, region = detect_face(face_detector, img)
|
||||
face, region = detect_face(face_detector, img, align)
|
||||
else:
|
||||
raise ValueError("invalid detector_backend passed - " + detector_backend)
|
||||
|
||||
|
@ -6,7 +6,7 @@ def build_model():
|
||||
face_detector = MTCNN()
|
||||
return face_detector
|
||||
|
||||
def detect_face(face_detector, img):
|
||||
def detect_face(face_detector, img, align = True):
|
||||
|
||||
detected_face = None
|
||||
img_region = [0, 0, img.shape[0], img.shape[1]]
|
||||
@ -24,6 +24,7 @@ def detect_face(face_detector, img):
|
||||
left_eye = keypoints["left_eye"]
|
||||
right_eye = keypoints["right_eye"]
|
||||
|
||||
detected_face = FaceDetector.alignment_procedure(detected_face, left_eye, right_eye)
|
||||
if align:
|
||||
detected_face = FaceDetector.alignment_procedure(detected_face, left_eye, right_eye)
|
||||
|
||||
return detected_face, img_region
|
||||
|
@ -35,7 +35,7 @@ def build_cascade(model_name = 'haarcascade'):
|
||||
eye_detector = cv2.CascadeClassifier(eye_detector_path)
|
||||
return eye_detector
|
||||
|
||||
def detect_face(detector, img):
|
||||
def detect_face(detector, img, align = True):
|
||||
|
||||
detected_face = None
|
||||
img_region = [0, 0, img.shape[0], img.shape[1]]
|
||||
@ -50,13 +50,14 @@ def detect_face(detector, img):
|
||||
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(detector["eye_detector"], detected_face)
|
||||
if align:
|
||||
detected_face = align_face(detector["eye_detector"], detected_face)
|
||||
img_region = [x, y, w, h]
|
||||
|
||||
return detected_face, img_region
|
||||
|
||||
def align_face(eye_detector, img):
|
||||
|
||||
|
||||
detected_face_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #eye detector expects gray scale image
|
||||
|
||||
eyes = eye_detector.detectMultiScale(detected_face_gray)
|
||||
|
@ -5,14 +5,14 @@ def build_model():
|
||||
face_detector = RetinaFace.build_model()
|
||||
return face_detector
|
||||
|
||||
def detect_face(face_detector, img):
|
||||
def detect_face(face_detector, img, align = True):
|
||||
|
||||
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, model = face_detector, align = True)
|
||||
faces = RetinaFace.extract_faces(img_rgb, model = face_detector, align = align)
|
||||
|
||||
if len(faces) > 0:
|
||||
face = faces[0][:, :, ::-1]
|
||||
|
@ -45,7 +45,7 @@ def build_model():
|
||||
|
||||
return detector
|
||||
|
||||
def detect_face(detector, img):
|
||||
def detect_face(detector, img, align = True):
|
||||
|
||||
detected_face = None
|
||||
img_region = [0, 0, img.shape[0], img.shape[1]]
|
||||
@ -94,6 +94,7 @@ def detect_face(detector, img):
|
||||
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(detector["eye_detector"], detected_face)
|
||||
if align:
|
||||
detected_face = OpenCvWrapper.align_face(detector["eye_detector"], detected_face)
|
||||
|
||||
return detected_face, img_region
|
||||
|
Loading…
x
Reference in New Issue
Block a user