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
6249f829
Commit
6249f829
authored
Sep 27, 2018
by
xiongyu
Browse files
add negative indexing support
parent
c75ff259
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
0 deletions
+11
-0
mmcv/video/io.py
mmcv/video/io.py
+5
-0
tests/test_video.py
tests/test_video.py
+6
-0
No files found.
mmcv/video/io.py
View file @
6249f829
...
@@ -241,6 +241,11 @@ class VideoReader(object):
...
@@ -241,6 +241,11 @@ class VideoReader(object):
def
__getitem__
(
self
,
index
):
def
__getitem__
(
self
,
index
):
if
isinstance
(
index
,
slice
):
if
isinstance
(
index
,
slice
):
return
[
self
.
get_frame
(
i
)
for
i
in
range
(
*
index
.
indices
(
self
.
frame_cnt
))]
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
)
return
self
.
get_frame
(
index
)
def
__iter__
(
self
):
def
__iter__
(
self
):
...
...
tests/test_video.py
View file @
6249f829
...
@@ -62,12 +62,18 @@ class TestVideo(object):
...
@@ -62,12 +62,18 @@ class TestVideo(object):
assert
int
(
round
(
img
.
mean
()))
==
94
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
[
64
]
img
=
v
[
64
]
assert
int
(
round
(
img
.
mean
()))
==
205
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
-
104
]
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
63
]
img
=
v
[
63
]
assert
int
(
round
(
img
.
mean
()))
==
94
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
[
-
105
]
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
.
read
()
img
=
v
.
read
()
assert
int
(
round
(
img
.
mean
()))
==
205
assert
int
(
round
(
img
.
mean
()))
==
205
with
pytest
.
raises
(
IndexError
):
with
pytest
.
raises
(
IndexError
):
v
.
get_frame
(
self
.
num_frames
+
1
)
v
.
get_frame
(
self
.
num_frames
+
1
)
with
pytest
.
raises
(
IndexError
):
v
[
-
self
.
num_frames
-
1
]
def
test_slice
(
self
):
def
test_slice
(
self
):
v
=
mmcv
.
VideoReader
(
self
.
video_path
)
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