File size: 5,310 Bytes
74f3479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8f186
74f3479
 
 
 
 
ed8f186
74f3479
 
 
 
 
 
 
 
ed8f186
07c3866
74f3479
 
 
 
 
 
 
 
 
 
23be2b5
 
aef8960
ed8f186
23be2b5
ed8f186
74f3479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584ffa2
ed8f186
e5ecdf2
74f3479
 
 
 
 
ed8f186
74f3479
 
 
 
 
 
ed8f186
74f3479
 
 
 
ed8f186
74f3479
 
 
 
 
 
 
 
 
e5ecdf2
ed8f186
5871d59
07c3866
 
74f3479
 
 
07c3866
74f3479
 
07c3866
 
74f3479
 
 
 
5871d59
07c3866
5871d59
23be2b5
5871d59
74f3479
 
 
07c3866
 
 
 
23be2b5
07c3866
74f3479
 
 
 
 
 
 
 
 
 
 
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
import json
import os

import datasets


_CITATION = """\
@SIA86{huggingface:dataset,
title = {WaterFlowCountersRecognition dataset},
author={SIA86},
year={2023}
}
"""

_DESCRIPTION = """\
This dataset is designed to detect digital data from water flow counters photos.
"""
_HOMEPAGE = "https://github.com/SIA86/WaterFlowRecognition"

_REGION_NAME = ['value_a', 'value_b', 'serial']

_REGION_ROTETION = ['0', '90', '180', '270']




class WaterFlowCounterConfig(datasets.BuilderConfig):
    """Builder Config for WaterFlowCounter"""

    def __init__(self, data_url, metadata_url, **kwargs):
        """BuilderConfig for WaterFlowCounter.
        Args:
          data_url: `string`, url to download the photos.
          metadata_urls: instance segmentation regions and description 
          **kwargs: keyword arguments forwarded to super.
        """
        super(WaterFlowCounterConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
        self.data_url = data_url
        self.metadata_url = metadata_url
        


class WaterFlowCounter(datasets.GeneratorBasedBuilder):
    """WaterFlowCounter Images dataset"""

    BUILDER_CONFIGS = [
        WaterFlowCounterConfig(
            name="WFCR_full",
            description="Full dataset which contains coordinates and names of regions and information about rotation",
            data_url={
                "train": "data/train_photos.zip",
                "test": "data/test_photos.zip",
            },
            metadata_url={
                'full': "data/WaterFlowCounter.json"
            }
        )
    ]

    def _info(self):
        features = datasets.Features(
            {
                
                "image": datasets.Image(),
                "regions": datasets.Sequence(
                    {                        
                        "all_points_x": datasets.Sequence(datasets.Value("int64")),
                        "all_points_y": datasets.Sequence(datasets.Value("int64")),                                                                      
                        "name": datasets.ClassLabel(names=_REGION_NAME, num_classes=3),
                        "rotated": datasets.ClassLabel(names=_REGION_ROTETION, num_classes=4)
                    }         
                )
            }
        )

        return datasets.DatasetInfo(
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_files = dl_manager.download_and_extract(self.config.data_url)
        meta_file = dl_manager.download(self.config.metadata_url)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "folder_dir": data_files["train"],
                    "metadata_path": meta_file['full']
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "folder_dir": data_files["test"],
                    "metadata_path": meta_file['full']
                },
            )
        ]

    def _generate_examples(self, folder_dir, metadata_path):
        name_to_id = {}
        rotation_to_id = {}

        for indx, name in enumerate(_REGION_NAME):
            name_to_id[name] = indx

        for indx, name in enumerate(_REGION_ROTETION):
            rotation_to_id[name] = indx

        
        with open(metadata_path, "r", encoding='utf-8') as f:
            annotations = json.load(f)
        
        #print(annotations['_via_image_id_list'][0:5])

        for file in os.listdir(folder_dir):
            filepath = os.path.join(folder_dir, file)
            #print(filepath)
            with open(filepath, "rb") as f:
                image_bytes = f.read()
            #print(image_bytes)
            idx = 0
            all_x = []
            all_y = []
            names = []
            
            for el in annotations['_via_img_metadata']:
                
                if annotations['_via_img_metadata'][el]['filename'] == file:
                    
                    for region in annotations['_via_img_metadata'][el]['regions']:
                        all_x.append(region['shape_attributes']['all_points_x'])
                        all_y.append(region['shape_attributes']['all_points_y'])
                        names.append(name_to_id[list(region['region_attributes']['name'].keys())[0]])
                        try:
                            rotated = [rotation_to_id[list(region['region_attributes']['rotated'].keys())[0]]]
                        except:
                            rotated = [int(region['region_attributes']['rotated'])]
                        
                    
                    yield idx, {       
                        "image": {"path": filepath, "bytes": image_bytes},
                        "regions": {
                            "all_points_x": all_x,
                            "all_points_y": all_y,                                                                      
                            "name":names,
                            "rotated": rotated
                        }
                    }
                    idx += 1