zensvi.cv.Segmenter

class zensvi.cv.Segmenter(dataset='cityscapes', task='semantic', device=None, verbosity=1)

A class for performing semantic and panoptic segmentation on images.

The models used are from the Mask2Former (https://huggingface.co/docs/transformers/model_doc/mask2former).

device

The device to run the model on (e.g., “cuda” or “cpu”).

Type:

str

dataset

The name of the dataset (e.g., “cityscapes” or “mapillary”).

Type:

str

task

The type of segmentation task (e.g., “semantic” or “panoptic”).

Type:

str

model_name

The name of the pre-trained model corresponding to the dataset and task.

Type:

str

model

The segmentation model.

processor

The image processor for the model.

color_map

A mapping of class IDs to colors.

label_map

A mapping of class IDs to labels.

id_to_name_map

A mapping of label IDs to label names.

verbosity

Level of verbosity for progress bars (0=no progress, 1=outer loops only, 2=all loops)

Type:

int

Parameters:
  • dataset (str) – The name of the dataset (default is “cityscapes”).

  • task (str) – The type of task (default is “semantic”).

  • device (str, optional) – The device to run the model on (e.g., “cuda” or “cpu”). If None, the default device will be used.

  • verbosity (int, optional) – Level of verbosity for progress bars (0=no progress, 1=outer loops only, 2=all loops). Default is 1.

Returns:

None

segment(dir_input: str | pathlib.Path, dir_image_output: str | pathlib.Path | None = None, dir_summary_output: str | pathlib.Path | None = None, batch_size=1, save_image_options='segmented_image blend_image', save_format='json csv', csv_format='long', max_workers: int | None = None)

Processes a batch of images for segmentation, saves the segmented images and summary statistics.

This method handles the processing of images for segmentation, managing input/output directories, saving options, and parallel processing settings. The method requires specifying an input directory or a path to a single image and supports optional saving of output images and segmentation summaries.

Parameters:
  • dir_input – Input directory or path to a single image file

  • dir_image_output – Output directory where segmented images are saved

  • dir_summary_output – Output directory where segmentation summary files are saved

  • batch_size – Batch size for processing images (Default: 1)

  • save_image_options – Options for saving images (“segmented_image blend_image”)

  • save_format – Format for saving summary files (“json csv”)

  • csv_format – Format for CSV summary files (“long” or “wide”)

  • max_workers – Maximum number of workers for parallel processing

Returns:

The method does not return any value but saves the processed results to specified directories.

Return type:

None

Raises:
  • ValueError – If neither dir_image_output nor dir_summary_output is provided

  • ValueError – If the input path is neither a file nor a directory

calculate_pixel_ratio_post_process(dir_input, dir_output, save_format='json csv')

Calculates the pixel ratio of different classes present in the segmented images and saves the results in either JSON or CSV format.

Parameters:
  • dir_input – A string or Path object representing the input directory containing the segmented images.

  • dir_output – A string or Path object representing the output directory where the pixel ratio results will be saved.

  • save_format – A list containing the file formats in which the results will be saved. The allowed file formats are “json” and “csv”. The default value is “json csv”.

Returns:

None