3
3
from pathlib import Path
4
4
from typing import Union
5
5
import requests
6
- from PIL import Image
7
6
import numpy as np
8
7
import cv2
9
8
@@ -27,12 +26,10 @@ def get_image(img_uri: Union[str, np.ndarray]) -> np.ndarray:
27
26
28
27
# if it is an external url
29
28
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 )
33
30
34
31
# then it has to be a path on filesystem
35
- elif isinstance (img_uri , str ):
32
+ elif isinstance (img_uri , ( str , Path ) ):
36
33
if isinstance (img_uri , Path ):
37
34
img_uri = str (img_uri )
38
35
@@ -72,6 +69,21 @@ def load_base64_img(uri) -> np.ndarray:
72
69
return img_bgr
73
70
74
71
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
+
75
87
def resize_image (img : np .ndarray , scales : list , allow_upscaling : bool ) -> tuple :
76
88
"""
77
89
This function is modified from the following code snippet:
0 commit comments