Skip to content

Commit

Permalink
bug for loading image from web
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Feb 16, 2024
1 parent 3ddbbd2 commit 01a25be
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions retinaface/commons/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
from typing import Union
import requests
from PIL import Image
import numpy as np
import cv2

Expand All @@ -27,12 +26,10 @@ def get_image(img_uri: Union[str, np.ndarray]) -> np.ndarray:

# if it is an external url
elif isinstance(img_uri, str) and img_uri.startswith("http"):
img = np.array(
Image.open(requests.get(img_uri, stream=True, timeout=60).raw).convert("BGR")
)
img = load_image_from_web(url=img_uri)

# then it has to be a path on filesystem
elif isinstance(img_uri, str):
elif isinstance(img_uri, (str, Path)):
if isinstance(img_uri, Path):
img_uri = str(img_uri)

Expand Down Expand Up @@ -72,6 +69,21 @@ def load_base64_img(uri) -> np.ndarray:
return img_bgr


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 resize_image(img: np.ndarray, scales: list, allow_upscaling: bool) -> tuple:
"""
This function is modified from the following code snippet:
Expand Down

0 comments on commit 01a25be

Please sign in to comment.