From 6db41ae3f2511d6b164a2c55df1b0e7f85abaa72 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Sun, 10 Nov 2024 15:18:05 +0000 Subject: [PATCH] load image from file storage added --- deepface/commons/image_utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/deepface/commons/image_utils.py b/deepface/commons/image_utils.py index c2ae1ed6..b72ce0b4 100644 --- a/deepface/commons/image_utils.py +++ b/deepface/commons/image_utils.py @@ -11,6 +11,7 @@ import numpy as np import cv2 from PIL import Image +from werkzeug.datastructures import FileStorage def list_images(path: str) -> List[str]: @@ -133,6 +134,21 @@ def load_image_from_base64(uri: str) -> np.ndarray: return img_bgr +def load_image_from_file_storage(file: FileStorage) -> np.ndarray: + """ + Loads an image from a FileStorage object and decodes it into an OpenCV image. + Args: + file (FileStorage): The FileStorage object containing the image file. + Returns: + img (np.ndarray): The decoded image as a numpy array (OpenCV format). + """ + file_bytes = np.frombuffer(file.read(), np.uint8) + image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) + if image is None: + raise ValueError("Failed to decode image") + return image + + def load_image_from_web(url: str) -> np.ndarray: """ Loading an image from web