File size: 3,685 Bytes
daa5db2
 
 
f6dde9b
daa5db2
 
 
 
 
 
f6dde9b
 
 
 
 
 
 
 
 
daa5db2
 
 
 
 
 
 
 
 
 
 
 
 
f6dde9b
daa5db2
 
 
 
 
 
 
 
 
 
 
 
f6dde9b
daa5db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6dde9b
daa5db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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"):  # Check for endoscopic images
                        image_path = os.path.join(clip_folder_path, file)

                        # Construct paths for each mask type
                        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