MCPcopy Create free account
hub / github.com/modelscope/modelscope / __call__

Method __call__

modelscope/preprocessors/image.py:35–80  ·  view source on GitHub ↗

Call functions to load image and get image meta information. Args: input (str or dict): input image path or input dict with a key `filename`. Returns: dict: The dict contains loaded image.

(self, input: Union[str, Dict[str, str]])

Source from the content-addressed store, hash-verified

33 self.backend = backend
34
35 def __call__(self, input: Union[str, Dict[str, str]]):
36 """Call functions to load image and get image meta information.
37 Args:
38 input (str or dict): input image path or input dict with
39 a key `filename`.
40 Returns:
41 dict: The dict contains loaded image.
42 """
43 if isinstance(input, dict):
44 image_path_or_url = input['filename']
45 else:
46 image_path_or_url = input
47
48 if self.backend == 'cv2':
49 storage = File._get_storage(image_path_or_url)
50 with storage.as_local_path(image_path_or_url) as img_path:
51 img = cv2.imread(img_path, cv2.IMREAD_COLOR)
52 if self.mode == 'RGB':
53 cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
54 img_h, img_w, img_c = img.shape[0], img.shape[1], img.shape[2]
55 img_shape = (img_h, img_w, img_c)
56 elif self.backend == 'pillow':
57 bytes = File.read(image_path_or_url)
58 # TODO @wenmeng.zwm add opencv decode as optional
59 # we should also look at the input format which is the most commonly
60 # used in Mind' image related models
61 with io.BytesIO(bytes) as infile:
62 img = Image.open(infile)
63 img = ImageOps.exif_transpose(img)
64 img = img.convert(self.mode)
65 img_shape = (img.size[1], img.size[0], 3)
66 else:
67 raise TypeError(f'backend should be either cv2 or pillow,'
68 f'but got {self.backend}')
69
70 results = {
71 'filename': image_path_or_url,
72 'img': img,
73 'img_shape': img_shape,
74 'img_field': 'img',
75 }
76 if isinstance(input, dict):
77 input_ret = input.copy()
78 input_ret.update(results)
79 results = input_ret
80 return results
81
82 def __repr__(self):
83 repr_str = f'{self.__class__.__name__}(' f'mode={self.mode})'

Callers

nothing calls this directly

Calls 7

_get_storageMethod · 0.80
openMethod · 0.80
as_local_pathMethod · 0.45
readMethod · 0.45
convertMethod · 0.45
copyMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected