Skip to content

Commit

Permalink
Merge pull request #450 from voxel51/imread-urls
Browse files Browse the repository at this point in the history
Read images from urls
  • Loading branch information
benjaminpkane committed Mar 9, 2020
2 parents 4c34c01 + 16a5550 commit 5da4dc5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit 5da4dc5

Please sign in to comment.