Upload WaterFlowCountersRecognition.py
Browse files- WaterFlowCountersRecognition.py +137 -0
WaterFlowCountersRecognition.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import collections
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
import datasets
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
_CITATION = """\
|
| 10 |
+
@SIA86{huggingface:dataset,
|
| 11 |
+
title = {WaterFlowCountersRecognition dataset},
|
| 12 |
+
author={SIA86},
|
| 13 |
+
year={2023}
|
| 14 |
+
}
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
_DESCRIPTION = """\
|
| 18 |
+
This dataset is designed to detect digital data from water flow counters photos.
|
| 19 |
+
"""
|
| 20 |
+
_HOMEPAGE = "https://github.com/SIA86/WaterFlowRecognition"
|
| 21 |
+
|
| 22 |
+
_REGION_NAME = ['value_a', 'value_b', 'serial']
|
| 23 |
+
|
| 24 |
+
_REGION_ROTETION = ['0', '90', '180', '270']
|
| 25 |
+
|
| 26 |
+
_ANNOTATION_FILENAME = '_annotations.json'
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class WaterFlowCounterConfig(datasets.BuilderConfig):
|
| 30 |
+
"""Builder Config for WaterFlowCounter"""
|
| 31 |
+
|
| 32 |
+
def __init__(self, data_url, **kwargs):
|
| 33 |
+
"""BuilderConfig for WaterFlowCounter.
|
| 34 |
+
Args:
|
| 35 |
+
data_url: `string`, url to download the photos.
|
| 36 |
+
metadata_urls: instance segmentation regions and description
|
| 37 |
+
**kwargs: keyword arguments forwarded to super.
|
| 38 |
+
"""
|
| 39 |
+
super(WaterFlowCounterConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 40 |
+
self.data_url = data_url
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class WaterFlowCounter(datasets.GeneratorBasedBuilder):
|
| 44 |
+
"""WaterFlowCounter Images dataset"""
|
| 45 |
+
|
| 46 |
+
BUILDER_CONFIGS = [
|
| 47 |
+
WaterFlowCounterConfig(
|
| 48 |
+
name="WFCR_full",
|
| 49 |
+
description="Full dataset which contains coordinates and names of regions and information about rotation",
|
| 50 |
+
data_url={
|
| 51 |
+
"train": "https://huggingface.co/datasets/SIA86/WaterFlowCountersRecognition/blob/main/data/train_photos.zip",
|
| 52 |
+
"test": "https://huggingface.co/datasets/SIA86/WaterFlowCountersRecognition/blob/main/data/test_photos.zip",
|
| 53 |
+
}
|
| 54 |
+
)
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def _info(self):
|
| 58 |
+
features = datasets.Features(
|
| 59 |
+
{
|
| 60 |
+
|
| 61 |
+
"image": datasets.Image(),
|
| 62 |
+
"regions": datasets.Sequence(
|
| 63 |
+
{
|
| 64 |
+
"all_points_x": datasets.Sequence(datasets.Value("int64")),
|
| 65 |
+
"all_points_y": datasets.Sequence(datasets.Value("int64")),
|
| 66 |
+
"name": datasets.ClassLabel(names=_REGION_NAME, num_classes=3),
|
| 67 |
+
"rotated": datasets.ClassLabel(names=_REGION_ROTETION, num_classes=4)
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
}
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return datasets.DatasetInfo(
|
| 74 |
+
features=features,
|
| 75 |
+
homepage=_HOMEPAGE,
|
| 76 |
+
citation=_CITATION,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def _split_generators(self, dl_manager):
|
| 80 |
+
data_files = dl_manager.download_and_extract(self.config.data_urls)
|
| 81 |
+
return [
|
| 82 |
+
datasets.SplitGenerator(
|
| 83 |
+
name=datasets.Split.TRAIN,
|
| 84 |
+
gen_kwargs={
|
| 85 |
+
"folder_dir": data_files["train"],
|
| 86 |
+
},
|
| 87 |
+
),
|
| 88 |
+
datasets.SplitGenerator(
|
| 89 |
+
name=datasets.Split.TEST,
|
| 90 |
+
gen_kwargs={
|
| 91 |
+
"folder_dir": data_files["test"],
|
| 92 |
+
},
|
| 93 |
+
)
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
def generate_examples(self, folder_dir):
|
| 97 |
+
name_to_id = {}
|
| 98 |
+
rotation_to_id = {}
|
| 99 |
+
|
| 100 |
+
for indx, name in enumerate(_REGION_NAME):
|
| 101 |
+
name_to_id[name] = indx
|
| 102 |
+
|
| 103 |
+
for indx, name in enumerate(_REGION_ROTETION):
|
| 104 |
+
rotation_to_id[name] = indx
|
| 105 |
+
|
| 106 |
+
with open('_annotations.json', 'r', encoding='utf-8') as fr:
|
| 107 |
+
json_file = json.load(fr)
|
| 108 |
+
|
| 109 |
+
for file in os.listdir(folder_dir):
|
| 110 |
+
filepath = os.path.join(folder_dir, file)
|
| 111 |
+
|
| 112 |
+
with open(filepath, "rb") as f:
|
| 113 |
+
image_bytes = f.read()
|
| 114 |
+
|
| 115 |
+
all_x = []
|
| 116 |
+
all_y = []
|
| 117 |
+
names = []
|
| 118 |
+
|
| 119 |
+
for el in json_file['_via_img_metadata']:
|
| 120 |
+
if json_file['_via_img_metadata'][el]['filename'] == file:
|
| 121 |
+
for region in json_file['_via_img_metadata'][el]['regions']:
|
| 122 |
+
all_x.append(region['shape_attributes']['all_points_x'])
|
| 123 |
+
all_y.append(region['shape_attributes']['all_points_y'])
|
| 124 |
+
names.append(name_to_id[list(region['region_attributes']['name'].keys())[0]])
|
| 125 |
+
rotated = rotation_to_id[list(region['region_attributes']['rotated'].keys())[0]]
|
| 126 |
+
|
| 127 |
+
yield idx, {
|
| 128 |
+
"image": {"path": filepath, "bytes": image_bytes},
|
| 129 |
+
"regions": {
|
| 130 |
+
"all_points_x": all_x,
|
| 131 |
+
"all_points_y": all_y,
|
| 132 |
+
"name":names,
|
| 133 |
+
"rotated": rotated
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
idx += 1
|
| 137 |
+
|