Skip to content

Commit

Permalink
Merge pull request #1291 from kremnik/singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil authored Aug 2, 2024
2 parents 6d33c1d + f6977ba commit b1e8508
Show file tree
Hide file tree
Showing 42 changed files with 117 additions and 105 deletions.
4 changes: 2 additions & 2 deletions deepface/DeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# package dependencies
from deepface.commons import package_utils, folder_utils
from deepface.commons import logger as log
from deepface.commons.logger import Logger
from deepface.modules import (
modeling,
representation,
Expand All @@ -29,7 +29,7 @@
)
from deepface import __version__

logger = log.get_singletonish_logger()
logger = Logger()

# -----------------------------------
# configurations for dependencies
Expand Down
4 changes: 2 additions & 2 deletions deepface/api/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# project dependencies
from deepface import DeepFace
from deepface.api.src.modules.core.routes import blueprint
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def create_app():
Expand Down
4 changes: 2 additions & 2 deletions deepface/api/src/modules/core/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from flask import Blueprint, request
from deepface import DeepFace
from deepface.api.src.modules.core import service
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

blueprint = Blueprint("routes", __name__)

Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/ArcFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition

from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# pylint: disable=unsubscriptable-object

Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/DeepID.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import gdown
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

tf_version = package_utils.get_tf_major_version()

Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/Dlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import numpy as np
from deepface.commons import folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

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

Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/Facenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import gdown
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# --------------------------------
# dependency configuration
Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/FbDeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import gdown
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# --------------------------------
# dependency configuration
Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/GhostFaceNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# project dependencies
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

tf_major = package_utils.get_tf_major_version()
if tf_major == 1:
Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/OpenFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import tensorflow as tf
from deepface.commons import package_utils, folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

tf_version = package_utils.get_tf_major_version()
if tf_version == 1:
Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/SFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# project dependencies
from deepface.commons import folder_utils
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

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

Expand Down
4 changes: 2 additions & 2 deletions deepface/basemodels/VGGFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from deepface.commons import package_utils, folder_utils
from deepface.modules import verification
from deepface.models.FacialRecognition import FacialRecognition
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

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

Expand Down
4 changes: 2 additions & 2 deletions deepface/commons/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import gdown

# project dependencies
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def download_external_file(file_name: str, exact_file_path: str, url: str) -> None:
Expand Down
4 changes: 2 additions & 2 deletions deepface/commons/folder_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from pathlib import Path
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def initialize_folder() -> None:
Expand Down
53 changes: 28 additions & 25 deletions deepface/commons/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@

# 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)})."
"Setting app log level to info."
)
self.log_level = logging.INFO
"""
A Logger class for logging messages with a specific log level.
The class follows the singleton design pattern, ensuring that only one
instance of the Logger is created. The parameters of the first instance
are preserved across all instances.
"""

__instance = None

def __new__(cls):
if cls.__instance is None:
cls.__instance = super(Logger, cls).__new__(cls)
return cls.__instance

def __init__(self):
if not hasattr(self, "_singleton_initialized"):
self._singleton_initialized = True # to prevent multiple initializations
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)})."
"Setting app log level to info."
)
self.log_level = logging.INFO

def info(self, message):
if self.log_level <= logging.INFO:
Expand All @@ -39,16 +55,3 @@ def critical(self, message):

def dump_log(self, message):
print(f"{str(datetime.now())[2:-7]} - {message}")


def get_singletonish_logger():
# singleton design pattern
global model_obj

if not "model_obj" in globals():
model_obj = {}

if "logger" not in model_obj.keys():
model_obj["logger"] = Logger(module="Singleton")

return model_obj["logger"]
4 changes: 2 additions & 2 deletions deepface/commons/package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import tensorflow as tf

# package dependencies
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def get_tf_major_version() -> int:
Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/CenterFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# project dependencies
from deepface.commons import folder_utils
from deepface.models.Detector import Detector, FacialAreaRegion
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# pylint: disable=c-extension-no-member

Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/DetectorWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
YuNet,
CenterFace,
)
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def build_model(detector_backend: str) -> Any:
Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/Dlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import numpy as np
from deepface.commons import folder_utils
from deepface.models.Detector import Detector, FacialAreaRegion
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


class DlibClient(Detector):
Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/Ssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from deepface.detectors import OpenCv
from deepface.commons import folder_utils
from deepface.models.Detector import Detector, FacialAreaRegion
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# pylint: disable=line-too-long, c-extension-no-member

Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/Yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import gdown
from deepface.models.Detector import Detector, FacialAreaRegion
from deepface.commons import folder_utils
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# Model's weights paths
PATH = "/.deepface/weights/yolov8n-face.pt"
Expand Down
4 changes: 2 additions & 2 deletions deepface/detectors/YuNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# project dependencies
from deepface.commons import folder_utils
from deepface.models.Detector import Detector, FacialAreaRegion
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


class YuNetClient(Detector):
Expand Down
4 changes: 2 additions & 2 deletions deepface/extendedmodels/Age.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from deepface.basemodels import VGGFace
from deepface.commons import package_utils, folder_utils
from deepface.models.Demography import Demography
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# ----------------------------------------
# dependency configurations
Expand Down
4 changes: 2 additions & 2 deletions deepface/extendedmodels/Emotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# project dependencies
from deepface.commons import package_utils, folder_utils
from deepface.models.Demography import Demography
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# -------------------------------------------
# pylint: disable=line-too-long
Expand Down
4 changes: 2 additions & 2 deletions deepface/extendedmodels/Gender.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from deepface.basemodels import VGGFace
from deepface.commons import package_utils, folder_utils
from deepface.models.Demography import Demography
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# -------------------------------------
# pylint: disable=line-too-long
Expand Down
4 changes: 2 additions & 2 deletions deepface/extendedmodels/Race.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from deepface.basemodels import VGGFace
from deepface.commons import package_utils, folder_utils
from deepface.models.Demography import Demography
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# --------------------------
# pylint: disable=line-too-long
Expand Down
4 changes: 2 additions & 2 deletions deepface/modules/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from deepface.models.Detector import DetectedFace, FacialAreaRegion
from deepface.detectors import DetectorWrapper
from deepface.commons import image_utils
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()

# pylint: disable=no-else-raise

Expand Down
4 changes: 2 additions & 2 deletions deepface/modules/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# project dependencies
from deepface.commons import image_utils
from deepface.modules import representation, detection, verification
from deepface.commons import logger as log
from deepface.commons.logger import Logger

logger = log.get_singletonish_logger()
logger = Logger()


def find(
Expand Down
Loading

0 comments on commit b1e8508

Please sign in to comment.