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
a338d43d
Unverified
Commit
a338d43d
authored
Apr 26, 2020
by
Kai Chen
Committed by
GitHub
Apr 26, 2020
Browse files
Refactor unittests (#241)
* refactor unittests * split test_video.py to two files
parent
b7e8d7d7
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
828 additions
and
120 deletions
+828
-120
mmcv/image/io.py
mmcv/image/io.py
+5
-3
tests/data/grayscale_dim3.jpg
tests/data/grayscale_dim3.jpg
+0
-0
tests/test_image/test_colorspace.py
tests/test_image/test_colorspace.py
+134
-0
tests/test_image/test_geometric.py
tests/test_image/test_geometric.py
+289
-0
tests/test_image/test_io.py
tests/test_image/test_io.py
+196
-0
tests/test_image/test_photometric.py
tests/test_image/test_photometric.py
+77
-0
tests/test_runner/test_hooks.py
tests/test_runner/test_hooks.py
+0
-0
tests/test_runner/test_runner.py
tests/test_runner/test_runner.py
+0
-0
tests/test_video/test_optflow.py
tests/test_video/test_optflow.py
+72
-76
tests/test_video/test_processing.py
tests/test_video/test_processing.py
+52
-0
tests/test_video/test_reader.py
tests/test_video/test_reader.py
+3
-41
No files found.
mmcv/image/io.py
View file @
a338d43d
...
...
@@ -38,20 +38,22 @@ def use_backend(backend):
imread_backend
=
backend
if
imread_backend
==
'turbojpeg'
:
if
TurboJPEG
is
None
:
raise
Value
Error
(
'`PyTurboJPEG` is not installed'
)
raise
Import
Error
(
'`PyTurboJPEG` is not installed'
)
global
jpeg
if
jpeg
is
None
:
jpeg
=
TurboJPEG
()
def
_jpegflag
(
flag
=
'color'
,
channel_order
=
'bgr'
):
channel_order
=
channel_order
.
lower
()
if
channel_order
not
in
[
'rgb'
,
'bgr'
]:
raise
ValueError
(
'channel order must be either "rgb" or "bgr"'
)
if
flag
==
'color'
:
if
channel_order
==
'bgr'
:
return
TJPF_BGR
elif
channel_order
==
'rgb'
:
return
TJCS_RGB
else
:
raise
ValueError
(
'channel order must be "rgb" or "bgr"'
)
elif
flag
==
'grayscale'
:
return
TJPF_GRAY
else
:
...
...
tests/data/grayscale_dim3.jpg
0 → 100644
View file @
a338d43d
33.8 KB
tests/test_image/test_colorspace.py
0 → 100644
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
import
cv2
import
numpy
as
np
import
pytest
from
numpy.testing
import
assert_array_almost_equal
,
assert_array_equal
import
mmcv
def
test_bgr2gray
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2gray
(
in_img
)
computed_gray
=
(
in_img
[:,
:,
0
]
*
0.114
+
in_img
[:,
:,
1
]
*
0.587
+
in_img
[:,
:,
2
]
*
0.299
)
assert_array_almost_equal
(
out_img
,
computed_gray
,
decimal
=
4
)
out_img_3d
=
mmcv
.
bgr2gray
(
in_img
,
True
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_rgb2gray
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
rgb2gray
(
in_img
)
computed_gray
=
(
in_img
[:,
:,
0
]
*
0.299
+
in_img
[:,
:,
1
]
*
0.587
+
in_img
[:,
:,
2
]
*
0.114
)
assert_array_almost_equal
(
out_img
,
computed_gray
,
decimal
=
4
)
out_img_3d
=
mmcv
.
rgb2gray
(
in_img
,
True
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_gray2bgr
():
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2bgr
(
in_img
)
assert
out_img
.
shape
==
(
10
,
10
,
3
)
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_gray2rgb
():
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2rgb
(
in_img
)
assert
out_img
.
shape
==
(
10
,
10
,
3
)
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_bgr2rgb
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2rgb
(
in_img
)
assert
out_img
.
shape
==
in_img
.
shape
assert_array_equal
(
out_img
[...,
0
],
in_img
[...,
2
])
assert_array_equal
(
out_img
[...,
1
],
in_img
[...,
1
])
assert_array_equal
(
out_img
[...,
2
],
in_img
[...,
0
])
def
test_rgb2bgr
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
rgb2bgr
(
in_img
)
assert
out_img
.
shape
==
in_img
.
shape
assert_array_equal
(
out_img
[...,
0
],
in_img
[...,
2
])
assert_array_equal
(
out_img
[...,
1
],
in_img
[...,
1
])
assert_array_equal
(
out_img
[...,
2
],
in_img
[...,
0
])
def
test_bgr2hsv
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2hsv
(
in_img
)
argmax
=
in_img
.
argmax
(
axis
=
2
)
computed_hsv
=
np
.
empty_like
(
in_img
,
dtype
=
in_img
.
dtype
)
for
i
in
range
(
in_img
.
shape
[
0
]):
for
j
in
range
(
in_img
.
shape
[
1
]):
b
=
in_img
[
i
,
j
,
0
]
g
=
in_img
[
i
,
j
,
1
]
r
=
in_img
[
i
,
j
,
2
]
v
=
max
(
r
,
g
,
b
)
s
=
(
v
-
min
(
r
,
g
,
b
))
/
v
if
v
!=
0
else
0
if
argmax
[
i
,
j
]
==
0
:
h
=
240
+
60
*
(
r
-
g
)
/
(
v
-
min
(
r
,
g
,
b
))
elif
argmax
[
i
,
j
]
==
1
:
h
=
120
+
60
*
(
b
-
r
)
/
(
v
-
min
(
r
,
g
,
b
))
else
:
h
=
60
*
(
g
-
b
)
/
(
v
-
min
(
r
,
g
,
b
))
if
h
<
0
:
h
+=
360
computed_hsv
[
i
,
j
,
:]
=
[
h
,
s
,
v
]
assert_array_almost_equal
(
out_img
,
computed_hsv
,
decimal
=
2
)
def
test_bgr2hls
():
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2hls
(
in_img
)
argmax
=
in_img
.
argmax
(
axis
=
2
)
computed_hls
=
np
.
empty_like
(
in_img
,
dtype
=
in_img
.
dtype
)
for
i
in
range
(
in_img
.
shape
[
0
]):
for
j
in
range
(
in_img
.
shape
[
1
]):
b
=
in_img
[
i
,
j
,
0
]
g
=
in_img
[
i
,
j
,
1
]
r
=
in_img
[
i
,
j
,
2
]
maxc
=
max
(
r
,
g
,
b
)
minc
=
min
(
r
,
g
,
b
)
_l
=
(
minc
+
maxc
)
/
2.0
if
minc
==
maxc
:
h
=
0.0
s
=
0.0
if
_l
<=
0.5
:
s
=
(
maxc
-
minc
)
/
(
maxc
+
minc
)
else
:
s
=
(
maxc
-
minc
)
/
(
2.0
-
maxc
-
minc
)
if
argmax
[
i
,
j
]
==
2
:
h
=
60
*
(
g
-
b
)
/
(
maxc
-
minc
)
elif
argmax
[
i
,
j
]
==
1
:
h
=
60
*
(
2.0
+
(
b
-
r
)
/
(
maxc
-
minc
))
else
:
h
=
60
*
(
4.0
+
(
r
-
g
)
/
(
maxc
-
minc
))
if
h
<
0
:
h
+=
360
computed_hls
[
i
,
j
,
:]
=
[
h
,
_l
,
s
]
assert_array_almost_equal
(
out_img
,
computed_hls
,
decimal
=
2
)
@
pytest
.
mark
.
parametrize
(
'src,dst,ref'
,
[(
'bgr'
,
'gray'
,
cv2
.
COLOR_BGR2GRAY
),
(
'rgb'
,
'gray'
,
cv2
.
COLOR_RGB2GRAY
),
(
'bgr'
,
'rgb'
,
cv2
.
COLOR_BGR2RGB
),
(
'rgb'
,
'bgr'
,
cv2
.
COLOR_RGB2BGR
),
(
'bgr'
,
'hsv'
,
cv2
.
COLOR_BGR2HSV
),
(
'hsv'
,
'bgr'
,
cv2
.
COLOR_HSV2BGR
),
(
'bgr'
,
'hls'
,
cv2
.
COLOR_BGR2HLS
),
(
'hls'
,
'bgr'
,
cv2
.
COLOR_HLS2BGR
)])
def
test_imconvert
(
src
,
dst
,
ref
):
img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
assert_array_equal
(
mmcv
.
imconvert
(
img
,
src
,
dst
),
cv2
.
cvtColor
(
img
,
ref
))
tests/test_image.py
→
tests/test_image
/test_geometric
.py
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
import
os
import
os.path
as
osp
import
tempfile
from
pathlib
import
Path
import
cv2
import
numpy
as
np
import
pytest
from
numpy.testing
import
assert_array_almost_equal
,
assert_array_equal
from
numpy.testing
import
assert_array_equal
import
mmcv
class
TestIO
:
@
classmethod
def
setup_class
(
cls
):
# the test img resolution is 400x300
cls
.
img_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/color.jpg'
)
cls
.
img_path_obj
=
Path
(
cls
.
img_path
)
cls
.
gray_img_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/grayscale.jpg'
)
cls
.
gray_img_path_obj
=
Path
(
cls
.
gray_img_path
)
cls
.
img
=
cv2
.
imread
(
cls
.
img_path
)
def
assert_img_equal
(
self
,
img
,
ref_img
,
ratio_thr
=
0.999
):
assert
img
.
shape
==
ref_img
.
shape
assert
img
.
dtype
==
ref_img
.
dtype
area
=
ref_img
.
shape
[
0
]
*
ref_img
.
shape
[
1
]
diff
=
np
.
abs
(
img
.
astype
(
'int32'
)
-
ref_img
.
astype
(
'int32'
))
assert
np
.
sum
(
diff
<=
1
)
/
float
(
area
)
>
ratio_thr
def
test_imread
(
self
):
# backend cv2
mmcv
.
use_backend
(
'cv2'
)
img_cv2_color_bgr
=
mmcv
.
imread
(
self
.
img_path
)
assert
img_cv2_color_bgr
.
shape
==
(
300
,
400
,
3
)
img_cv2_color_rgb
=
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'rgb'
)
assert
img_cv2_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2_color_rgb
[:,
:,
::
-
1
],
img_cv2_color_bgr
)
img_cv2_grayscale1
=
mmcv
.
imread
(
self
.
img_path
,
'grayscale'
)
assert
img_cv2_grayscale1
.
shape
==
(
300
,
400
)
img_cv2_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path
)
assert
img_cv2_grayscale2
.
shape
==
(
300
,
400
,
3
)
img_cv2_unchanged
=
mmcv
.
imread
(
self
.
gray_img_path
,
'unchanged'
)
assert
img_cv2_unchanged
.
shape
==
(
300
,
400
)
img_cv2_unchanged
=
mmcv
.
imread
(
img_cv2_unchanged
)
assert_array_equal
(
img_cv2_unchanged
,
mmcv
.
imread
(
img_cv2_unchanged
))
img_cv2_color_bgr
=
mmcv
.
imread
(
self
.
img_path_obj
)
assert
img_cv2_color_bgr
.
shape
==
(
300
,
400
,
3
)
img_cv2_color_rgb
=
mmcv
.
imread
(
self
.
img_path_obj
,
channel_order
=
'rgb'
)
assert
img_cv2_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2_color_rgb
[:,
:,
::
-
1
],
img_cv2_color_bgr
)
img_cv2_grayscale1
=
mmcv
.
imread
(
self
.
img_path_obj
,
'grayscale'
)
assert
img_cv2_grayscale1
.
shape
==
(
300
,
400
)
img_cv2_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path_obj
)
assert
img_cv2_grayscale2
.
shape
==
(
300
,
400
,
3
)
img_cv2_unchanged
=
mmcv
.
imread
(
self
.
gray_img_path_obj
,
'unchanged'
)
assert
img_cv2_unchanged
.
shape
==
(
300
,
400
)
with
pytest
.
raises
(
TypeError
):
mmcv
.
imread
(
1
)
# backend turbojpeg
mmcv
.
use_backend
(
'turbojpeg'
)
img_turbojpeg_color_bgr
=
mmcv
.
imread
(
self
.
img_path
)
assert
img_turbojpeg_color_bgr
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_color_bgr
,
img_cv2_color_bgr
)
img_turbojpeg_color_rgb
=
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'rgb'
)
assert
img_turbojpeg_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_color_rgb
,
img_cv2_color_rgb
)
with
pytest
.
raises
(
ValueError
):
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'unsupport_order'
)
img_turbojpeg_grayscale1
=
mmcv
.
imread
(
self
.
img_path
,
flag
=
'grayscale'
)
assert
img_turbojpeg_grayscale1
.
shape
==
(
300
,
400
)
assert_array_equal
(
img_turbojpeg_grayscale1
,
img_cv2_grayscale1
)
img_turbojpeg_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path
)
assert
img_turbojpeg_grayscale2
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_grayscale2
,
img_cv2_grayscale2
)
img_turbojpeg_grayscale2
=
mmcv
.
imread
(
img_turbojpeg_grayscale2
)
assert_array_equal
(
img_turbojpeg_grayscale2
,
mmcv
.
imread
(
img_turbojpeg_grayscale2
))
with
pytest
.
raises
(
ValueError
):
mmcv
.
imread
(
self
.
gray_img_path
,
'unchanged'
)
with
pytest
.
raises
(
TypeError
):
mmcv
.
imread
(
1
)
with
pytest
.
raises
(
AssertionError
):
mmcv
.
use_backend
(
'unsupport_backend'
)
mmcv
.
use_backend
(
'cv2'
)
def
test_imfrombytes
(
self
):
# backend cv2
mmcv
.
use_backend
(
'cv2'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_cv2
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
img_cv2
.
shape
==
(
300
,
400
,
3
)
# backend turbojpeg
mmcv
.
use_backend
(
'turbojpeg'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
img_turbojpeg
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2
,
img_turbojpeg
)
mmcv
.
use_backend
(
'cv2'
)
def
test_imwrite
(
self
):
img
=
mmcv
.
imread
(
self
.
img_path
)
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'mmcv_test.jpg'
)
mmcv
.
imwrite
(
img
,
out_file
)
rewrite_img
=
mmcv
.
imread
(
out_file
)
os
.
remove
(
out_file
)
self
.
assert_img_equal
(
img
,
rewrite_img
)
class
TestColorSpace
:
def
test_bgr2gray
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2gray
(
in_img
)
computed_gray
=
(
in_img
[:,
:,
0
]
*
0.114
+
in_img
[:,
:,
1
]
*
0.587
+
in_img
[:,
:,
2
]
*
0.299
)
assert_array_almost_equal
(
out_img
,
computed_gray
,
decimal
=
4
)
out_img_3d
=
mmcv
.
bgr2gray
(
in_img
,
True
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_rgb2gray
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
rgb2gray
(
in_img
)
computed_gray
=
(
in_img
[:,
:,
0
]
*
0.299
+
in_img
[:,
:,
1
]
*
0.587
+
in_img
[:,
:,
2
]
*
0.114
)
assert_array_almost_equal
(
out_img
,
computed_gray
,
decimal
=
4
)
out_img_3d
=
mmcv
.
rgb2gray
(
in_img
,
True
)
assert
out_img_3d
.
shape
==
(
10
,
10
,
1
)
assert_array_almost_equal
(
out_img_3d
[...,
0
],
out_img
,
decimal
=
4
)
def
test_gray2bgr
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2bgr
(
in_img
)
assert
out_img
.
shape
==
(
10
,
10
,
3
)
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_gray2rgb
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
gray2rgb
(
in_img
)
assert
out_img
.
shape
==
(
10
,
10
,
3
)
for
i
in
range
(
3
):
assert_array_almost_equal
(
out_img
[...,
i
],
in_img
,
decimal
=
4
)
def
test_bgr2rgb
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2rgb
(
in_img
)
assert
out_img
.
shape
==
in_img
.
shape
assert_array_equal
(
out_img
[...,
0
],
in_img
[...,
2
])
assert_array_equal
(
out_img
[...,
1
],
in_img
[...,
1
])
assert_array_equal
(
out_img
[...,
2
],
in_img
[...,
0
])
def
test_rgb2bgr
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
rgb2bgr
(
in_img
)
assert
out_img
.
shape
==
in_img
.
shape
assert_array_equal
(
out_img
[...,
0
],
in_img
[...,
2
])
assert_array_equal
(
out_img
[...,
1
],
in_img
[...,
1
])
assert_array_equal
(
out_img
[...,
2
],
in_img
[...,
0
])
def
test_bgr2hsv
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2hsv
(
in_img
)
argmax
=
in_img
.
argmax
(
axis
=
2
)
computed_hsv
=
np
.
empty_like
(
in_img
,
dtype
=
in_img
.
dtype
)
for
i
in
range
(
in_img
.
shape
[
0
]):
for
j
in
range
(
in_img
.
shape
[
1
]):
b
=
in_img
[
i
,
j
,
0
]
g
=
in_img
[
i
,
j
,
1
]
r
=
in_img
[
i
,
j
,
2
]
v
=
max
(
r
,
g
,
b
)
s
=
(
v
-
min
(
r
,
g
,
b
))
/
v
if
v
!=
0
else
0
if
argmax
[
i
,
j
]
==
0
:
h
=
240
+
60
*
(
r
-
g
)
/
(
v
-
min
(
r
,
g
,
b
))
elif
argmax
[
i
,
j
]
==
1
:
h
=
120
+
60
*
(
b
-
r
)
/
(
v
-
min
(
r
,
g
,
b
))
else
:
h
=
60
*
(
g
-
b
)
/
(
v
-
min
(
r
,
g
,
b
))
if
h
<
0
:
h
+=
360
computed_hsv
[
i
,
j
,
:]
=
[
h
,
s
,
v
]
assert_array_almost_equal
(
out_img
,
computed_hsv
,
decimal
=
2
)
def
test_bgr2hls
(
self
):
in_img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
out_img
=
mmcv
.
bgr2hls
(
in_img
)
argmax
=
in_img
.
argmax
(
axis
=
2
)
computed_hls
=
np
.
empty_like
(
in_img
,
dtype
=
in_img
.
dtype
)
for
i
in
range
(
in_img
.
shape
[
0
]):
for
j
in
range
(
in_img
.
shape
[
1
]):
b
=
in_img
[
i
,
j
,
0
]
g
=
in_img
[
i
,
j
,
1
]
r
=
in_img
[
i
,
j
,
2
]
maxc
=
max
(
r
,
g
,
b
)
minc
=
min
(
r
,
g
,
b
)
_l
=
(
minc
+
maxc
)
/
2.0
if
minc
==
maxc
:
h
=
0.0
s
=
0.0
if
_l
<=
0.5
:
s
=
(
maxc
-
minc
)
/
(
maxc
+
minc
)
else
:
s
=
(
maxc
-
minc
)
/
(
2.0
-
maxc
-
minc
)
if
argmax
[
i
,
j
]
==
2
:
h
=
60
*
(
g
-
b
)
/
(
maxc
-
minc
)
elif
argmax
[
i
,
j
]
==
1
:
h
=
60
*
(
2.0
+
(
b
-
r
)
/
(
maxc
-
minc
))
else
:
h
=
60
*
(
4.0
+
(
r
-
g
)
/
(
maxc
-
minc
))
if
h
<
0
:
h
+=
360
computed_hls
[
i
,
j
,
:]
=
[
h
,
_l
,
s
]
assert_array_almost_equal
(
out_img
,
computed_hls
,
decimal
=
2
)
@
pytest
.
mark
.
parametrize
(
'src,dst,ref'
,
[(
'bgr'
,
'gray'
,
cv2
.
COLOR_BGR2GRAY
),
(
'rgb'
,
'gray'
,
cv2
.
COLOR_RGB2GRAY
),
(
'bgr'
,
'rgb'
,
cv2
.
COLOR_BGR2RGB
),
(
'rgb'
,
'bgr'
,
cv2
.
COLOR_RGB2BGR
),
(
'bgr'
,
'hsv'
,
cv2
.
COLOR_BGR2HSV
),
(
'hsv'
,
'bgr'
,
cv2
.
COLOR_HSV2BGR
),
(
'bgr'
,
'hls'
,
cv2
.
COLOR_BGR2HLS
),
(
'hls'
,
'bgr'
,
cv2
.
COLOR_HLS2BGR
)])
def
test_imconvert
(
self
,
src
,
dst
,
ref
):
img
=
np
.
random
.
rand
(
10
,
10
,
3
).
astype
(
np
.
float32
)
assert_array_equal
(
mmcv
.
imconvert
(
img
,
src
,
dst
),
cv2
.
cvtColor
(
img
,
ref
))
class
TestGeometric
:
@
classmethod
def
setup_class
(
cls
):
cls
.
data_dir
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'../data'
)
# the test img resolution is 400x300
cls
.
img_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/
color.jpg'
)
cls
.
img_path
=
osp
.
join
(
cls
.
data_dir
,
'
color.jpg'
)
cls
.
img
=
cv2
.
imread
(
cls
.
img_path
)
def
test_imresize
(
self
):
...
...
@@ -432,7 +191,7 @@ class TestGeometric:
patch
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
[
0
,
:])
patches
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
[[
0
],
:])
assert
patch
.
shape
==
(
100
,
100
,
3
)
patch_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/
patches'
)
patch_path
=
osp
.
join
(
self
.
data_dir
,
'
patches'
)
ref_patch
=
np
.
load
(
patch_path
+
'/0.npy'
)
assert_array_equal
(
patch
,
ref_patch
)
assert
isinstance
(
patches
,
list
)
and
len
(
patches
)
==
1
...
...
@@ -442,23 +201,23 @@ class TestGeometric:
patches
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
)
assert
len
(
patches
)
==
bboxes
.
shape
[
0
]
for
i
in
range
(
len
(
patches
)):
ref_patch
=
np
.
load
(
patch_path
+
f
'/
{
i
}
.npy'
)
ref_patch
=
np
.
load
(
patch_path
+
'/{}.npy'
.
format
(
i
)
)
assert_array_equal
(
patches
[
i
],
ref_patch
)
# crop with scaling and no padding
patches
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
,
1.2
)
for
i
in
range
(
len
(
patches
)):
ref_patch
=
np
.
load
(
patch_path
+
f
'/scale_
{
i
}
.npy'
)
ref_patch
=
np
.
load
(
patch_path
+
'/scale_{}.npy'
.
format
(
i
)
)
assert_array_equal
(
patches
[
i
],
ref_patch
)
# crop with scaling and padding
patches
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
,
1.2
,
pad_fill
=
[
255
,
255
,
0
])
for
i
in
range
(
len
(
patches
)):
ref_patch
=
np
.
load
(
patch_path
+
f
'/pad_
{
i
}
.npy'
)
ref_patch
=
np
.
load
(
patch_path
+
'/pad_{}.npy'
.
format
(
i
)
)
assert_array_equal
(
patches
[
i
],
ref_patch
)
patches
=
mmcv
.
imcrop
(
self
.
img
,
bboxes
,
1.2
,
pad_fill
=
0
)
for
i
in
range
(
len
(
patches
)):
ref_patch
=
np
.
load
(
patch_path
+
f
'/pad0_
{
i
}
.npy'
)
ref_patch
=
np
.
load
(
patch_path
+
'/pad0_{}.npy'
.
format
(
i
)
)
assert_array_equal
(
patches
[
i
],
ref_patch
)
def
test_impad
(
self
):
...
...
@@ -528,72 +287,3 @@ class TestGeometric:
with
pytest
.
raises
(
ValueError
):
mmcv
.
imrotate
(
img
,
90
,
center
=
(
0
,
0
),
auto_bound
=
True
)
class
TestPhotometric
:
@
classmethod
def
setup_class
(
cls
):
# the test img resolution is 400x300
cls
.
img_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/color.jpg'
)
cls
.
img
=
cv2
.
imread
(
cls
.
img_path
)
cls
.
mean
=
np
.
array
([
123.675
,
116.28
,
103.53
],
dtype
=
np
.
float32
)
cls
.
std
=
np
.
array
([
58.395
,
57.12
,
57.375
],
dtype
=
np
.
float32
)
def
test_imnormalize
(
self
):
rgb_img
=
self
.
img
[:,
:,
::
-
1
]
baseline
=
(
rgb_img
-
self
.
mean
)
/
self
.
std
img
=
mmcv
.
imnormalize
(
self
.
img
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
!=
id
(
self
.
img
)
img
=
mmcv
.
imnormalize
(
rgb_img
,
self
.
mean
,
self
.
std
,
to_rgb
=
False
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
!=
id
(
rgb_img
)
def
test_imnormalize_
(
self
):
img_for_normalize
=
np
.
float32
(
self
.
img
)
rgb_img_for_normalize
=
np
.
float32
(
self
.
img
[:,
:,
::
-
1
])
baseline
=
(
rgb_img_for_normalize
-
self
.
mean
)
/
self
.
std
img
=
mmcv
.
imnormalize_
(
img_for_normalize
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img_for_normalize
,
baseline
)
assert
id
(
img
)
==
id
(
img_for_normalize
)
img
=
mmcv
.
imnormalize_
(
rgb_img_for_normalize
,
self
.
mean
,
self
.
std
,
to_rgb
=
False
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
==
id
(
rgb_img_for_normalize
)
def
test_imdenormalize
(
self
):
norm_img
=
(
self
.
img
[:,
:,
::
-
1
]
-
self
.
mean
)
/
self
.
std
rgb_baseline
=
(
norm_img
*
self
.
std
+
self
.
mean
)
bgr_baseline
=
rgb_baseline
[:,
:,
::
-
1
]
img
=
mmcv
.
imdenormalize
(
norm_img
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img
,
bgr_baseline
)
img
=
mmcv
.
imdenormalize
(
norm_img
,
self
.
mean
,
self
.
std
,
to_bgr
=
False
)
assert
np
.
allclose
(
img
,
rgb_baseline
)
def
test_iminvert
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
255
,
127
,
0
],
[
254
,
128
,
1
],
[
253
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
iminvert
(
img
),
img_r
)
def
test_solarize
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
0
,
127
,
0
],
[
1
,
127
,
1
],
[
2
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
solarize
(
img
),
img_r
)
img_r
=
np
.
array
([[
0
,
127
,
0
],
[
1
,
128
,
1
],
[
2
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
solarize
(
img
,
100
),
img_r
)
def
test_posterize
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
0
,
128
,
128
],
[
0
,
0
,
128
],
[
0
,
128
,
128
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
posterize
(
img
,
1
),
img_r
)
img_r
=
np
.
array
([[
0
,
128
,
224
],
[
0
,
96
,
224
],
[
0
,
128
,
224
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
posterize
(
img
,
3
),
img_r
)
tests/test_image/test_io.py
0 → 100644
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
import
os
import
os.path
as
osp
import
tempfile
from
pathlib
import
Path
from
unittest.mock
import
patch
import
cv2
import
numpy
as
np
import
pytest
from
numpy.testing
import
assert_array_equal
import
mmcv
class
TestIO
:
@
classmethod
def
setup_class
(
cls
):
cls
.
data_dir
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'../data'
)
# the test img resolution is 400x300
cls
.
img_path
=
osp
.
join
(
cls
.
data_dir
,
'color.jpg'
)
cls
.
img_path_obj
=
Path
(
cls
.
img_path
)
cls
.
gray_img_path
=
osp
.
join
(
cls
.
data_dir
,
'grayscale.jpg'
)
cls
.
gray_img_path_obj
=
Path
(
cls
.
gray_img_path
)
cls
.
gray_img_dim3_path
=
osp
.
join
(
cls
.
data_dir
,
'grayscale_dim3.jpg'
)
cls
.
img
=
cv2
.
imread
(
cls
.
img_path
)
def
assert_img_equal
(
self
,
img
,
ref_img
,
ratio_thr
=
0.999
):
assert
img
.
shape
==
ref_img
.
shape
assert
img
.
dtype
==
ref_img
.
dtype
area
=
ref_img
.
shape
[
0
]
*
ref_img
.
shape
[
1
]
diff
=
np
.
abs
(
img
.
astype
(
'int32'
)
-
ref_img
.
astype
(
'int32'
))
assert
np
.
sum
(
diff
<=
1
)
/
float
(
area
)
>
ratio_thr
def
test_imread
(
self
):
# backend cv2
mmcv
.
use_backend
(
'cv2'
)
img_cv2_color_bgr
=
mmcv
.
imread
(
self
.
img_path
)
assert
img_cv2_color_bgr
.
shape
==
(
300
,
400
,
3
)
img_cv2_color_rgb
=
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'rgb'
)
assert
img_cv2_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2_color_rgb
[:,
:,
::
-
1
],
img_cv2_color_bgr
)
img_cv2_grayscale1
=
mmcv
.
imread
(
self
.
img_path
,
'grayscale'
)
assert
img_cv2_grayscale1
.
shape
==
(
300
,
400
)
img_cv2_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path
)
assert
img_cv2_grayscale2
.
shape
==
(
300
,
400
,
3
)
img_cv2_unchanged
=
mmcv
.
imread
(
self
.
gray_img_path
,
'unchanged'
)
assert
img_cv2_unchanged
.
shape
==
(
300
,
400
)
img_cv2_unchanged
=
mmcv
.
imread
(
img_cv2_unchanged
)
assert_array_equal
(
img_cv2_unchanged
,
mmcv
.
imread
(
img_cv2_unchanged
))
img_cv2_color_bgr
=
mmcv
.
imread
(
self
.
img_path_obj
)
assert
img_cv2_color_bgr
.
shape
==
(
300
,
400
,
3
)
img_cv2_color_rgb
=
mmcv
.
imread
(
self
.
img_path_obj
,
channel_order
=
'rgb'
)
assert
img_cv2_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2_color_rgb
[:,
:,
::
-
1
],
img_cv2_color_bgr
)
img_cv2_grayscale1
=
mmcv
.
imread
(
self
.
img_path_obj
,
'grayscale'
)
assert
img_cv2_grayscale1
.
shape
==
(
300
,
400
)
img_cv2_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path_obj
)
assert
img_cv2_grayscale2
.
shape
==
(
300
,
400
,
3
)
img_cv2_unchanged
=
mmcv
.
imread
(
self
.
gray_img_path_obj
,
'unchanged'
)
assert
img_cv2_unchanged
.
shape
==
(
300
,
400
)
with
pytest
.
raises
(
TypeError
):
mmcv
.
imread
(
1
)
# backend turbojpeg
mmcv
.
use_backend
(
'turbojpeg'
)
img_turbojpeg_color_bgr
=
mmcv
.
imread
(
self
.
img_path
)
assert
img_turbojpeg_color_bgr
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_color_bgr
,
img_cv2_color_bgr
)
img_turbojpeg_color_rgb
=
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'rgb'
)
assert
img_turbojpeg_color_rgb
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_color_rgb
,
img_cv2_color_rgb
)
with
pytest
.
raises
(
ValueError
):
mmcv
.
imread
(
self
.
img_path
,
channel_order
=
'unsupport_order'
)
img_turbojpeg_grayscale1
=
mmcv
.
imread
(
self
.
img_path
,
flag
=
'grayscale'
)
assert
img_turbojpeg_grayscale1
.
shape
==
(
300
,
400
)
assert_array_equal
(
img_turbojpeg_grayscale1
,
img_cv2_grayscale1
)
img_turbojpeg_grayscale2
=
mmcv
.
imread
(
self
.
gray_img_path
)
assert
img_turbojpeg_grayscale2
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_turbojpeg_grayscale2
,
img_cv2_grayscale2
)
img_turbojpeg_grayscale2
=
mmcv
.
imread
(
img_turbojpeg_grayscale2
)
assert_array_equal
(
img_turbojpeg_grayscale2
,
mmcv
.
imread
(
img_turbojpeg_grayscale2
))
with
pytest
.
raises
(
ValueError
):
mmcv
.
imread
(
self
.
gray_img_path
,
'unchanged'
)
with
pytest
.
raises
(
TypeError
):
mmcv
.
imread
(
1
)
with
pytest
.
raises
(
AssertionError
):
mmcv
.
use_backend
(
'unsupport_backend'
)
mmcv
.
use_backend
(
'cv2'
)
def
test_imfrombytes
(
self
):
# backend cv2, channel order: bgr
mmcv
.
use_backend
(
'cv2'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_cv2
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
img_cv2
.
shape
==
(
300
,
400
,
3
)
# backend cv2, channel order: rgb
mmcv
.
use_backend
(
'cv2'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_rgb_cv2
=
mmcv
.
imfrombytes
(
img_bytes
,
channel_order
=
'rgb'
)
assert
img_rgb_cv2
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_rgb_cv2
,
img_cv2
[:,
:,
::
-
1
])
# backend cv2, grayscale, decode as 3 channels
with
open
(
self
.
gray_img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_rgb_cv2
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
gray_img_rgb_cv2
.
shape
==
(
300
,
400
,
3
)
# backend cv2, grayscale
with
open
(
self
.
gray_img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_cv2
=
mmcv
.
imfrombytes
(
img_bytes
,
flag
=
'grayscale'
)
assert
gray_img_cv2
.
shape
==
(
300
,
400
)
# backend cv2, grayscale dim3
with
open
(
self
.
gray_img_dim3_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_dim3_cv2
=
mmcv
.
imfrombytes
(
img_bytes
,
flag
=
'grayscale'
)
assert
gray_img_dim3_cv2
.
shape
==
(
300
,
400
)
# backend turbojpeg, channel order: bgr
mmcv
.
use_backend
(
'turbojpeg'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
img_turbojpeg
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_cv2
,
img_turbojpeg
)
# backend turbojpeg, channel order: rgb
mmcv
.
use_backend
(
'cv2'
)
with
open
(
self
.
img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
img_rgb_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
,
channel_order
=
'rgb'
)
assert
img_rgb_turbojpeg
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
img_rgb_turbojpeg
,
img_cv2
[:,
:,
::
-
1
])
# backend turbojpeg, grayscale, decode as 3 channels
with
open
(
self
.
gray_img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
)
assert
gray_img_turbojpeg
.
shape
==
(
300
,
400
,
3
)
assert_array_equal
(
gray_img_rgb_cv2
,
gray_img_turbojpeg
)
# backend turbojpeg, grayscale
with
open
(
self
.
gray_img_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
,
flag
=
'grayscale'
)
assert
gray_img_turbojpeg
.
shape
==
(
300
,
400
)
assert_array_equal
(
gray_img_cv2
,
gray_img_turbojpeg
)
# backend turbojpeg, grayscale dim3
with
open
(
self
.
gray_img_dim3_path
,
'rb'
)
as
f
:
img_bytes
=
f
.
read
()
gray_img_dim3_turbojpeg
=
mmcv
.
imfrombytes
(
img_bytes
,
flag
=
'grayscale'
)
assert
gray_img_dim3_turbojpeg
.
shape
==
(
300
,
400
)
assert_array_equal
(
gray_img_dim3_cv2
,
gray_img_dim3_turbojpeg
)
mmcv
.
use_backend
(
'cv2'
)
def
test_imwrite
(
self
):
img
=
mmcv
.
imread
(
self
.
img_path
)
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'mmcv_test.jpg'
)
mmcv
.
imwrite
(
img
,
out_file
)
rewrite_img
=
mmcv
.
imread
(
out_file
)
os
.
remove
(
out_file
)
self
.
assert_img_equal
(
img
,
rewrite_img
)
ret
=
mmcv
.
imwrite
(
img
,
'./non_exist_path/mmcv_test.jpg'
,
auto_mkdir
=
False
)
assert
ret
is
False
@
patch
(
'mmcv.image.io.TurboJPEG'
,
None
)
def
test_no_turbojpeg
(
self
):
with
pytest
.
raises
(
ImportError
):
mmcv
.
use_backend
(
'turbojpeg'
)
mmcv
.
use_backend
(
'cv2'
)
tests/test_image/test_photometric.py
0 → 100644
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
import
os.path
as
osp
import
cv2
import
numpy
as
np
from
numpy.testing
import
assert_array_equal
import
mmcv
class
TestPhotometric
:
@
classmethod
def
setup_class
(
cls
):
# the test img resolution is 400x300
cls
.
img_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'../data/color.jpg'
)
cls
.
img
=
cv2
.
imread
(
cls
.
img_path
)
cls
.
mean
=
np
.
array
([
123.675
,
116.28
,
103.53
],
dtype
=
np
.
float32
)
cls
.
std
=
np
.
array
([
58.395
,
57.12
,
57.375
],
dtype
=
np
.
float32
)
def
test_imnormalize
(
self
):
rgb_img
=
self
.
img
[:,
:,
::
-
1
]
baseline
=
(
rgb_img
-
self
.
mean
)
/
self
.
std
img
=
mmcv
.
imnormalize
(
self
.
img
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
!=
id
(
self
.
img
)
img
=
mmcv
.
imnormalize
(
rgb_img
,
self
.
mean
,
self
.
std
,
to_rgb
=
False
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
!=
id
(
rgb_img
)
def
test_imnormalize_
(
self
):
img_for_normalize
=
np
.
float32
(
self
.
img
)
rgb_img_for_normalize
=
np
.
float32
(
self
.
img
[:,
:,
::
-
1
])
baseline
=
(
rgb_img_for_normalize
-
self
.
mean
)
/
self
.
std
img
=
mmcv
.
imnormalize_
(
img_for_normalize
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img_for_normalize
,
baseline
)
assert
id
(
img
)
==
id
(
img_for_normalize
)
img
=
mmcv
.
imnormalize_
(
rgb_img_for_normalize
,
self
.
mean
,
self
.
std
,
to_rgb
=
False
)
assert
np
.
allclose
(
img
,
baseline
)
assert
id
(
img
)
==
id
(
rgb_img_for_normalize
)
def
test_imdenormalize
(
self
):
norm_img
=
(
self
.
img
[:,
:,
::
-
1
]
-
self
.
mean
)
/
self
.
std
rgb_baseline
=
(
norm_img
*
self
.
std
+
self
.
mean
)
bgr_baseline
=
rgb_baseline
[:,
:,
::
-
1
]
img
=
mmcv
.
imdenormalize
(
norm_img
,
self
.
mean
,
self
.
std
)
assert
np
.
allclose
(
img
,
bgr_baseline
)
img
=
mmcv
.
imdenormalize
(
norm_img
,
self
.
mean
,
self
.
std
,
to_bgr
=
False
)
assert
np
.
allclose
(
img
,
rgb_baseline
)
def
test_iminvert
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
255
,
127
,
0
],
[
254
,
128
,
1
],
[
253
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
iminvert
(
img
),
img_r
)
def
test_solarize
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
0
,
127
,
0
],
[
1
,
127
,
1
],
[
2
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
solarize
(
img
),
img_r
)
img_r
=
np
.
array
([[
0
,
127
,
0
],
[
1
,
128
,
1
],
[
2
,
126
,
2
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
solarize
(
img
,
100
),
img_r
)
def
test_posterize
(
self
):
img
=
np
.
array
([[
0
,
128
,
255
],
[
1
,
127
,
254
],
[
2
,
129
,
253
]],
dtype
=
np
.
uint8
)
img_r
=
np
.
array
([[
0
,
128
,
128
],
[
0
,
0
,
128
],
[
0
,
128
,
128
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
posterize
(
img
,
1
),
img_r
)
img_r
=
np
.
array
([[
0
,
128
,
224
],
[
0
,
96
,
224
],
[
0
,
128
,
224
]],
dtype
=
np
.
uint8
)
assert_array_equal
(
mmcv
.
posterize
(
img
,
3
),
img_r
)
tests/test_hooks.py
→
tests/test_
runner/test_
hooks.py
View file @
a338d43d
File moved
tests/test_runner.py
→
tests/test_runner
/test_runner
.py
View file @
a338d43d
File moved
tests/test_optflow.py
→
tests/test_
video/test_
optflow.py
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
# flake8: noqa
import
os
import
os.path
as
osp
import
tempfile
import
time
import
numpy
as
np
import
pytest
...
...
@@ -14,10 +11,11 @@ import mmcv
def
test_flowread
():
data_dir
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'../data'
)
flow_shape
=
(
60
,
80
,
2
)
# read .flo file
flow
=
mmcv
.
flowread
(
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/
optflow.flo'
))
flow
=
mmcv
.
flowread
(
osp
.
join
(
data_dir
,
'
optflow.flo'
))
assert
flow
.
shape
==
flow_shape
# pseudo read
...
...
@@ -26,21 +24,19 @@ def test_flowread():
# read quantized flow concatenated vertically
flow
=
mmcv
.
flowread
(
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/optflow_concat0.jpg'
),
quantize
=
True
,
denorm
=
True
)
osp
.
join
(
data_dir
,
'optflow_concat0.jpg'
),
quantize
=
True
,
denorm
=
True
)
assert
flow
.
shape
==
flow_shape
# read quantized flow concatenated horizontally
flow
=
mmcv
.
flowread
(
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/
optflow_concat1.jpg'
),
osp
.
join
(
data_dir
,
'
optflow_concat1.jpg'
),
quantize
=
True
,
concat_axis
=
1
,
denorm
=
True
)
assert
flow
.
shape
==
flow_shape
# test exceptions
notflow_file
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/
color.jpg'
)
notflow_file
=
osp
.
join
(
data_dir
,
'
color.jpg'
)
with
pytest
.
raises
(
TypeError
):
mmcv
.
flowread
(
1
)
with
pytest
.
raises
(
IOError
):
...
...
@@ -188,74 +184,74 @@ def test_make_color_wheel():
color_wheel
=
mmcv
.
make_color_wheel
([
2
,
2
,
2
,
2
,
2
,
2
])
# yapf: disable
assert_array_equal
(
default_color_wheel
,
np
.
array
(
[[
1.
,
0.
,
0.
],
[
1.
,
0.06666667
,
0.
],
[
1.
,
0.13333334
,
0.
],
[
1.
,
0.2
,
0.
],
[
1.
,
0.26666668
,
0.
],
[
1.
,
0.33333334
,
0.
],
[
1.
,
0.4
,
0.
],
[
1.
,
0.46666667
,
0.
],
[
1.
,
0.53333336
,
0.
],
[
1.
,
0.6
,
0.
],
[
1.
,
0.6666667
,
0.
],
[
1.
,
0.73333335
,
0.
],
[
1.
,
0.8
,
0.
],
[
1.
,
0.8666667
,
0.
],
[
1.
,
0.93333334
,
0.
],
[
1.
,
1.
,
0.
],
[
0.8333333
,
1.
,
0.
],
[
0.6666667
,
1.
,
0.
],
[
0.5
,
1.
,
0.
],
[
0.33333334
,
1.
,
0.
],
[
0.16666667
,
1.
,
0.
],
[
0.
,
1.
,
0.
],
[
0.
,
1.
,
0.25
],
[
0.
,
1.
,
0.5
],
[
0.
,
1.
,
0.75
],
[
0.
,
1.
,
1.
],
[
0.
,
0.90909094
,
1.
],
[
0.
,
0.8181818
,
1.
],
[
0.
,
0.72727275
,
1.
],
[
0.
,
0.6363636
,
1.
],
[
0.
,
0.54545456
,
1.
],
[
0.
,
0.45454547
,
1.
],
[
0.
,
0.36363637
,
1.
],
[
0.
,
0.27272728
,
1.
],
[
0.
,
0.18181819
,
1.
],
[
0.
,
0.09090909
,
1.
],
[
0.
,
0.
,
1.
],
[
0.07692308
,
0.
,
1.
],
[
0.15384616
,
0.
,
1.
],
[
0.23076923
,
0.
,
1.
],
[
0.30769232
,
0.
,
1.
],
[
0.3846154
,
0.
,
1.
],
[
0.46153846
,
0.
,
1.
],
[
0.53846157
,
0.
,
1.
],
[
0.61538464
,
0.
,
1.
],
[
0.6923077
,
0.
,
1.
],
[
0.7692308
,
0.
,
1.
],
[
0.84615386
,
0.
,
1.
],
[
0.9230769
,
0.
,
1.
],
[
1.
,
0.
,
1.
],
[
1.
,
0.
,
0.8333333
],
[
1.
,
0.
,
0.6666667
],
[
1.
,
0.
,
0.5
],
[
1.
,
0.
,
0.33333334
],
[
1.
,
0.
,
0.16666667
]],
dtype
=
np
.
float32
))
[[
1.
,
0.
,
0.
],
# noqa
[
1.
,
0.06666667
,
0.
],
# noqa
[
1.
,
0.13333334
,
0.
],
# noqa
[
1.
,
0.2
,
0.
],
# noqa
[
1.
,
0.26666668
,
0.
],
# noqa
[
1.
,
0.33333334
,
0.
],
# noqa
[
1.
,
0.4
,
0.
],
# noqa
[
1.
,
0.46666667
,
0.
],
# noqa
[
1.
,
0.53333336
,
0.
],
# noqa
[
1.
,
0.6
,
0.
],
# noqa
[
1.
,
0.6666667
,
0.
],
# noqa
[
1.
,
0.73333335
,
0.
],
# noqa
[
1.
,
0.8
,
0.
],
# noqa
[
1.
,
0.8666667
,
0.
],
# noqa
[
1.
,
0.93333334
,
0.
],
# noqa
[
1.
,
1.
,
0.
],
# noqa
[
0.8333333
,
1.
,
0.
],
# noqa
[
0.6666667
,
1.
,
0.
],
# noqa
[
0.5
,
1.
,
0.
],
# noqa
[
0.33333334
,
1.
,
0.
],
# noqa
[
0.16666667
,
1.
,
0.
],
# noqa
[
0.
,
1.
,
0.
],
# noqa
[
0.
,
1.
,
0.25
],
# noqa
[
0.
,
1.
,
0.5
],
# noqa
[
0.
,
1.
,
0.75
],
# noqa
[
0.
,
1.
,
1.
],
# noqa
[
0.
,
0.90909094
,
1.
],
# noqa
[
0.
,
0.8181818
,
1.
],
# noqa
[
0.
,
0.72727275
,
1.
],
# noqa
[
0.
,
0.6363636
,
1.
],
# noqa
[
0.
,
0.54545456
,
1.
],
# noqa
[
0.
,
0.45454547
,
1.
],
# noqa
[
0.
,
0.36363637
,
1.
],
# noqa
[
0.
,
0.27272728
,
1.
],
# noqa
[
0.
,
0.18181819
,
1.
],
# noqa
[
0.
,
0.09090909
,
1.
],
# noqa
[
0.
,
0.
,
1.
],
# noqa
[
0.07692308
,
0.
,
1.
],
# noqa
[
0.15384616
,
0.
,
1.
],
# noqa
[
0.23076923
,
0.
,
1.
],
# noqa
[
0.30769232
,
0.
,
1.
],
# noqa
[
0.3846154
,
0.
,
1.
],
# noqa
[
0.46153846
,
0.
,
1.
],
# noqa
[
0.53846157
,
0.
,
1.
],
# noqa
[
0.61538464
,
0.
,
1.
],
# noqa
[
0.6923077
,
0.
,
1.
],
# noqa
[
0.7692308
,
0.
,
1.
],
# noqa
[
0.84615386
,
0.
,
1.
],
# noqa
[
0.9230769
,
0.
,
1.
],
# noqa
[
1.
,
0.
,
1.
],
# noqa
[
1.
,
0.
,
0.8333333
],
# noqa
[
1.
,
0.
,
0.6666667
],
# noqa
[
1.
,
0.
,
0.5
],
# noqa
[
1.
,
0.
,
0.33333334
],
# noqa
[
1.
,
0.
,
0.16666667
]],
dtype
=
np
.
float32
))
# noqa
assert_array_equal
(
color_wheel
,
np
.
array
([[
1.
,
0.
,
0.
],
[
1.
,
0.5
,
0.
],
[
1.
,
1.
,
0.
],
[
0.5
,
1.
,
0.
],
[
0.
,
1.
,
0.
],
[
0.
,
1.
,
0.5
],
[
0.
,
1.
,
1.
],
[
0.
,
0.5
,
1.
],
[
0.
,
0.
,
1.
],
[
0.5
,
0.
,
1.
],
[
1.
,
0.
,
1.
],
[
1.
,
0.
,
0.5
]],
dtype
=
np
.
float32
))
np
.
array
([[
1.
,
0.
,
0.
],
# noqa
[
1.
,
0.5
,
0.
],
# noqa
[
1.
,
1.
,
0.
],
# noqa
[
0.5
,
1.
,
0.
],
# noqa
[
0.
,
1.
,
0.
],
# noqa
[
0.
,
1.
,
0.5
],
# noqa
[
0.
,
1.
,
1.
],
# noqa
[
0.
,
0.5
,
1.
],
# noqa
[
0.
,
0.
,
1.
],
# noqa
[
0.5
,
0.
,
1.
],
# noqa
[
1.
,
0.
,
1.
],
# noqa
[
1.
,
0.
,
0.5
]],
dtype
=
np
.
float32
))
# noqa
# yapf: enable
tests/test_video/test_processing.py
0 → 100644
View file @
a338d43d
# Copyright (c) Open-MMLab. All rights reserved.
import
os
import
os.path
as
osp
import
tempfile
import
mmcv
class
TestVideoEditor
:
@
classmethod
def
setup_class
(
cls
):
cls
.
video_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'../data/test.mp4'
)
cls
.
num_frames
=
168
def
test_cut_concat_video
(
self
):
part1_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test1.mp4'
)
part2_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test2.mp4'
)
mmcv
.
cut_video
(
self
.
video_path
,
part1_file
,
end
=
3
,
vcodec
=
'h264'
)
mmcv
.
cut_video
(
self
.
video_path
,
part2_file
,
start
=
3
,
vcodec
=
'h264'
)
v1
=
mmcv
.
VideoReader
(
part1_file
)
v2
=
mmcv
.
VideoReader
(
part2_file
)
assert
len
(
v1
)
==
75
assert
len
(
v2
)
==
self
.
num_frames
-
75
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test.mp4'
)
mmcv
.
concat_video
([
part1_file
,
part2_file
],
out_file
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
len
(
v
)
==
self
.
num_frames
os
.
remove
(
part1_file
)
os
.
remove
(
part2_file
)
os
.
remove
(
out_file
)
def
test_resize_video
(
self
):
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test.mp4'
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
(
200
,
100
),
quiet
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
200
,
100
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
ratio
=
2
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
240
*
2
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
(
1000
,
480
),
keep_ar
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
240
*
2
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
ratio
=
(
2
,
1.5
),
keep_ar
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
360
)
os
.
remove
(
out_file
)
tests/test_video.py
→
tests/test_video
/test_reader
.py
View file @
a338d43d
...
...
@@ -10,7 +10,7 @@ import pytest
import
mmcv
class
TestCache
(
object
)
:
class
TestCache
:
def
test_init
(
self
):
with
pytest
.
raises
(
ValueError
):
...
...
@@ -39,11 +39,11 @@ class TestCache(object):
assert
cache
.
get
(
'k1'
)
==
1
class
TestVideo
(
object
)
:
class
TestVideo
Reader
:
@
classmethod
def
setup_class
(
cls
):
cls
.
video_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/test.mp4'
)
cls
.
video_path
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../
data/test.mp4'
)
cls
.
num_frames
=
168
def
test_load
(
self
):
...
...
@@ -196,41 +196,3 @@ class TestVideo(object):
os
.
remove
(
filename
)
shutil
.
rmtree
(
frame_dir
)
os
.
remove
(
out_filename
)
def
test_cut_concat_video
(
self
):
part1_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test1.mp4'
)
part2_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test2.mp4'
)
mmcv
.
cut_video
(
self
.
video_path
,
part1_file
,
end
=
3
,
vcodec
=
'h264'
)
mmcv
.
cut_video
(
self
.
video_path
,
part2_file
,
start
=
3
,
vcodec
=
'h264'
)
v1
=
mmcv
.
VideoReader
(
part1_file
)
v2
=
mmcv
.
VideoReader
(
part2_file
)
assert
len
(
v1
)
==
75
assert
len
(
v2
)
==
self
.
num_frames
-
75
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test.mp4'
)
mmcv
.
concat_video
([
part1_file
,
part2_file
],
out_file
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
len
(
v
)
==
self
.
num_frames
os
.
remove
(
part1_file
)
os
.
remove
(
part2_file
)
os
.
remove
(
out_file
)
def
test_resize_video
(
self
):
out_file
=
osp
.
join
(
tempfile
.
gettempdir
(),
'.mmcv_test.mp4'
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
(
200
,
100
),
quiet
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
200
,
100
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
ratio
=
2
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
240
*
2
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
(
1000
,
480
),
keep_ar
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
240
*
2
)
os
.
remove
(
out_file
)
mmcv
.
resize_video
(
self
.
video_path
,
out_file
,
ratio
=
(
2
,
1.5
),
keep_ar
=
True
)
v
=
mmcv
.
VideoReader
(
out_file
)
assert
v
.
resolution
==
(
294
*
2
,
360
)
os
.
remove
(
out_file
)
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