File size: 7,164 Bytes
a62c8cb
 
 
fc20259
 
8baeea4
a62c8cb
 
 
431e811
a62c8cb
431e811
 
 
 
 
a62c8cb
 
 
431e811
a62c8cb
 
431e811
a62c8cb
 
431e811
a62c8cb
 
d2599ae
a62c8cb
 
3615276
a62c8cb
431e811
a62c8cb
02b5ff2
 
 
 
e000a58
a592cec
e7ff388
c759c95
 
 
 
02b5ff2
 
 
7d1e22e
 
 
 
 
 
 
 
d736198
 
 
 
02b5ff2
 
 
 
 
 
 
 
 
 
d3c5791
a62c8cb
f3dacf5
7ac7cd2
a62507a
 
afb6bc0
173fe72
5c31efa
4f297f5
787cb93
4f297f5
 
 
ee683e8
4f297f5
 
 
 
 
3ddf48a
4f297f5
 
ee683e8
8906b34
a64e957
 
 
a1a29f4
d13e499
 
 
 
 
 
5a77adc
 
 
 
7d1e22e
 
 
 
d13e499
 
d736198
7d1e22e
 
 
 
 
 
 
 
d736198
d13e499
 
a64e957
d736198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8314360
ae01135
e000a58
4f297f5
ae01135
a62507a
ae01135
e000a58
 
ae01135
 
 
 
 
c759c95
e000a58
 
c759c95
 
ae01135
4f297f5
 
 
431e811
 
e000a58
a64e957
e000a58
ae01135
 
 
 
 
e000a58
a592cec
b687cd4
dce386e
c759c95
 
 
ae01135
431e811
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import csv
import json
import os
from PIL import Image
import numpy as np
import pandas as pd
import datasets



_CITATION = """\
@article{nature},
  title={Labeled temperate hardwood tree stomatal image datasets from seven taxa of Populus and 17 hardwood species},
  author={Jiaxin Wang, Heidi J. Renninger and Qin Ma},
  journal={Sci Data 11, 1 (2024)},
  year={2024}
"""

_DESCRIPTION = """\
This new dataset is designed to solve image classification and segmentation tasks and is crafted with a lot of care.
"""

_HOMEPAGE = "https://zenodo.org/records/8271253"


_LICENSE = "https://creativecommons.org/licenses/by/4.0/"


class NewDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    VERSION = datasets.Version("1.1.0")

    
    def _info(self):
        features = datasets.Features({
            "image_id": datasets.Value("string"),
            "species": datasets.Value("string"),
            "scientific_name": datasets.Value("string"),
            "image": datasets.Image(),
            "image_path": datasets.Value("string"), 
            "label_path": datasets.Value("string"),
            "magnification": datasets.Value("int32"),
            "width": datasets.Value("int32"),
            "height": datasets.Value("int32"),
            "resolution": datasets.Value("int32"),
            "annotations": datasets.Sequence({
                "category_id": datasets.Value("int32"),
                "bounding_box": {
                    "x_center_rel": datasets.Value("float32"),
                    "y_center_rel": datasets.Value("float32"),
                    "width_rel": datasets.Value("float32"),
                    "height_rel": datasets.Value("float32"),
                    # "x_min": datasets.Value("float32"),
                    # "y_min": datasets.Value("float32"),
                    # "x_max": datasets.Value("float32"),
                    # "y_max": datasets.Value("float32"),
                    # "x_center_rel": datasets.Value("float32"),
                    # "y_center_rel": datasets.Value("float32"),
                    # "width_rel": datasets.Value("float32"),
                    # "height_rel": datasets.Value("float32"),
                },
            }),
        })
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,  # Here we define them because they are different between the two configurations
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        
        data_files = dl_manager.download_and_extract({
            "csv": "https://huggingface.co/datasets/XintongHe/Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.csv",
            "zip": "https://huggingface.co/datasets/XintongHe/Stomatal_Images_Datasets/resolve/main/data/Labeled Stomatal Images.zip"
        })

        
        species_info = pd.read_csv(data_files["csv"])
        extracted_images_path = os.path.join(data_files["zip"], "Labeled Stomatal Images")

        # Get all image filenames
        all_image_filenames = species_info['FileName'].apply(lambda x: x + '.jpg').tolist()
        
        return [datasets.SplitGenerator(
            name=datasets.Split.TRAIN,
            gen_kwargs={
                "filepaths": all_image_filenames,
                "species_info": species_info,
                "data_dir": extracted_images_path
            },
        )]
    

   

        
    def _parse_yolo_labels(self, label_path, width, height):
        annotations = []
        with open(label_path, 'r') as file:
            yolo_data = file.readlines()
    
        for line in yolo_data:
            class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
            # x_min = (x_center_rel - width_rel / 2) * width
            # y_min = (y_center_rel - height_rel / 2) * height
            # x_max = (x_center_rel + width_rel / 2) * width
            # y_max = (y_center_rel + height_rel / 2) * height
            # x_min = x_center_rel 
            # y_min = y_center_rel 
            # x_max = width_rel
            # y_max = height_rel 
            annotations.append({
                "category_id": int(class_id),
                "bounding_box": {
                    "x_center_rel": x_center_rel ,
                    "y_center_rel": y_center_rel,
                    "width_rel": width_rel,
                    "height_rel": height_rel
                    # "x_min": x_min,
                    # "y_min": y_min,
                    # "x_max": x_max,
                    # "y_max": y_max
                }
            })
        return annotations

    # def _parse_yolo_labels(self, label_path, width, height):
    #     annotations = []
    #     with open(label_path, 'r') as file:
    #         yolo_data = file.readlines()
    
    #     for line in yolo_data:
    #         class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
    #         annotations.append({
    #             "category_id": int(class_id),
    #             "x_center_rel": x_center_rel,
    #             "y_center_rel": y_center_rel,
    #             "width_rel": width_rel,
    #             "height_rel": height_rel,
    #         })
    #     return annotations


    
    def _generate_examples(self, filepaths, species_info, data_dir):
        """Yields examples as (key, example) tuples."""
        for file_name in filepaths:
            image_id = os.path.splitext(file_name)[0] 
            image_path = os.path.join(data_dir, f"{image_id}.jpg")
            label_path = os.path.join(data_dir, f"{image_id}.txt")
            img = Image.open(image_path)
            # Find the corresponding row in the CSV for the current image
            species_row = species_info.loc[species_info['FileName'] == image_id]
            if not species_row.empty:
                species = species_row['Species'].values[0]
                scientific_name = species_row['ScientificName'].values[0]
                magnification = species_row['Magnification'].values[0]
                width = species_row['Witdh'].values[0]
                height = species_row['Heigth'].values[0]
                resolution = species_row['Resolution'].values[0]
                
            else:
                # Default values if not found
                species = None
                scientific_name = None
                width = 1024 
                height = 768   
            
            annotations = self._parse_yolo_labels(label_path, width, height)
            
            # Yield the dataset example
            yield image_id, {
                "image_id": image_id,
                "species": species,
                "scientific_name": scientific_name,
                "image": img,
                "image_path": image_path,
                "label_path": label_path,
                "magnification": magnification,
                "width": width,
                "height": height,
                "resolution": resolution,
                "annotations": annotations
            }