File size: 6,523 Bytes
74f3479 7a83f60 74f3479 ed8f186 74f3479 ed8f186 74f3479 ed8f186 07c3866 74f3479 23be2b5 aef8960 ed8f186 23be2b5 ed8f186 74f3479 5c4e1b6 74f3479 5c4e1b6 3847d94 5c4e1b6 4a0f506 5c4e1b6 74f3479 584ffa2 ed8f186 e5ecdf2 74f3479 ed8f186 74f3479 ed8f186 74f3479 ed8f186 74f3479 e5ecdf2 ed8f186 5871d59 07c3866 39d8995 3022e89 39d8995 74f3479 7a83f60 74f3479 39d8995 7a83f60 74f3479 5827598 3022e89 74f3479 5871d59 07c3866 5871d59 39d8995 5871d59 3022e89 7a83f60 41e6d1c 7a83f60 3022e89 5827598 304c135 ede3a5b 899e479 5c4e1b6 07c3866 5827598 304c135 07c3866 5827598 23be2b5 5c4e1b6 ede3a5b 272ad79 |
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 188 189 190 |
import json
import os
from PIL import Image
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_id": datasets.Value("int64"),
"image": datasets.Image(),
"width": datasets.Value("int32"),
"height": datasets.Value("int32"),
"objects": datasets.Sequence(
{
"id": datasets.Value("int64"),
"area": datasets.Value("int64"),
"bbox": datasets.Sequence(datasets.Value("float32"), length=4),
"category": datasets.ClassLabel(names=_REGION_NAME),
}
),
}
)
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)
idx = 0
id = 0
for file in os.listdir(folder_dir):
filepath = os.path.join(folder_dir, file)
with open(filepath, "rb") as f:
image_bytes = f.read()
image = Image.open(filepath)
width, height = image.size
all_bbox = []
all_area = []
all_segmentation = []
names = []
rotated = []
ids = []
for el in annotations['_via_img_metadata']:
if annotations['_via_img_metadata'][el]['filename'] == file:
for region in annotations['_via_img_metadata'][el]['regions']:
ids.append(id)
id += 1
all_x = region['shape_attributes']['all_points_x']
all_y = region['shape_attributes']['all_points_y']
x_min = min(all_x)
y_min = min(all_y)
x_max = max(all_x)
y_max = max(all_y)
p_width = x_max - x_min
p_height = y_max - y_min
bbox = [x_min, y_min, p_width, p_height ]
area = p_width * p_height
segmentation = list(zip(all_x, all_y))
all_bbox.append(bbox)
all_area.append(area)
all_segmentation.append(segmentation)
for name in list(region['region_attributes']['name'].keys()):
names.append(name_to_id[name])
if len(names) > 3:
names = names[:-3]
'''
try:
for rot in list(region['region_attributes']['rotated'].keys()):
rotated.append(rotation_to_id[rot])
except:
rotated.append(int(region['region_attributes']['rotated']))
'''
yield idx, {
"image_id": idx,
"image": {"path": filepath, "bytes": image_bytes},
"width": width,
"height": height,
"objects": {
"id": ids,
"area": all_area,
"bbox": all_bbox,
"category":names,
}
}
idx += 1
|