Skip to content

Commit

Permalink
Refactor image loading logic to enforce detection base64 encoded stri…
Browse files Browse the repository at this point in the history
…ngs and URLs vs filenames
  • Loading branch information
AndreaLanfranchi committed Feb 28, 2024
1 parent e6624e0 commit a8b6623
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions deepface/modules/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,30 @@ def load_image(source: Any) -> Tuple[np.ndarray, str]:

if len(source.replace(" ", "")) == 0:
raise ValueError("Invalid source. Empty string.")
if source.lower().startswith("data:image/"):

base64_pattern = re.compile(r"^data:image\/.*", re.IGNORECASE)
if base64_pattern.match(source):
return __load_base64(uri=source), "base64 encoded string"
if source.lower().startswith("http"):

http_pattern = re.compile(r"^http(s)?://.*", re.IGNORECASE)
if http_pattern.match(source):
return __load_image_from_web(url=source), source

return __load_image_from_file(filename=source), source

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)
Raises:
HTTPError: if the response status code is not 200
"""
response = requests.get(url, stream=True, timeout=60)
response.raise_for_status()
Expand Down

0 comments on commit a8b6623

Please sign in to comment.