Skip to content

Commit 01a25be

Browse files
committed
bug for loading image from web
1 parent 3ddbbd2 commit 01a25be

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

retinaface/commons/preprocess.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44
from typing import Union
55
import requests
6-
from PIL import Image
76
import numpy as np
87
import cv2
98

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

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

3431
# then it has to be a path on filesystem
35-
elif isinstance(img_uri, str):
32+
elif isinstance(img_uri, (str, Path)):
3633
if isinstance(img_uri, Path):
3734
img_uri = str(img_uri)
3835

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

7471

72+
def load_image_from_web(url: str) -> np.ndarray:
73+
"""
74+
Loading an image from web
75+
Args:
76+
url: link for the image
77+
Returns:
78+
img (np.ndarray): equivalent to pre-loaded image from opencv (BGR format)
79+
"""
80+
response = requests.get(url, stream=True, timeout=60)
81+
response.raise_for_status()
82+
image_array = np.asarray(bytearray(response.raw.read()), dtype=np.uint8)
83+
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
84+
return image
85+
86+
7587
def resize_image(img: np.ndarray, scales: list, allow_upscaling: bool) -> tuple:
7688
"""
7789
This function is modified from the following code snippet:

0 commit comments

Comments
 (0)