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
f90567a0
Commit
f90567a0
authored
Jun 24, 2022
by
liukuikun
Committed by
zhouzaida
Jul 19, 2022
Browse files
[Fix] LoadImageFromFile
parent
864942be
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
12 deletions
+33
-12
mmcv/transforms/loading.py
mmcv/transforms/loading.py
+22
-11
tests/test_transforms/test_transforms_loading.py
tests/test_transforms/test_transforms_loading.py
+11
-1
No files found.
mmcv/transforms/loading.py
View file @
f90567a0
# Copyright (c) OpenMMLab. All rights reserved.
from
typing
import
Optional
import
numpy
as
np
import
mmcv
...
...
@@ -33,22 +35,24 @@ class LoadImageFromFile(BaseTransform):
file_client_args (dict): Arguments to instantiate a FileClient.
See :class:`mmcv.fileio.FileClient` for details.
Defaults to ``dict(backend='disk')``.
ignore_empty (bool): Whether to allow loading empty image or file path
not existent. Defaults to False.
"""
def
__init__
(
self
,
def
__init__
(
self
,
to_float32
:
bool
=
False
,
color_type
:
str
=
'color'
,
imdecode_backend
:
str
=
'cv2'
,
file_client_args
:
dict
=
dict
(
backend
=
'disk'
)
)
->
None
:
file_client_args
:
dict
=
dict
(
backend
=
'disk'
),
ignore_empty
:
bool
=
False
)
->
None
:
self
.
ignore_empty
=
ignore_empty
self
.
to_float32
=
to_float32
self
.
color_type
=
color_type
self
.
imdecode_backend
=
imdecode_backend
self
.
file_client_args
=
file_client_args
.
copy
()
self
.
file_client
=
mmcv
.
FileClient
(
**
self
.
file_client_args
)
def
transform
(
self
,
results
:
dict
)
->
dict
:
def
transform
(
self
,
results
:
dict
)
->
Optional
[
dict
]
:
"""Functions to load image.
Args:
...
...
@@ -59,9 +63,15 @@ class LoadImageFromFile(BaseTransform):
"""
filename
=
results
[
'img_path'
]
try
:
img_bytes
=
self
.
file_client
.
get
(
filename
)
img
=
mmcv
.
imfrombytes
(
img_bytes
,
flag
=
self
.
color_type
,
backend
=
self
.
imdecode_backend
)
except
Exception
as
e
:
if
self
.
ignore_empty
:
return
None
else
:
raise
e
if
self
.
to_float32
:
img
=
img
.
astype
(
np
.
float32
)
...
...
@@ -72,6 +82,7 @@ class LoadImageFromFile(BaseTransform):
def
__repr__
(
self
):
repr_str
=
(
f
'
{
self
.
__class__
.
__name__
}
('
f
'ignore_empty=
{
self
.
ignore_empty
}
, '
f
'to_float32=
{
self
.
to_float32
}
, '
f
"color_type='
{
self
.
color_type
}
', "
f
"imdecode_backend='
{
self
.
imdecode_backend
}
', "
...
...
tests/test_transforms/test_transforms_loading.py
View file @
f90567a0
...
...
@@ -3,6 +3,7 @@ import copy
import
os.path
as
osp
import
numpy
as
np
import
pytest
from
mmcv.transforms
import
LoadAnnotations
,
LoadImageFromFile
...
...
@@ -21,7 +22,7 @@ class TestLoadImageFromFile:
assert
results
[
'img_shape'
]
==
(
300
,
400
)
assert
results
[
'ori_shape'
]
==
(
300
,
400
)
assert
repr
(
transform
)
==
transform
.
__class__
.
__name__
+
\
"(to_float32=False, color_type='color', "
+
\
"(
ignore_empty=False,
to_float32=False, color_type='color', "
+
\
"imdecode_backend='cv2', file_client_args={'backend': 'disk'})"
# to_float32
...
...
@@ -41,6 +42,15 @@ class TestLoadImageFromFile:
assert
results
[
'img'
].
shape
==
(
300
,
400
)
assert
results
[
'img'
].
dtype
==
np
.
uint8
# test load empty
fake_img_path
=
osp
.
join
(
data_prefix
,
'fake.jpg'
)
results
[
'img_path'
]
=
fake_img_path
transform
=
LoadImageFromFile
(
ignore_empty
=
False
)
with
pytest
.
raises
(
FileNotFoundError
):
transform
(
copy
.
deepcopy
(
results
))
transform
=
LoadImageFromFile
(
ignore_empty
=
True
)
assert
transform
(
copy
.
deepcopy
(
results
))
is
None
class
TestLoadAnnotations
:
...
...
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