11# Copyright (c) OpenMMLab. All rights reserved.
2+ import warnings
23from typing import Optional
34
45import mmengine .fileio as fileio
@@ -33,6 +34,11 @@ class LoadImageFromFile(BaseTransform):
3334 argument for :func:`mmcv.imfrombytes`.
3435 See :func:`mmcv.imfrombytes` for details.
3536 Defaults to 'cv2'.
37+ file_client_args (dict, optional): Arguments to instantiate a
38+ FileClient. See :class:`mmengine.fileio.FileClient` for details.
39+ Defaults to None. It will be deprecated in future. Please use
40+ ``backend_args`` instead.
41+ Deprecated in version 2.0.0rc4.
3642 ignore_empty (bool): Whether to allow loading empty image or file path
3743 not existent. Defaults to False.
3844 backend_args (dict, optional): Instantiates the corresponding file
@@ -41,20 +47,36 @@ class LoadImageFromFile(BaseTransform):
4147 value will be used and initialized with the remaining values,
4248 otherwise the corresponding file backend will be selected
4349 based on the prefix of the file path. Defaults to None.
50+ New in version 2.0.0rc4.
4451 """
4552
4653 def __init__ (self ,
4754 to_float32 : bool = False ,
4855 color_type : str = 'color' ,
4956 imdecode_backend : str = 'cv2' ,
57+ file_client_args : Optional [dict ] = None ,
5058 ignore_empty : bool = False ,
5159 * ,
5260 backend_args : Optional [dict ] = None ) -> None :
5361 self .ignore_empty = ignore_empty
5462 self .to_float32 = to_float32
5563 self .color_type = color_type
5664 self .imdecode_backend = imdecode_backend
57- self .backend_args = backend_args
65+
66+ self .file_client_args : Optional [dict ] = None
67+ self .backend_args : Optional [dict ] = None
68+ if file_client_args is not None :
69+ warnings .warn (
70+ '"file_client_args" will be deprecated in future. '
71+ 'Please use "backend_args" instead' , DeprecationWarning )
72+ if backend_args is not None :
73+ raise ValueError (
74+ '"file_client_args" and "backend_args" cannot be set '
75+ 'at the same time.' )
76+
77+ self .file_client_args = file_client_args .copy ()
78+ if backend_args is not None :
79+ self .backend_args = backend_args .copy ()
5880
5981 def transform (self , results : dict ) -> Optional [dict ]:
6082 """Functions to load image.
@@ -69,7 +91,13 @@ def transform(self, results: dict) -> Optional[dict]:
6991
7092 filename = results ['img_path' ]
7193 try :
72- img_bytes = fileio .get (filename , backend_args = self .backend_args )
94+ if self .file_client_args is not None :
95+ file_client = fileio .FileClient .infer_client (
96+ self .file_client_args , filename )
97+ img_bytes = file_client .get (filename )
98+ else :
99+ img_bytes = fileio .get (
100+ filename , backend_args = self .backend_args )
73101 img = mmcv .imfrombytes (
74102 img_bytes , flag = self .color_type , backend = self .imdecode_backend )
75103 except Exception as e :
@@ -90,12 +118,12 @@ def __repr__(self):
90118 f'ignore_empty={ self .ignore_empty } , '
91119 f'to_float32={ self .to_float32 } , '
92120 f"color_type='{ self .color_type } ', "
93- f"imdecode_backend='{ self .imdecode_backend } '" )
121+ f"imdecode_backend='{ self .imdecode_backend } ', " )
94122
95- if self .backend_args is not None :
96- repr_str += f', backend_args ={ self .backend_args } )'
123+ if self .file_client_args is not None :
124+ repr_str += f'file_client_args ={ self .file_client_args } )'
97125 else :
98- repr_str += ' )'
126+ repr_str += f'backend_args= { self . backend_args } )'
99127
100128 return repr_str
101129
@@ -177,12 +205,18 @@ class LoadAnnotations(BaseTransform):
177205 argument for :func:`mmcv.imfrombytes`.
178206 See :func:`mmcv.imfrombytes` for details.
179207 Defaults to 'cv2'.
208+ file_client_args (dict, optional): Arguments to instantiate a
209+ FileClient. See :class:`mmengine.fileio.FileClient` for details.
210+ Defaults to None. It will be deprecated in future. Please use
211+ ``backend_args`` instead.
212+ Deprecated in version 2.0.0rc4.
180213 backend_args (dict, optional): Instantiates the corresponding file
181214 backend. It may contain `backend` key to specify the file
182215 backend. If it contains, the file backend corresponding to this
183216 value will be used and initialized with the remaining values,
184217 otherwise the corresponding file backend will be selected
185218 based on the prefix of the file path. Defaults to None.
219+ New in version 2.0.0rc4.
186220 """
187221
188222 def __init__ (
@@ -192,6 +226,7 @@ def __init__(
192226 with_seg : bool = False ,
193227 with_keypoints : bool = False ,
194228 imdecode_backend : str = 'cv2' ,
229+ file_client_args : Optional [dict ] = None ,
195230 * ,
196231 backend_args : Optional [dict ] = None ,
197232 ) -> None :
@@ -201,7 +236,21 @@ def __init__(
201236 self .with_seg = with_seg
202237 self .with_keypoints = with_keypoints
203238 self .imdecode_backend = imdecode_backend
204- self .backend_args = backend_args
239+
240+ self .file_client_args : Optional [dict ] = None
241+ self .backend_args : Optional [dict ] = None
242+ if file_client_args is not None :
243+ warnings .warn (
244+ '"file_client_args" will be deprecated in future. '
245+ 'Please use "backend_args" instead' , DeprecationWarning )
246+ if backend_args is not None :
247+ raise ValueError (
248+ '"file_client_args" and "backend_args" cannot be set '
249+ 'at the same time.' )
250+
251+ self .file_client_args = file_client_args .copy ()
252+ if backend_args is not None :
253+ self .backend_args = backend_args .copy ()
205254
206255 def _load_bboxes (self , results : dict ) -> None :
207256 """Private function to load bounding box annotations.
@@ -245,9 +294,14 @@ def _load_seg_map(self, results: dict) -> None:
245294 Returns:
246295 dict: The dict contains loaded semantic segmentation annotations.
247296 """
297+ if self .file_client_args is not None :
298+ file_client = fileio .FileClient .infer_client (
299+ self .file_client_args , results ['seg_map_path' ])
300+ img_bytes = file_client .get (results ['seg_map_path' ])
301+ else :
302+ img_bytes = fileio .get (
303+ results ['seg_map_path' ], backend_args = self .backend_args )
248304
249- img_bytes = fileio .get (
250- results ['seg_map_path' ], backend_args = self .backend_args )
251305 results ['gt_seg_map' ] = mmcv .imfrombytes (
252306 img_bytes , flag = 'unchanged' ,
253307 backend = self .imdecode_backend ).squeeze ()
@@ -296,11 +350,11 @@ def __repr__(self) -> str:
296350 repr_str += f'with_label={ self .with_label } , '
297351 repr_str += f'with_seg={ self .with_seg } , '
298352 repr_str += f'with_keypoints={ self .with_keypoints } , '
299- repr_str += f"imdecode_backend='{ self .imdecode_backend } '"
353+ repr_str += f"imdecode_backend='{ self .imdecode_backend } ', "
300354
301- if self .backend_args is not None :
302- repr_str += f', backend_args ={ self .backend_args } )'
355+ if self .file_client_args is not None :
356+ repr_str += f'file_client_args ={ self .file_client_args } )'
303357 else :
304- repr_str += ' )'
358+ repr_str += f'backend_args= { self . backend_args } )'
305359
306360 return repr_str
0 commit comments