face region fixes

This commit is contained in:
Sefik Serengil 2021-02-17 20:18:04 +03:00
parent 8af418d052
commit 97be78b004
4 changed files with 21 additions and 32 deletions

3
.gitignore vendored
View File

@ -14,5 +14,4 @@ deepface/commons/__pycache__/*
deepface/basemodels/__pycache__/* deepface/basemodels/__pycache__/*
deepface/extendedmodels/__pycache__/* deepface/extendedmodels/__pycache__/*
deepface/subsidiarymodels/__pycache__/* deepface/subsidiarymodels/__pycache__/*
tests/dataset/*.pkl tests/dataset/*.pkl
Age_Gender_Retail_Analysis.ipynb

View File

@ -282,12 +282,7 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race']
The function returns a dictionary. If img_path is a list, then it will return list of dictionary. The function returns a dictionary. If img_path is a list, then it will return list of dictionary.
{ {
"region": { "region": {'x': 230, 'y': 120, 'w': 36, 'h': 45},
'x': 230,
'y': 120,
'w': 36,
'h': 45
}
"age": 28.66, "age": 28.66,
"gender": "woman", "gender": "woman",
"dominant_emotion": "neutral", "dominant_emotion": "neutral",
@ -370,7 +365,6 @@ def analyze(img_path, actions = ['emotion', 'age', 'gender', 'race']
img_224 = None # Set to prevent re-detection img_224 = None # Set to prevent re-detection
region = [] # x, y, w, h of the detected face region region = [] # x, y, w, h of the detected face region
region_labels = ['x', 'y', 'w', 'h'] region_labels = ['x', 'y', 'w', 'h']
#facial attribute analysis #facial attribute analysis

View File

@ -190,6 +190,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
home = str(Path.home()) 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 functions.preproces_face is called directly, then face_detector global variable might not been initialized.
if not "face_detector" in globals(): if not "face_detector" in globals():
initialize_detector(detector_backend = detector_backend) initialize_detector(detector_backend = detector_backend)
@ -211,7 +213,7 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
else: #if no face detected else: #if no face detected
if enforce_detection != True: if enforce_detection != True:
return img return img, img_region
else: 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.") 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.")
@ -266,7 +268,7 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
if enforce_detection != True: if enforce_detection != True:
img = base_img.copy() img = base_img.copy()
return img return img, img_region
else: 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.") 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.")
@ -288,7 +290,7 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
else: #if no face detected else: #if no face detected
if enforce_detection != True: if enforce_detection != True:
return img return img, img_region
else: 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.") 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.")
@ -306,7 +308,7 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
else: #if no face detected else: #if no face detected
if not enforce_detection: if not enforce_detection:
return img return img, img_region
else: 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.") 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.")

View File

@ -21,6 +21,9 @@ if tf_version == 2:
print("Running unit tests for TF ", tf.__version__) print("Running unit tests for TF ", tf.__version__)
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
from deepface.extendedmodels import Age, Gender, Race, Emotion
#----------------------------------------- #-----------------------------------------
dataset = [ dataset = [
@ -200,27 +203,10 @@ else:
raise ValueError("Unit test score does not satisfy the minimum required accuracy. Minimum expected score is ",threshold,"% but this got ",accuracy,"%") raise ValueError("Unit test score does not satisfy the minimum required accuracy. Minimum expected score is ",threshold,"% but this got ",accuracy,"%")
#----------------------------------- #-----------------------------------
# api tests - already built models will be passed to the functions
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
#----------------------------------- #-----------------------------------
print("--------------------------")
print("Verify function with passing pre-trained model")
vggface_model = VGGFace.loadModel()
resp_obj = DeepFace.verify("dataset/img1.jpg", "dataset/img2.jpg", model_name = "VGG-Face", model = vggface_model)
print(resp_obj)
#-----------------------------------
print("--------------------------")
print("Analyze function with passing pre-trained model") print("Analyze function with passing pre-trained model")
from deepface.extendedmodels import Age, Gender, Race, Emotion
emotion_model = Emotion.loadModel() emotion_model = Emotion.loadModel()
age_model = Age.loadModel() age_model = Age.loadModel()
gender_model = Gender.loadModel() gender_model = Gender.loadModel()
@ -257,7 +243,7 @@ for i in range(0, len(dataset)):
#----------------------------------- #-----------------------------------
print("--------------------------") print("--------------------------")
print("Pre-trained ensemble method") print("Pre-trained ensemble method - find")
from deepface import DeepFace from deepface import DeepFace
from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace
@ -272,10 +258,18 @@ print("OpenFace loaded")
model["DeepFace"] = FbDeepFace.loadModel() model["DeepFace"] = FbDeepFace.loadModel()
print("DeepFace loaded") print("DeepFace loaded")
df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model=model, enforce_detection=False) df = DeepFace.find("dataset/img1.jpg", db_path = "dataset", model_name = 'Ensemble', model = model, enforce_detection=False)
print(df) print(df)
#-----------------------------------
print("--------------------------")
print("Pre-trained ensemble method - verify")
res = DeepFace.verify(dataset, model_name = "Ensemble", model = model)
print(res)
#-----------------------------------
print("--------------------------") print("--------------------------")
import cv2 import cv2