Skip to content

Commit

Permalink
Support reading video from url (#531)
Browse files Browse the repository at this point in the history
* add url support for VideoReader

* add a comment

* add unittest

* use a connectable url for ci
  • Loading branch information
dreamerlin authored Sep 6, 2020
1 parent 9769024 commit fec7cd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mmcv/video/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class VideoReader:
"""

def __init__(self, filename, cache_capacity=10):
check_file_exist(filename, 'Video file not found: ' + filename)
# Check whether the video path is a url
if not filename.startswith(('https://', 'http://')):
check_file_exist(filename, 'Video file not found: ' + filename)
self._vcap = cv2.VideoCapture(filename)
assert cache_capacity > 0
self._cache = Cache(cache_capacity)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_video/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class TestVideoReader:
def setup_class(cls):
cls.video_path = osp.join(osp.dirname(__file__), '../data/test.mp4')
cls.num_frames = 168
cls.video_url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4' # noqa: E501

def test_load(self):
# read from video file
v = mmcv.VideoReader(self.video_path)
assert v.width == 294
assert v.height == 240
Expand All @@ -57,6 +59,16 @@ def test_load(self):
import cv2
assert isinstance(v.vcap, type(cv2.VideoCapture()))

# read from video url
v = mmcv.VideoReader(self.video_url)
assert v.width == 320
assert v.height == 240
assert v.fps == 15
assert v.frame_cnt == 1889
assert len(v) == 1889
assert v.opened
assert isinstance(v.vcap, type(cv2.VideoCapture()))

def test_read(self):
v = mmcv.VideoReader(self.video_path)
img = v.read()
Expand Down

0 comments on commit fec7cd6

Please sign in to comment.