Merge pull request #1126 from serengil/feat-task-1903-bugs-and-enhancements

Feat task 1903 bugs and enhancements
This commit is contained in:
Sefik Ilkin Serengil 2024-03-19 09:26:18 +00:00 committed by GitHub
commit de2f08fe5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 15 deletions

View File

@ -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()

View File

@ -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))

View File

@ -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

View File

@ -53,6 +53,14 @@ 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
if left_eye is not None:
left_eye = (int(x + left_eye[0]), int(y + left_eye[1]))
if right_eye is not None:
right_eye = (int(x + right_eye[0]), int(y + right_eye[1]))
facial_area = FacialAreaRegion(
x=x,
y=y,

View File

@ -132,6 +132,13 @@ 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
if left_eye is not None:
left_eye = (int(x + left_eye[0]), int(y + left_eye[1]))
if right_eye is not None:
right_eye = (int(x + right_eye[0]), int(y + right_eye[1]))
facial_area = FacialAreaRegion(
x=x,
y=y,