From d16f0765fa16f2446ad43322976dc19b086825c7 Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Fri, 16 Feb 2024 13:41:46 +0000 Subject: [PATCH] load img from web sorted --- deepface/modules/preprocessing.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/deepface/modules/preprocessing.py b/deepface/modules/preprocessing.py index aad9b0b47..4287dd088 100644 --- a/deepface/modules/preprocessing.py +++ b/deepface/modules/preprocessing.py @@ -6,7 +6,6 @@ # 3rd party import numpy as np import cv2 -from PIL import Image import requests @@ -36,11 +35,7 @@ def load_image(img: Union[str, np.ndarray]) -> Tuple[np.ndarray, str]: # The image is a url if img.startswith("http"): - return ( - np.array(Image.open(requests.get(img, stream=True, timeout=60).raw).convert("BGR")), - # return url as image name - img, - ) + return load_image_from_web(url=img), img # The image is a path if os.path.isfile(img) is not True: @@ -57,6 +52,21 @@ def load_image(img: Union[str, np.ndarray]) -> Tuple[np.ndarray, str]: return img_obj_bgr, img +def load_image_from_web(url: str) -> np.ndarray: + """ + Loading an image from web + Args: + url: link for the image + Returns: + img (np.ndarray): equivalent to pre-loaded image from opencv (BGR format) + """ + response = requests.get(url, stream=True, timeout=60) + response.raise_for_status() + image_array = np.asarray(bytearray(response.raw.read()), dtype=np.uint8) + image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) + return image + + def load_base64(uri: str) -> np.ndarray: """Load image from base64 string.