From 2cbfc417a45704fd675e81a7a60f17bb20dd4e53 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 19 Mar 2024 08:31:41 +0000 Subject: [PATCH 1/5] issue 1123 resolved for opencv and ssd, we were finding eyes from the detected faces. that is why, eye coordinates were inccorrect with respect to the image itself. we added the x and y coordinates of the detected face into the eye coordinates. --- deepface/detectors/OpenCv.py | 6 ++++++ deepface/detectors/Ssd.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/deepface/detectors/OpenCv.py b/deepface/detectors/OpenCv.py index cf0e8d7..15bdcf3 100644 --- a/deepface/detectors/OpenCv.py +++ b/deepface/detectors/OpenCv.py @@ -53,6 +53,12 @@ class OpenCvClient(Detector): for (x, y, w, h), confidence in zip(faces, scores): detected_face = img[int(y) : int(y + h), int(x) : int(x + w)] left_eye, right_eye = self.find_eyes(img=detected_face) + + # eyes found in the detected face instead image itself + # detected face's coordinates should be added + left_eye = (x + left_eye[0], y + left_eye[1]) + right_eye = (x + right_eye[0], y + right_eye[1]) + facial_area = FacialAreaRegion( x=x, y=y, diff --git a/deepface/detectors/Ssd.py b/deepface/detectors/Ssd.py index f21d44c..b427b4d 100644 --- a/deepface/detectors/Ssd.py +++ b/deepface/detectors/Ssd.py @@ -132,6 +132,11 @@ class SsdClient(Detector): left_eye, right_eye = opencv_module.find_eyes(detected_face) + # eyes found in the detected face instead image itself + # detected face's coordinates should be added + left_eye = (x + left_eye[0], y + left_eye[1]) + right_eye = (x + right_eye[0], y + right_eye[1]) + facial_area = FacialAreaRegion( x=x, y=y, From 71bea587dafa5c4e2c95600f4aeb876664c3328d Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 19 Mar 2024 08:45:10 +0000 Subject: [PATCH 2/5] check tf_keras installation for tf 2.16 or later versions --- deepface/DeepFace.py | 8 ++++++++ deepface/commons/package_utils.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index e9f6d9d..c8920d8 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -4,6 +4,11 @@ import warnings import logging from typing import Any, Dict, List, Tuple, Union, Optional +# this has to be set before importing tensorflow +os.environ["TF_USE_LEGACY_KERAS"] = "1" + +# pylint: disable=wrong-import-position + # 3rd party dependencies import numpy as np import pandas as pd @@ -28,6 +33,9 @@ logger = Logger(module="DeepFace") # ----------------------------------- # configurations for dependencies +# users should install tf_keras package if they are using tf 2.16 or later versions +package_utils.validate_for_keras3() + warnings.filterwarnings("ignore") os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" tf_version = package_utils.get_tf_major_version() diff --git a/deepface/commons/package_utils.py b/deepface/commons/package_utils.py index 20fa35e..1620732 100644 --- a/deepface/commons/package_utils.py +++ b/deepface/commons/package_utils.py @@ -50,3 +50,24 @@ def find_hash_of_file(file_path: str) -> str: hasher = hashlib.sha1() hasher.update(properties.encode("utf-8")) return hasher.hexdigest() + + +def validate_for_keras3(): + tf_major = get_tf_major_version() + tf_minor = get_tf_minor_version() + + # tf_keras is a must dependency after tf 2.16 + if tf_major == 1 or (tf_major == 2 and tf_minor < 16): + return + + try: + import tf_keras + + logger.debug(f"tf_keras is already available - {tf_keras.__version__}") + except ImportError as err: + # you may consider to install that package here + raise ValueError( + f"You have tensorflow {tf.__version__} and this requires " + "tf-keras package. Please run `pip install tf-keras` " + "or downgrade your tensorflow." + ) from err From 87a95dc2ae867d593b05a48eaa2db9de7996b775 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 19 Mar 2024 08:53:40 +0000 Subject: [PATCH 3/5] eyes can be none --- deepface/detectors/OpenCv.py | 6 ++++-- deepface/detectors/Ssd.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/deepface/detectors/OpenCv.py b/deepface/detectors/OpenCv.py index 15bdcf3..5c7c4eb 100644 --- a/deepface/detectors/OpenCv.py +++ b/deepface/detectors/OpenCv.py @@ -56,8 +56,10 @@ class OpenCvClient(Detector): # eyes found in the detected face instead image itself # detected face's coordinates should be added - left_eye = (x + left_eye[0], y + left_eye[1]) - right_eye = (x + right_eye[0], y + right_eye[1]) + if left_eye is not None: + left_eye = (x + left_eye[0], y + left_eye[1]) + if right_eye is not None: + right_eye = (x + right_eye[0], y + right_eye[1]) facial_area = FacialAreaRegion( x=x, diff --git a/deepface/detectors/Ssd.py b/deepface/detectors/Ssd.py index b427b4d..c1cd32e 100644 --- a/deepface/detectors/Ssd.py +++ b/deepface/detectors/Ssd.py @@ -134,8 +134,10 @@ class SsdClient(Detector): # eyes found in the detected face instead image itself # detected face's coordinates should be added - left_eye = (x + left_eye[0], y + left_eye[1]) - right_eye = (x + right_eye[0], y + right_eye[1]) + if left_eye is not None: + left_eye = (x + left_eye[0], y + left_eye[1]) + if right_eye is not None: + right_eye = (x + right_eye[0], y + right_eye[1]) facial_area = FacialAreaRegion( x=x, From ce1fa57d357a7c6aca83dd5db8e02b76634c95a9 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 19 Mar 2024 08:56:05 +0000 Subject: [PATCH 4/5] no need to check version for fb deepface --- deepface/basemodels/FbDeepFace.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/deepface/basemodels/FbDeepFace.py b/deepface/basemodels/FbDeepFace.py index 278f5cc..5e41c7d 100644 --- a/deepface/basemodels/FbDeepFace.py +++ b/deepface/basemodels/FbDeepFace.py @@ -23,6 +23,7 @@ if tf_major == 1: Flatten, Dense, Dropout, + LocallyConnected2D, ) else: from tensorflow.keras.models import Model, Sequential @@ -32,6 +33,7 @@ else: Flatten, Dense, Dropout, + LocallyConnected2D, ) @@ -43,14 +45,6 @@ class DeepFaceClient(FacialRecognition): """ def __init__(self): - # DeepFace requires tf 2.12 or less - if tf_major == 2 and tf_minor > 12: - # Ref: https://github.com/serengil/deepface/pull/1079 - raise ValueError( - "DeepFace model requires LocallyConnected2D but it is no longer supported" - f" after tf 2.12 but you have {tf_major}.{tf_minor}. You need to downgrade your tf." - ) - self.model = load_model() self.model_name = "DeepFace" self.input_shape = (152, 152) @@ -75,13 +69,6 @@ def load_model( """ Construct DeepFace model, download its weights and load """ - # we have some checks for this dependency in the init of client - # putting this in global causes library initialization - if tf_major == 1: - from keras.layers import LocallyConnected2D - else: - from tensorflow.keras.layers import LocallyConnected2D - base_model = Sequential() base_model.add( Convolution2D(32, (11, 11), activation="relu", name="C1", input_shape=(152, 152, 3)) From e442ccd2f15d2672bb28da6115475d5b3055d570 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Tue, 19 Mar 2024 09:18:23 +0000 Subject: [PATCH 5/5] avoid int64 not serializable error --- deepface/detectors/OpenCv.py | 4 ++-- deepface/detectors/Ssd.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deepface/detectors/OpenCv.py b/deepface/detectors/OpenCv.py index 5c7c4eb..93d2492 100644 --- a/deepface/detectors/OpenCv.py +++ b/deepface/detectors/OpenCv.py @@ -57,9 +57,9 @@ class OpenCvClient(Detector): # eyes found in the detected face instead image itself # detected face's coordinates should be added if left_eye is not None: - left_eye = (x + left_eye[0], y + left_eye[1]) + left_eye = (int(x + left_eye[0]), int(y + left_eye[1])) if right_eye is not None: - right_eye = (x + right_eye[0], y + right_eye[1]) + right_eye = (int(x + right_eye[0]), int(y + right_eye[1])) facial_area = FacialAreaRegion( x=x, diff --git a/deepface/detectors/Ssd.py b/deepface/detectors/Ssd.py index c1cd32e..ae0a819 100644 --- a/deepface/detectors/Ssd.py +++ b/deepface/detectors/Ssd.py @@ -135,9 +135,9 @@ class SsdClient(Detector): # eyes found in the detected face instead image itself # detected face's coordinates should be added if left_eye is not None: - left_eye = (x + left_eye[0], y + left_eye[1]) + left_eye = (int(x + left_eye[0]), int(y + left_eye[1])) if right_eye is not None: - right_eye = (x + right_eye[0], y + right_eye[1]) + right_eye = (int(x + right_eye[0]), int(y + right_eye[1])) facial_area = FacialAreaRegion( x=x,