mirror of
https://github.com/serengil/deepface.git
synced 2025-06-06 11:35:21 +00:00
Engineering stuff, remove redundant code.
As mentioned: https://github.com/serengil/deepface/pull/1396#discussion_r1900017766
This commit is contained in:
parent
472f146ecc
commit
b69dcfcca7
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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])
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user