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
7478e1a6
Unverified
Commit
7478e1a6
authored
Sep 21, 2018
by
Kai Chen
Committed by
GitHub
Sep 21, 2018
Browse files
Merge pull request #8 from ycxioooong/master
change video reader to 0-based indexing
parents
27c81690
6d51a941
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
16 deletions
+16
-16
mmcv/video/io.py
mmcv/video/io.py
+12
-12
tests/test_video.py
tests/test_video.py
+4
-4
No files found.
mmcv/video/io.py
View file @
7478e1a6
...
@@ -136,9 +136,9 @@ class VideoReader(object):
...
@@ -136,9 +136,9 @@ class VideoReader(object):
Returns:
Returns:
ndarray or None: Return the frame if successful, otherwise None.
ndarray or None: Return the frame if successful, otherwise None.
"""
"""
pos
=
self
.
_position
+
1
#
pos = self._position
if
self
.
_cache
:
if
self
.
_cache
:
img
=
self
.
_cache
.
get
(
pos
)
img
=
self
.
_cache
.
get
(
self
.
_position
)
if
img
is
not
None
:
if
img
is
not
None
:
ret
=
True
ret
=
True
else
:
else
:
...
@@ -146,38 +146,38 @@ class VideoReader(object):
...
@@ -146,38 +146,38 @@ class VideoReader(object):
self
.
_set_real_position
(
self
.
_position
)
self
.
_set_real_position
(
self
.
_position
)
ret
,
img
=
self
.
_vcap
.
read
()
ret
,
img
=
self
.
_vcap
.
read
()
if
ret
:
if
ret
:
self
.
_cache
.
put
(
pos
,
img
)
self
.
_cache
.
put
(
self
.
_position
,
img
)
else
:
else
:
ret
,
img
=
self
.
_vcap
.
read
()
ret
,
img
=
self
.
_vcap
.
read
()
if
ret
:
if
ret
:
self
.
_position
=
pos
self
.
_position
+
=
1
return
img
return
img
def
get_frame
(
self
,
frame_id
):
def
get_frame
(
self
,
frame_id
):
"""Get frame by index.
"""Get frame by index.
Args:
Args:
frame_id (int): Index of the expected frame,
1
-based.
frame_id (int): Index of the expected frame,
0
-based.
Returns:
Returns:
ndarray or None: Return the frame if successful, otherwise None.
ndarray or None: Return the frame if successful, otherwise None.
"""
"""
if
frame_id
<
=
0
or
frame_id
>
self
.
_frame_cnt
:
if
frame_id
<
0
or
frame_id
>
=
self
.
_frame_cnt
:
raise
ValueError
(
'"frame_id" must be between
1
and {}'
.
format
(
raise
ValueError
(
'"frame_id" must be between
0
and {}'
.
format
(
self
.
_frame_cnt
))
self
.
_frame_cnt
))
if
frame_id
==
self
.
_position
+
1
:
if
frame_id
==
self
.
_position
:
return
self
.
read
()
return
self
.
read
()
if
self
.
_cache
:
if
self
.
_cache
:
img
=
self
.
_cache
.
get
(
frame_id
)
img
=
self
.
_cache
.
get
(
frame_id
)
if
img
is
not
None
:
if
img
is
not
None
:
self
.
_position
=
frame_id
self
.
_position
=
frame_id
+
1
return
img
return
img
self
.
_set_real_position
(
frame_id
-
1
)
self
.
_set_real_position
(
frame_id
)
ret
,
img
=
self
.
_vcap
.
read
()
ret
,
img
=
self
.
_vcap
.
read
()
if
ret
:
if
ret
:
self
.
_position
+=
1
if
self
.
_cache
:
if
self
.
_cache
:
self
.
_cache
.
put
(
self
.
_position
,
img
)
self
.
_cache
.
put
(
self
.
_position
,
img
)
self
.
_position
+=
1
return
img
return
img
def
current_frame
(
self
):
def
current_frame
(
self
):
...
@@ -189,7 +189,7 @@ class VideoReader(object):
...
@@ -189,7 +189,7 @@ class VideoReader(object):
"""
"""
if
self
.
_position
==
0
:
if
self
.
_position
==
0
:
return
None
return
None
return
self
.
_cache
.
get
(
self
.
_position
)
return
self
.
_cache
.
get
(
self
.
_position
-
1
)
def
cvt2frames
(
self
,
def
cvt2frames
(
self
,
frame_dir
,
frame_dir
,
...
...
tests/test_video.py
View file @
7478e1a6
...
@@ -58,11 +58,11 @@ class TestVideo(object):
...
@@ -58,11 +58,11 @@ class TestVideo(object):
v
=
mmcv
.
VideoReader
(
self
.
video_path
)
v
=
mmcv
.
VideoReader
(
self
.
video_path
)
img
=
v
.
read
()
img
=
v
.
read
()
assert
int
(
round
(
img
.
mean
()))
==
94
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
.
get_frame
(
6
4
)
img
=
v
.
get_frame
(
6
3
)
assert
int
(
round
(
img
.
mean
()))
==
94
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
[
65
]
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
64
]
img
=
v
[
64
]
assert
int
(
round
(
img
.
mean
()))
==
205
img
=
v
[
63
]
assert
int
(
round
(
img
.
mean
()))
==
94
assert
int
(
round
(
img
.
mean
()))
==
94
img
=
v
.
read
()
img
=
v
.
read
()
assert
int
(
round
(
img
.
mean
()))
==
205
assert
int
(
round
(
img
.
mean
()))
==
205
...
@@ -82,7 +82,7 @@ class TestVideo(object):
...
@@ -82,7 +82,7 @@ class TestVideo(object):
for
_
in
range
(
10
):
for
_
in
range
(
10
):
v
.
read
()
v
.
read
()
assert
v
.
position
==
10
assert
v
.
position
==
10
v
.
get_frame
(
100
)
v
.
get_frame
(
99
)
assert
v
.
position
==
100
assert
v
.
position
==
100
def
test_iterator
(
self
):
def
test_iterator
(
self
):
...
...
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