mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 03:55: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
|
@abstractmethod
|
||||||
def predict(self, img: Union[np.ndarray, List[np.ndarray]]) -> Union[np.ndarray, np.float64]:
|
def predict(self, img: Union[np.ndarray, List[np.ndarray]]) -> Union[np.ndarray, np.float64]:
|
||||||
pass
|
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:
|
Returns:
|
||||||
np.ndarray (n,)
|
np.ndarray (n,)
|
||||||
"""
|
"""
|
||||||
# Convert to numpy array if input is list
|
# Preprocessing input image or image list.
|
||||||
if isinstance(img, list):
|
imgs = self._preprocess_batch_or_single_input(img)
|
||||||
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)
|
|
||||||
|
|
||||||
# Batch prediction
|
# Batch prediction
|
||||||
age_predictions = self.model.predict_on_batch(imgs)
|
age_predictions = self.model.predict_on_batch(imgs)
|
||||||
|
@ -69,19 +69,8 @@ class EmotionClient(Demography):
|
|||||||
np.ndarray (n, n_emotions)
|
np.ndarray (n, n_emotions)
|
||||||
where n_emotions is the number of emotion categories
|
where n_emotions is the number of emotion categories
|
||||||
"""
|
"""
|
||||||
# Convert to numpy array if input is list
|
# Preprocessing input image or image list.
|
||||||
if isinstance(img, list):
|
imgs = self._preprocess_batch_or_single_input(img)
|
||||||
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)
|
|
||||||
|
|
||||||
# Preprocess each image
|
# Preprocess each image
|
||||||
processed_imgs = np.array([self._preprocess_image(img) for img in imgs])
|
processed_imgs = np.array([self._preprocess_image(img) for img in imgs])
|
||||||
|
@ -51,19 +51,8 @@ class GenderClient(Demography):
|
|||||||
Returns:
|
Returns:
|
||||||
np.ndarray (n, 2)
|
np.ndarray (n, 2)
|
||||||
"""
|
"""
|
||||||
# Convert to numpy array if input is list
|
# Preprocessing input image or image list.
|
||||||
if isinstance(img, list):
|
imgs = self._preprocess_batch_or_single_input(img)
|
||||||
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)
|
|
||||||
|
|
||||||
# Batch prediction
|
# Batch prediction
|
||||||
predictions = self.model.predict_on_batch(imgs)
|
predictions = self.model.predict_on_batch(imgs)
|
||||||
|
@ -51,19 +51,8 @@ class RaceClient(Demography):
|
|||||||
np.ndarray (n, n_races)
|
np.ndarray (n, n_races)
|
||||||
where n_races is the number of race categories
|
where n_races is the number of race categories
|
||||||
"""
|
"""
|
||||||
# Convert to numpy array if input is list
|
# Preprocessing input image or image list.
|
||||||
if isinstance(img, list):
|
imgs = self._preprocess_batch_or_single_input(img)
|
||||||
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)
|
|
||||||
|
|
||||||
# Batch prediction
|
# Batch prediction
|
||||||
predictions = self.model.predict_on_batch(imgs)
|
predictions = self.model.predict_on_batch(imgs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user