| import os |
| import datasets |
|
|
| _CITATION = "https://arxiv.org/abs/2012.12453" |
| _DESCRIPTION = "CholecSeg8K dataset for semantic segmentation in laparoscopic cholecystectomy surgery." |
| _HOMEPAGE_URL = "https://www.kaggle.com/datasets/newslab/cholecseg8k" |
| _DATA_URL = "data/CholecSeg8k.zip" |
| _LICENSE= "cc-by-nc-sa-4.0" |
|
|
| class CholecSeg8KConfig(datasets.BuilderConfig): |
| """ |
| BuilderConfig for CholecSeg8k dataset. |
| |
| Args: |
| name (str): Name of the configuration. |
| description (str): Description of the dataset. |
| homepage (str): Homepage URL of the dataset. |
| data_url (str): URL to download the dataset. |
| """ |
| |
| def __init__(self, name, description, homepage, data_url): |
| super(CholecSeg8KConfig, self).__init__( |
| name=self.name, |
| version=datasets.Version("1.0.0"), |
| description=self.description, |
| ) |
| self.name = name |
| self.description = description |
| self.homepage = homepage |
| self.data_url = data_url |
|
|
| def _build_config(name): |
| """Builds and returns the dataset configuration.""" |
| return CholecSeg8KConfig( |
| name=name, |
| description=_DESCRIPTION, |
| homepage=_HOMEPAGE_URL, |
| data_url=_DATA_URL, |
| ) |
|
|
| class CholecSeg8K(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [_build_config("all")] |
|
|
| def _info(self): |
| """Returns the dataset info.""" |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=datasets.Features( |
| { |
| "image": datasets.Image(), |
| "color_mask": datasets.Image(), |
| "watershed_mask": datasets.Image(), |
| "annotation_mask": datasets.Image(), |
| } |
| ), |
| supervised_keys=None, |
| homepage=_HOMEPAGE_URL, |
| citation=_CITATION, |
| license=_LICENSE, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Generates data splits.""" |
| datapath = dl_manager.download_and_extract(_DATA_URL) |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={"datapath": datapath}, |
| ), |
| ] |
|
|
| def _generate_examples(self, datapath): |
| """Yields examples.""" |
| key=0 |
| datapath = os.path.join(datapath, "CholecSeg8k") |
| |
| for video_folder in os.listdir(datapath): |
| video_folder_path = os.path.join(datapath, video_folder) |
|
|
| for clip_folder in os.listdir(video_folder_path): |
| clip_folder_path = os.path.join(video_folder_path, clip_folder) |
|
|
| for file in os.listdir(clip_folder_path): |
| if file.endswith("_endo.png"): |
| image_path = os.path.join(clip_folder_path, file) |
|
|
| |
| base_filename = file.replace("_endo.png", "") |
| color_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_color_mask.png") |
| watershed_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_watershed_mask.png") |
| annotation_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_mask.png") |
|
|
| yield key, { |
| "image": image_path, |
| "color_mask": color_mask_path, |
| "watershed_mask": watershed_mask_path, |
| "annotation_mask": annotation_mask_path, |
| } |
| key+=1 |