Skip to content

Commit

Permalink
Merge pull request #1026 from serengil/feat-task-1602-loading-image-f…
Browse files Browse the repository at this point in the history
…rom-web-bug

load img from web sorted
  • Loading branch information
serengil authored Feb 16, 2024
2 parents ee4ad41 + d16f076 commit ab8adf2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions deepface/modules/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# 3rd party
import numpy as np
import cv2
from PIL import Image
import requests


Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down

0 comments on commit ab8adf2

Please sign in to comment.