Engineering stuff, remove redundant code.

As mentioned:
https://github.com/serengil/deepface/pull/1396#discussion_r1900017766
This commit is contained in:
h-alice 2025-01-03 10:57:59 +08:00
parent 472f146ecc
commit b69dcfcca7
No known key found for this signature in database
GPG Key ID: 5708F34144A70909
5 changed files with 34 additions and 52 deletions

View File

@ -20,3 +20,29 @@ class Demography(ABC):
@abstractmethod
def predict(self, img: Union[np.ndarray, List[np.ndarray]]) -> Union[np.ndarray, np.float64]:
pass
def _preprocess_batch_or_single_input(self, img: Union[np.ndarray, List[np.ndarray]]) -> np.ndarray:
"""
Preprocess single or batch of images, return as 4-D numpy array.
Args:
img: Single image as np.ndarray (224, 224, 3) or
List of images as List[np.ndarray] or
Batch of images as np.ndarray (n, 224, 224, 3)
Returns:
Four-dimensional numpy array (n, 224, 224, 3)
"""
if isinstance(img, list): # Convert from list to image batch.
image_batch = np.array(img)
else:
image_batch = img
# Remove batch dimension in advance if exists
image_batch = image_batch.squeeze()
# Check input dimension
if len(image_batch.shape) == 3:
# Single image - add batch dimension
imgs = np.expand_dims(image_batch, axis=0)
return image_batch

View File

@ -51,19 +51,8 @@ class ApparentAgeClient(Demography):
Returns:
np.ndarray (n,)
"""
# Convert to numpy array if input is list
if isinstance(img, list):
imgs = np.array(img)
else:
imgs = img
# Remove batch dimension if exists
imgs = imgs.squeeze()
# Check input dimension
if len(imgs.shape) == 3:
# Single image - add batch dimension
imgs = np.expand_dims(imgs, axis=0)
# Preprocessing input image or image list.
imgs = self._preprocess_batch_or_single_input(img)
# Batch prediction
age_predictions = self.model.predict_on_batch(imgs)

View File

@ -69,19 +69,8 @@ class EmotionClient(Demography):
np.ndarray (n, n_emotions)
where n_emotions is the number of emotion categories
"""
# Convert to numpy array if input is list
if isinstance(img, list):
imgs = np.array(img)
else:
imgs = img
# Remove batch dimension if exists
imgs = imgs.squeeze()
# Check input dimension
if len(imgs.shape) == 3:
# Single image - add batch dimension
imgs = np.expand_dims(imgs, axis=0)
# Preprocessing input image or image list.
imgs = self._preprocess_batch_or_single_input(img)
# Preprocess each image
processed_imgs = np.array([self._preprocess_image(img) for img in imgs])

View File

@ -51,19 +51,8 @@ class GenderClient(Demography):
Returns:
np.ndarray (n, 2)
"""
# Convert to numpy array if input is list
if isinstance(img, list):
imgs = np.array(img)
else:
imgs = img
# Remove batch dimension if exists
imgs = imgs.squeeze()
# Check input dimension
if len(imgs.shape) == 3:
# Single image - add batch dimension
imgs = np.expand_dims(imgs, axis=0)
# Preprocessing input image or image list.
imgs = self._preprocess_batch_or_single_input(img)
# Batch prediction
predictions = self.model.predict_on_batch(imgs)

View File

@ -51,19 +51,8 @@ class RaceClient(Demography):
np.ndarray (n, n_races)
where n_races is the number of race categories
"""
# Convert to numpy array if input is list
if isinstance(img, list):
imgs = np.array(img)
else:
imgs = img
# Remove batch dimension if exists
imgs = imgs.squeeze()
# Check input dimension
if len(imgs.shape) == 3:
# Single image - add batch dimension
imgs = np.expand_dims(imgs, axis=0)
# Preprocessing input image or image list.
imgs = self._preprocess_batch_or_single_input(img)
# Batch prediction
predictions = self.model.predict_on_batch(imgs)