Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read images from urls #450

Merged
merged 6 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions eta/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ def download(url, include_alpha=False, flag=None):
return decode(bytes, include_alpha=include_alpha, flag=flag)


def read(path, include_alpha=False, flag=None):
'''Reads image from path.
def read(path_or_url, include_alpha=False, flag=None):
'''Reads image from a file path or url.

By default, images are returned as color images with no alpha channel.

Args:
path: the path to the image on disk
path_or_url: the file path or url to the image
include_alpha: whether to include the alpha channel of the image, if
present, in the returned array. By default, this is False
flag: an optional OpenCV image format flag to use. If provided, this
Expand All @@ -406,10 +406,13 @@ def read(path, include_alpha=False, flag=None):
Returns:
a uint8 numpy array containing the image
'''
if etaw.is_url(path_or_url):
return download(path_or_url, include_alpha=include_alpha, flag=flag)

flag = _get_opencv_imread_flag(flag, include_alpha)
img_bgr = cv2.imread(path, flag)
img_bgr = cv2.imread(path_or_url, flag)
if img_bgr is None:
raise OSError("Image not found '%s'" % path)
raise OSError("Image not found '%s'" % path_or_url)
return _exchange_rb(img_bgr)


Expand Down
18 changes: 18 additions & 0 deletions eta/core/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
# pragma pylint: enable=wildcard-import

import logging
import re
import requests
import six
from time import time

import eta.constants as etac
Expand All @@ -30,6 +32,22 @@
logger = logging.getLogger(__name__)


URL_REGEX = re.compile(r'http://|https://|ftp://|file://|file:\\')


def is_url(filename):
'''Return True if string is an http or ftp path.

Args:
filename: a string

Returns:
True/False if the filename is a url
'''
return (isinstance(filename, six.string_types) and
URL_REGEX.match(filename) is not None)


def download_file(url, path=None, chunk_size=None):
'''Downloads a file from a URL. If a path is specified, the file is written
there. Otherwise, the content is returned as a binary string.
Expand Down