Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-task-654-use-custom-logger #907

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
cd tests && python -m pytest unit_tests.py -s --disable-warnings

lint:
python -m pylint deepface/ --fail-under=10
21 changes: 14 additions & 7 deletions deepface/DeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
)
from deepface.extendedmodels import Age, Gender, Race, Emotion
from deepface.commons import functions, realtime, distance as dst
from deepface.commons.logger import Logger

logger = Logger(module="DeepFace")

# -----------------------------------
# configurations for dependencies
Expand Down Expand Up @@ -340,7 +343,11 @@ def analyze(
if img_content.shape[0] > 0 and img_content.shape[1] > 0:
obj = {}
# facial attribute analysis
pbar = tqdm(range(0, len(actions)), desc="Finding actions", disable=silent)
pbar = tqdm(
range(0, len(actions)),
desc="Finding actions",
disable=silent if len(actions) > 1 else True,
)
for index in pbar:
action = actions[index]
pbar.set_description(f"Action: {action}")
Expand Down Expand Up @@ -461,8 +468,8 @@ def find(
if path.exists(db_path + "/" + file_name):

if not silent:
print(
f"WARNING: Representations for images in {db_path} folder were previously stored"
logger.warn(
f"Representations for images in {db_path} folder were previously stored"
+ f" in {file_name}. If you added new instances after the creation, then please "
serengil marked this conversation as resolved.
Show resolved Hide resolved
+ "delete this file and call find function again. It will create it again."
)
Expand All @@ -471,7 +478,7 @@ def find(
representations = pickle.load(f)

if not silent:
print("There are ", len(representations), " representations found in ", file_name)
logger.info(f"There are {len(representations)} representations found in {file_name}")

else: # create representation.pkl from scratch
employees = []
Expand Down Expand Up @@ -539,7 +546,7 @@ def find(
pickle.dump(representations, f)

if not silent:
print(
logger.info(
f"Representations stored in {db_path}/{file_name} file."
+ "Please delete this file when you add new identities in your database."
)
Expand Down Expand Up @@ -614,7 +621,7 @@ def find(
toc = time.time()

if not silent:
print("find function lasts ", toc - tic, " seconds")
logger.info(f"find function lasts {toc - tic} seconds")

return resp_obj

Expand Down Expand Up @@ -869,7 +876,7 @@ def detectFace(
detected and aligned face as numpy array

"""
print("⚠️ Function detectFace is deprecated. Use extract_faces instead.")
logger.info("⚠️ Function detectFace is deprecated. Use extract_faces instead.")
serengil marked this conversation as resolved.
Show resolved Hide resolved
face_objs = extract_faces(
img_path=img_path,
target_size=target_size,
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.ArcFace")

# pylint: disable=unsubscriptable-object

Expand Down Expand Up @@ -71,7 +74,7 @@ def loadModel(

if os.path.isfile(output) != True:

print(file_name, " will be downloaded to ", output)
logger.info(f"{file_name} will be downloaded to {output}")
gdown.download(url, output, quiet=False)

# ---------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.DeepID")

tf_version = int(tf.__version__.split(".", maxsplit=1)[0])

Expand Down Expand Up @@ -71,7 +74,7 @@ def loadModel(
home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/deepid_keras_weights.h5") != True:
print("deepid_keras_weights.h5 will be downloaded...")
logger.info("deepid_keras_weights.h5 will be downloaded...")

output = home + "/.deepface/weights/deepid_keras_weights.h5"
gdown.download(url, output, quiet=False)
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/DlibResNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import gdown
import numpy as np
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.DlibResNet")

# pylint: disable=too-few-public-methods

Expand All @@ -24,7 +27,7 @@ def __init__(self):

# download pre-trained model if it does not exist
if os.path.isfile(weight_file) != True:
print("dlib_face_recognition_resnet_model_v1.dat is going to be downloaded")
logger.info("dlib_face_recognition_resnet_model_v1.dat is going to be downloaded")

file_name = "dlib_face_recognition_resnet_model_v1.dat.bz2"
url = f"http://dlib.net/files/{file_name}"
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.Facenet")

# --------------------------------
# dependency configuration
Expand Down Expand Up @@ -1628,7 +1631,7 @@ def loadModel(
home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/facenet_weights.h5") != True:
print("facenet_weights.h5 will be downloaded...")
logger.info("facenet_weights.h5 will be downloaded...")

output = home + "/.deepface/weights/facenet_weights.h5"
gdown.download(url, output, quiet=False)
Expand Down
4 changes: 3 additions & 1 deletion deepface/basemodels/Facenet512.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import gdown
from deepface.basemodels import Facenet
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.Facenet512")

def loadModel(
url="https://github.com/serengil/deepface_models/releases/download/v1.0/facenet512_weights.h5",
Expand All @@ -15,7 +17,7 @@ def loadModel(
home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/facenet512_weights.h5") != True:
print("facenet512_weights.h5 will be downloaded...")
logger.info("facenet512_weights.h5 will be downloaded...")

output = home + "/.deepface/weights/facenet512_weights.h5"
gdown.download(url, output, quiet=False)
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.FbDeepFace")

# --------------------------------
# dependency configuration
Expand Down Expand Up @@ -57,7 +60,7 @@ def loadModel(
home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5") != True:
print("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")
logger.info("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")

output = home + "/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"

Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/OpenFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.OpenFace")

tf_version = int(tf.__version__.split(".", maxsplit=1)[0])
if tf_version == 1:
Expand Down Expand Up @@ -362,7 +365,7 @@ def loadModel(
home = functions.get_deepface_home()

if os.path.isfile(home + "/.deepface/weights/openface_weights.h5") != True:
print("openface_weights.h5 will be downloaded...")
logger.info("openface_weights.h5 will be downloaded...")

output = home + "/.deepface/weights/openface_weights.h5"
gdown.download(url, output, quiet=False)
Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/SFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import gdown

from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.SFace")

# pylint: disable=line-too-long, too-few-public-methods

Expand Down Expand Up @@ -44,7 +47,7 @@ def load_model(

if not os.path.isfile(file_name):

print("sface weights will be downloaded...")
logger.info("sface weights will be downloaded...")

gdown.download(url, file_name, quiet=False)

Expand Down
5 changes: 4 additions & 1 deletion deepface/basemodels/VGGFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import gdown
import tensorflow as tf
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="basemodels.VGGFace")

# ---------------------------------------

Expand Down Expand Up @@ -95,7 +98,7 @@ def loadModel(
output = home + "/.deepface/weights/vgg_face_weights.h5"

if os.path.isfile(output) != True:
print("vgg_face_weights.h5 will be downloaded...")
logger.info("vgg_face_weights.h5 will be downloaded...")
gdown.download(url, output, quiet=False)

# -----------------------------------
Expand Down
9 changes: 6 additions & 3 deletions deepface/commons/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

# package dependencies
from deepface.detectors import FaceDetector
from deepface.commons.logger import Logger

logger = Logger(module="commons.functions")

# --------------------------------------------------
# configurations of dependencies
Expand Down Expand Up @@ -41,11 +43,11 @@ def initialize_folder():

if not os.path.exists(deepFaceHomePath):
os.makedirs(deepFaceHomePath, exist_ok=True)
print("Directory ", home, "/.deepface created")
logger.info(f"Directory {home}/.deepface created")

if not os.path.exists(weightsPath):
os.makedirs(weightsPath, exist_ok=True)
print("Directory ", home, "/.deepface/weights created")
logger.info(f"Directory {home}/.deepface/weights created")


def get_deepface_home():
Expand Down Expand Up @@ -115,6 +117,7 @@ def load_image(img):
# This causes troubles when reading files with non english names
# return cv2.imread(img)


# --------------------------------------------------


Expand Down Expand Up @@ -357,7 +360,7 @@ def preprocess_face(
Deprecated:
0.0.78: Use extract_faces instead of preprocess_face.
"""
print("⚠️ Function preprocess_face is deprecated. Use extract_faces instead.")
logger.info("⚠️ Function preprocess_face is deprecated. Use extract_faces instead.")
serengil marked this conversation as resolved.
Show resolved Hide resolved
result = None
img_objs = extract_faces(
img=img,
Expand Down
39 changes: 39 additions & 0 deletions deepface/commons/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import logging

# pylint: disable=broad-except
class Logger:
def __init__(self, module=None):
self.module = module
log_level = os.environ.get("DEEPFACE_LOG_LEVEL", str(logging.INFO))
try:
self.log_level = int(log_level)
except Exception as err:
self.dump_log(
f"Exception while parsing $DEEPFACE_LOG_LEVEL."
f"Expected int but it is {log_level} ({str(err)})"
)
self.log_level = logging.INFO

def info(self, message):
if self.log_level <= logging.INFO:
self.dump_log(message)

def debug(self, message):
if self.log_level <= logging.DEBUG:
self.dump_log(f"🕷️ {message}")

def warn(self, message):
if self.log_level <= logging.WARNING:
self.dump_log(f"⚠️ {message}")

def error(self, message):
if self.log_level <= logging.ERROR:
self.dump_log(f"🔴 {message}")

def critical(self, message):
if self.log_level <= logging.CRITICAL:
self.dump_log(f"💥 {message}")

def dump_log(self, message):
print(message)
15 changes: 9 additions & 6 deletions deepface/commons/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import cv2
from deepface import DeepFace
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="commons.realtime")

# dependency configuration
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
Expand Down Expand Up @@ -35,15 +38,15 @@ def analysis(
# build models once to store them in the memory
# otherwise, they will be built after cam started and this will cause delays
DeepFace.build_model(model_name=model_name)
print(f"facial recognition model {model_name} is just built")
logger.info(f"facial recognition model {model_name} is just built")

if enable_face_analysis:
DeepFace.build_model(model_name="Age")
print("Age model is just built")
logger.info("Age model is just built")
DeepFace.build_model(model_name="Gender")
print("Gender model is just built")
logger.info("Gender model is just built")
DeepFace.build_model(model_name="Emotion")
print("Emotion model is just built")
logger.info("Emotion model is just built")
# -----------------------
# call a dummy find function for db_path once to create embeddings in the initialization
DeepFace.find(
Expand Down Expand Up @@ -300,7 +303,7 @@ def analysis(
apparent_age = demography["age"]
dominant_gender = demography["dominant_gender"]
gender = "M" if dominant_gender == "Man" else "W"
# print(f"{apparent_age} years old {dominant_emotion}")
logger.debug(f"{apparent_age} years old {dominant_gender}")
analysis_report = str(int(apparent_age)) + " " + gender

# -------------------------------
Expand Down Expand Up @@ -675,7 +678,7 @@ def analysis(
1,
)
except Exception as err: # pylint: disable=broad-except
print(str(err))
logger.error(str(err))

tic = time.time() # in this way, freezed image can show 5 seconds

Expand Down
4 changes: 3 additions & 1 deletion deepface/detectors/DlibWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import bz2
import gdown
from deepface.commons import functions
from deepface.commons.logger import Logger

logger = Logger(module="detectors.DlibWrapper")

def build_model():

Expand All @@ -14,7 +16,7 @@ def build_model():
if os.path.isfile(home + "/.deepface/weights/shape_predictor_5_face_landmarks.dat") != True:

file_name = "shape_predictor_5_face_landmarks.dat.bz2"
print(f"{file_name} is going to be downloaded")
logger.info(f"{file_name} is going to be downloaded")

url = f"http://dlib.net/files/{file_name}"
output = f"{home}/.deepface/weights/{file_name}"
Expand Down
Loading
Loading