Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
93620a28
Unverified
Commit
93620a28
authored
Sep 27, 2018
by
Kai Chen
Committed by
GitHub
Sep 27, 2018
Browse files
Merge pull request #9 from ycxioooong/master
add slice support and unit test
parents
c211ab13
6249f829
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
4 deletions
+42
-4
mmcv/video/io.py
mmcv/video/io.py
+8
-3
tests/test_video.py
tests/test_video.py
+34
-1
No files found.
mmcv/video/io.py
View file @
93620a28
...
...
@@ -163,8 +163,8 @@ class VideoReader(object):
ndarray or None: Return the frame if successful, otherwise None.
"""
if
frame_id
<
0
or
frame_id
>=
self
.
_frame_cnt
:
raise
Value
Error
(
'"frame_id" must be between 0 and {}'
.
format
(
self
.
_frame_cnt
))
raise
Index
Error
(
'"frame_id" must be between 0 and {}'
.
format
(
self
.
_frame_cnt
-
1
))
if
frame_id
==
self
.
_position
:
return
self
.
read
()
if
self
.
_cache
:
...
...
@@ -240,7 +240,12 @@ class VideoReader(object):
def
__getitem__
(
self
,
index
):
if
isinstance
(
index
,
slice
):
raise
RuntimeError
(
'slice has not been supported yet'
)
return
[
self
.
get_frame
(
i
)
for
i
in
range
(
*
index
.
indices
(
self
.
frame_cnt
))]
# support negative indexing
if
index
<
0
:
index
+=
self
.
frame_cnt
if
index
<
0
:
raise
IndexError
(
'index out of range'
)
return
self
.
get_frame
(
index
)
def
__iter__
(
self
):
...
...
tests/test_video.py
View file @
93620a28
...
...
@@ -62,12 +62,45 @@ class TestVideo(object):
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
[
64
]
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
-
104
]
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
63
]
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
[
-
105
]
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
.
read
()
assert
int
(
round
(
img
.
mean
()))
==
205
with
pytest
.
raises
(
Value
Error
):
with
pytest
.
raises
(
Index
Error
):
v
.
get_frame
(
self
.
num_frames
+
1
)
with
pytest
.
raises
(
IndexError
):
v
[
-
self
.
num_frames
-
1
]
def
test_slice
(
self
):
v
=
mmcv
.
VideoReader
(
self
.
video_path
)
imgs
=
v
[
-
105
:
-
103
]
assert
int
(
round
(
imgs
[
0
].
mean
()))
==
94
assert
int
(
round
(
imgs
[
1
].
mean
()))
==
205
assert
len
(
imgs
)
==
2
imgs
=
v
[
63
:
65
]
assert
int
(
round
(
imgs
[
0
].
mean
()))
==
94
assert
int
(
round
(
imgs
[
1
].
mean
()))
==
205
assert
len
(
imgs
)
==
2
imgs
=
v
[
64
:
62
:
-
1
]
assert
int
(
round
(
imgs
[
0
].
mean
()))
==
205
assert
int
(
round
(
imgs
[
1
].
mean
()))
==
94
assert
len
(
imgs
)
==
2
imgs
=
v
[:
5
]
assert
len
(
imgs
)
==
5
for
img
in
imgs
:
assert
int
(
round
(
img
.
mean
()))
==
94
imgs
=
v
[
165
:]
assert
len
(
imgs
)
==
3
for
img
in
imgs
:
assert
int
(
round
(
img
.
mean
()))
==
0
imgs
=
v
[
-
3
:]
assert
len
(
imgs
)
==
3
for
img
in
imgs
:
assert
int
(
round
(
img
.
mean
()))
==
0
def
test_current_frame
(
self
):
v
=
mmcv
.
VideoReader
(
self
.
video_path
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment