clean code refactoring

This commit is contained in:
Sefik Ilkin Serengil 2023-01-29 21:39:54 +00:00
parent 029f3dfa43
commit 2d39ade138
5 changed files with 255 additions and 262 deletions

View File

@ -26,9 +26,6 @@ from deepface.basemodels import (
SFace,
)
from deepface.extendedmodels import Age, Gender, Race, Emotion
from deepface.extendedmodels.Emotion import EMOTION_LABELS
from deepface.extendedmodels.Gender import GENDER_LABELS
from deepface.extendedmodels.Race import RACE_LABELS
from deepface.commons import functions, realtime, distance as dst
# -----------------------------------
@ -342,11 +339,11 @@ def analyze(
obj["emotion"] = {}
for i, emotion_label in enumerate(EMOTION_LABELS):
for i, emotion_label in enumerate(Emotion.labels):
emotion_prediction = 100 * emotion_predictions[i] / sum_of_predictions
obj["emotion"][emotion_label] = emotion_prediction
obj["dominant_emotion"] = EMOTION_LABELS[np.argmax(emotion_predictions)]
obj["dominant_emotion"] = Emotion.labels[np.argmax(emotion_predictions)]
elif action == "age":
age_predictions = models["age"].predict(img_content, verbose=0)[0, :]
@ -357,22 +354,22 @@ def analyze(
elif action == "gender":
gender_predictions = models["gender"].predict(img_content, verbose=0)[0, :]
obj["gender"] = {}
for i, gender_label in enumerate(GENDER_LABELS):
for i, gender_label in enumerate(Gender.labels):
gender_prediction = 100 * gender_predictions[i]
obj["gender"][gender_label] = gender_prediction
obj["dominant_gender"] = GENDER_LABELS[np.argmax(gender_predictions)]
obj["dominant_gender"] = Gender.labels[np.argmax(gender_predictions)]
elif action == "race":
race_predictions = models["race"].predict(img_content, verbose=0)[0, :]
sum_of_predictions = race_predictions.sum()
obj["race"] = {}
for i, race_label in enumerate(RACE_LABELS):
for i, race_label in enumerate(Race.labels):
race_prediction = 100 * race_predictions[i] / sum_of_predictions
obj["race"][race_label] = race_prediction
obj["dominant_race"] = RACE_LABELS[np.argmax(race_predictions)]
obj["dominant_race"] = Race.labels[np.argmax(race_predictions)]
# -----------------------------
# mention facial areas

View File

@ -4,7 +4,7 @@ import numpy as np
import pandas as pd
import cv2
from deepface import DeepFace
from deepface.commons import functions, distance as dst
from deepface.commons import functions
# dependency configuration
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
@ -30,7 +30,6 @@ def analysis(
enable_age_gender = True
# ------------------------
# find custom values for this input set
threshold = dst.findThreshold(model_name, distance_metric)
target_size = functions.find_target_size(model_name=model_name)
# ------------------------
# build models once to store them in the memory
@ -423,8 +422,7 @@ def analysis(
if df.shape[0] > 0:
candidate = df.iloc[0]
label = candidate["identity"]
best_distance = candidate[f"{model_name}_{distance_metric}"]
if best_distance <= threshold:
# to use this source image as is
display_img = cv2.imread(label)
# to use extracted face

View File

@ -24,6 +24,9 @@ elif tf_version == 2:
)
# -------------------------------------------
# Labels for the emotions that can be detected by the model.
labels = ["angry", "disgust", "fear", "happy", "sad", "surprise", "neutral"]
def loadModel(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facial_expression_model_weights.h5",
@ -70,7 +73,3 @@ def loadModel(
model.load_weights(home + "/.deepface/weights/facial_expression_model_weights.h5")
return model
EMOTION_LABELS = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
"""Labels for the emotions that can be detected by the model."""

View File

@ -18,6 +18,11 @@ elif tf_version == 2:
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Convolution2D, Flatten, Activation
# -------------------------------------
# Labels for the genders that can be detected by the model.
labels = ["Woman", "Man"]
def loadModel(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/gender_model_weights.h5",
):
@ -51,9 +56,3 @@ def loadModel(
gender_model.load_weights(home + "/.deepface/weights/gender_model_weights.h5")
return gender_model
# --------------------------
GENDER_LABELS = ["Woman", "Man"]
"""Labels for the genders that can be detected by the model."""

View File

@ -17,6 +17,10 @@ elif tf_version == 2:
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Convolution2D, Flatten, Activation
# --------------------------
# Labels for the ethnic phenotypes that can be detected by the model.
labels = ["asian", "indian", "black", "white", "middle eastern", "latino hispanic"]
def loadModel(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/race_model_single_batch.h5",
):
@ -50,7 +54,3 @@ def loadModel(
race_model.load_weights(home + "/.deepface/weights/race_model_single_batch.h5")
return race_model
RACE_LABELS = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
"""Labels for the ethnic phenotypes that can be detected by the model."""