Spaces:
Running
Running
| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Any | |
| import numpy as np | |
| from PIL import Image | |
| def image_to_rgb_array(image: Any) -> np.ndarray | str: | |
| """Convert common image inputs to a DeepFace-friendly RGB array or path.""" | |
| if isinstance(image, (str, Path)): | |
| return str(image) | |
| if isinstance(image, Image.Image): | |
| return np.asarray(image.convert("RGB")) | |
| if isinstance(image, np.ndarray): | |
| if image.ndim == 2: | |
| return np.stack([image] * 3, axis=-1) | |
| if image.ndim == 3 and image.shape[-1] == 4: | |
| return image[:, :, :3] | |
| return image | |
| raise TypeError(f"Unsupported image input type: {type(image)!r}") | |