MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / write_segmentation

Function write_segmentation

eval_code/recons/models/moge/utils/io.py:179–198  ·  view source on GitHub ↗

Write a segmentation mask and label mapping, as PNG format. ### Parameters: - `path: Union[str, os.PathLike, IO]` The file path or file object to write to. - `mask: np.ndarray` The segmentation mask, uint8 or uint16 array of shape (H, W). - `labels: Dict[str, int

(path: Union[str, os.PathLike, IO], mask: np.ndarray, labels: Dict[str, int] = None, compression_level: int = 7)

Source from the content-addressed store, hash-verified

177
178
179def write_segmentation(path: Union[str, os.PathLike, IO], mask: np.ndarray, labels: Dict[str, int] = None, compression_level: int = 7):
180 """
181 Write a segmentation mask and label mapping, as PNG format.
182 ### Parameters:
183 - `path: Union[str, os.PathLike, IO]`
184 The file path or file object to write to.
185 - `mask: np.ndarray`
186 The segmentation mask, uint8 or uint16 array of shape (H, W).
187 - `labels: Dict[str, int] = None`
188 The label mapping, a dictionary of {label_name: label_id}.
189 - `compression_level: int = 7`
190 The compression level for PNG compression.
191 """
192 assert mask.dtype == np.uint8 or mask.dtype == np.uint16, f"Unsupported dtype {mask.dtype}"
193 pil_image = Image.fromarray(mask)
194 pnginfo = PngImagePlugin.PngInfo()
195 if labels is not None:
196 labels_json = json.dumps(labels, ensure_ascii=True, separators=(',', ':'))
197 pnginfo.add_text('labels', labels_json)
198 pil_image.save(path, pnginfo=pnginfo, compress_level=compression_level)
199
200
201

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected