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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
from typing import Dict, List, Tuple
|
| 18 |
+
|
| 19 |
+
import datasets
|
| 20 |
+
|
| 21 |
+
from seacrowd.utils import schemas
|
| 22 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 23 |
+
from seacrowd.utils.constants import TASK_TO_SCHEMA, Licenses, Tasks
|
| 24 |
+
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@article{SURINTA2015405,
|
| 27 |
+
title = "Recognition of handwritten characters using local gradient feature descriptors",
|
| 28 |
+
journal = "Engineering Applications of Artificial Intelligence",
|
| 29 |
+
volume = "45",
|
| 30 |
+
number = "Supplement C",
|
| 31 |
+
pages = "405 - 414",
|
| 32 |
+
year = "2015",
|
| 33 |
+
issn = "0952-1976",
|
| 34 |
+
doi = "https://doi.org/10.1016/j.engappai.2015.07.017",
|
| 35 |
+
url = "http://www.sciencedirect.com/science/article/pii/S0952197615001724",
|
| 36 |
+
author = "Olarik Surinta and Mahir F. Karaaba and Lambert R.B. Schomaker and Marco A. Wiering",
|
| 37 |
+
keywords = "Handwritten character recognition, Feature extraction, Local gradient feature descriptor,
|
| 38 |
+
Support vector machine, k-nearest neighbors"
|
| 39 |
+
}
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
_DATASETNAME = "alice_thi"
|
| 43 |
+
|
| 44 |
+
_DESCRIPTION = """\
|
| 45 |
+
ALICE-THI is a Thai handwritten script dataset that contains 24045 character
|
| 46 |
+
images, which is split into Thai handwritten character dataset (THI-C68) for
|
| 47 |
+
14490 images and Thai handwritten digit dataset (THI-D10) for 9555 images. The
|
| 48 |
+
data was collected from 150 native writers aged from 20 to 23 years old. The
|
| 49 |
+
participants were allowed to write only the isolated Thai script on the form and
|
| 50 |
+
at least 100 samples per character. The character images obtained from this
|
| 51 |
+
dataset generally have no background noise.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
_HOMEPAGE = "https://www.ai.rug.nl/~mrolarik/ALICE-THI/"
|
| 55 |
+
|
| 56 |
+
_LANGUAGES = ["tha"]
|
| 57 |
+
_SUBSETS = {
|
| 58 |
+
"THI-D10": {
|
| 59 |
+
"data_dir": "Thai_digit_sqr",
|
| 60 |
+
"label_dict": {
|
| 61 |
+
0: "0",
|
| 62 |
+
1: "1",
|
| 63 |
+
2: "2",
|
| 64 |
+
3: "3",
|
| 65 |
+
4: "4",
|
| 66 |
+
5: "5",
|
| 67 |
+
6: "6",
|
| 68 |
+
7: "7",
|
| 69 |
+
8: "8",
|
| 70 |
+
9: "9",
|
| 71 |
+
},
|
| 72 |
+
},
|
| 73 |
+
"THI-C68": {
|
| 74 |
+
"data_dir": "Thai_char_sqr",
|
| 75 |
+
"label_dict": {
|
| 76 |
+
0: "ko kai",
|
| 77 |
+
1: "kho khai",
|
| 78 |
+
2: "kho khuat",
|
| 79 |
+
3: "kho khwai",
|
| 80 |
+
4: "kho khon",
|
| 81 |
+
5: "kho rakhang",
|
| 82 |
+
6: "ngo ngu",
|
| 83 |
+
7: "cho chan",
|
| 84 |
+
8: "cho ching",
|
| 85 |
+
9: "cho chang",
|
| 86 |
+
10: "so so",
|
| 87 |
+
11: "cho choe",
|
| 88 |
+
12: "yo ying",
|
| 89 |
+
13: "do chada",
|
| 90 |
+
14: "to patak",
|
| 91 |
+
15: "tho than",
|
| 92 |
+
16: "tho nangmontho",
|
| 93 |
+
17: "tho phuthao",
|
| 94 |
+
18: "no nen",
|
| 95 |
+
19: "do dek",
|
| 96 |
+
20: "to tao",
|
| 97 |
+
21: "tho thung",
|
| 98 |
+
22: "tho thahan",
|
| 99 |
+
23: "tho thong",
|
| 100 |
+
24: "no nu",
|
| 101 |
+
25: "bo baimai",
|
| 102 |
+
26: "po pla",
|
| 103 |
+
27: "pho phung",
|
| 104 |
+
28: "fo fa",
|
| 105 |
+
29: "pho phan",
|
| 106 |
+
30: "fo fan",
|
| 107 |
+
31: "pho samphao",
|
| 108 |
+
32: "mo ma",
|
| 109 |
+
33: "yo yak",
|
| 110 |
+
34: "ro rua",
|
| 111 |
+
35: "ru",
|
| 112 |
+
36: "lo ling",
|
| 113 |
+
37: "lu",
|
| 114 |
+
38: "wo waen",
|
| 115 |
+
39: "so rusi",
|
| 116 |
+
40: "so sala",
|
| 117 |
+
41: "so sua",
|
| 118 |
+
42: "ho hip",
|
| 119 |
+
43: "lo chula",
|
| 120 |
+
44: "o ang",
|
| 121 |
+
45: "ho nokhuk",
|
| 122 |
+
46: "paiyannoi",
|
| 123 |
+
47: "sara a",
|
| 124 |
+
48: "mai han",
|
| 125 |
+
49: "sara aa",
|
| 126 |
+
50: "sara i",
|
| 127 |
+
51: "sara ii",
|
| 128 |
+
52: "sara ue",
|
| 129 |
+
53: "sara uee",
|
| 130 |
+
54: "sara u",
|
| 131 |
+
55: "sara uu",
|
| 132 |
+
56: "sara e",
|
| 133 |
+
57: "sara o",
|
| 134 |
+
58: "sara ai maimuan",
|
| 135 |
+
59: "sara ai maimalai",
|
| 136 |
+
60: "maiyamok",
|
| 137 |
+
61: "maitaikhu",
|
| 138 |
+
62: "mai ek",
|
| 139 |
+
63: "mai tho",
|
| 140 |
+
64: "mai tri",
|
| 141 |
+
65: "mai chattawa",
|
| 142 |
+
66: "thanthakhat",
|
| 143 |
+
67: "nikhahit",
|
| 144 |
+
},
|
| 145 |
+
},
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
_LICENSE = Licenses.UNKNOWN.value
|
| 149 |
+
|
| 150 |
+
_LOCAL = False
|
| 151 |
+
|
| 152 |
+
_URLS = {
|
| 153 |
+
_DATASETNAME: "https://www.ai.rug.nl/~mrolarik/ALICE-THI/ALICE-THI-Dataset.tar.gz",
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
_SUPPORTED_TASKS = [Tasks.OPTICAL_CHARACTER_RECOGNITION]
|
| 157 |
+
_SEACROWD_SCHEMA = f"seacrowd_{TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]].lower()}" # imtext
|
| 158 |
+
|
| 159 |
+
_SOURCE_VERSION = "1.0.0"
|
| 160 |
+
|
| 161 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
class AliceTHIDataset(datasets.GeneratorBasedBuilder):
|
| 165 |
+
"""Thai handwritten script dataset for character and digit recognition."""
|
| 166 |
+
|
| 167 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 168 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 169 |
+
|
| 170 |
+
BUILDER_CONFIGS = []
|
| 171 |
+
for subset in list(_SUBSETS.keys()):
|
| 172 |
+
BUILDER_CONFIGS += [
|
| 173 |
+
SEACrowdConfig(
|
| 174 |
+
name=f"{_DATASETNAME}_{subset}_source",
|
| 175 |
+
version=SOURCE_VERSION,
|
| 176 |
+
description=f"{_DATASETNAME} {subset} source schema",
|
| 177 |
+
schema="source",
|
| 178 |
+
subset_id=subset,
|
| 179 |
+
),
|
| 180 |
+
SEACrowdConfig(
|
| 181 |
+
name=f"{_DATASETNAME}_{subset}_{_SEACROWD_SCHEMA}",
|
| 182 |
+
version=SEACROWD_VERSION,
|
| 183 |
+
description=f"{_DATASETNAME} {subset} SEACrowd schema",
|
| 184 |
+
schema=_SEACROWD_SCHEMA,
|
| 185 |
+
subset_id=subset,
|
| 186 |
+
),
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_THI-C68_source"
|
| 190 |
+
|
| 191 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 192 |
+
label_names = [val for _, val in sorted(_SUBSETS[self.config.subset_id]["label_dict"].items())]
|
| 193 |
+
if self.config.schema == "source":
|
| 194 |
+
features = datasets.Features(
|
| 195 |
+
{
|
| 196 |
+
"label": datasets.ClassLabel(names=label_names),
|
| 197 |
+
"text": datasets.Value("string"),
|
| 198 |
+
"image_path": datasets.Value("string"),
|
| 199 |
+
}
|
| 200 |
+
)
|
| 201 |
+
elif self.config.schema == _SEACROWD_SCHEMA:
|
| 202 |
+
features = schemas.image_text_features(label_names=label_names)
|
| 203 |
+
|
| 204 |
+
return datasets.DatasetInfo(
|
| 205 |
+
description=_DESCRIPTION,
|
| 206 |
+
features=features,
|
| 207 |
+
homepage=_HOMEPAGE,
|
| 208 |
+
license=_LICENSE,
|
| 209 |
+
citation=_CITATION,
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 213 |
+
"""Returns SplitGenerators."""
|
| 214 |
+
data_name = "ALICE-THI Dataset"
|
| 215 |
+
data_path = Path(dl_manager.download_and_extract(_URLS[_DATASETNAME]))
|
| 216 |
+
data_path = Path(dl_manager.extract(data_path / data_name / f"{data_name}.tar.gz"))
|
| 217 |
+
data_path = data_path / _SUBSETS[self.config.subset_id]["data_dir"]
|
| 218 |
+
|
| 219 |
+
return [
|
| 220 |
+
datasets.SplitGenerator(
|
| 221 |
+
name=datasets.Split.TRAIN,
|
| 222 |
+
gen_kwargs={
|
| 223 |
+
"data_path": data_path,
|
| 224 |
+
},
|
| 225 |
+
),
|
| 226 |
+
]
|
| 227 |
+
|
| 228 |
+
def _generate_examples(self, data_path: Path) -> Tuple[int, Dict]:
|
| 229 |
+
"""Yields examples as (key, example) tuples."""
|
| 230 |
+
# iterate over files and directories
|
| 231 |
+
for subfolder in data_path.iterdir():
|
| 232 |
+
if subfolder.is_dir():
|
| 233 |
+
|
| 234 |
+
# source schema yield one image per label
|
| 235 |
+
if self.config.schema == "source":
|
| 236 |
+
_get_label = True # efficiency placeholder
|
| 237 |
+
for image_file in subfolder.glob("*.png"):
|
| 238 |
+
if _get_label: # get label from filename
|
| 239 |
+
label = int(image_file.name.split("-")[0].lower())
|
| 240 |
+
_get_label = False
|
| 241 |
+
|
| 242 |
+
yield image_file.stem, {
|
| 243 |
+
"label": label,
|
| 244 |
+
"text": _SUBSETS[self.config.subset_id]["label_dict"][label],
|
| 245 |
+
"image_path": str(image_file),
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
# seacrowd schema yield multiple images per label
|
| 249 |
+
elif self.config.schema == _SEACROWD_SCHEMA:
|
| 250 |
+
image_files = list(subfolder.glob("*.png"))
|
| 251 |
+
label = int(image_files[0].name.split("-")[0].lower())
|
| 252 |
+
|
| 253 |
+
yield subfolder.name, {
|
| 254 |
+
"id": subfolder.name,
|
| 255 |
+
"image_paths": [str(file) for file in image_files],
|
| 256 |
+
"texts": _SUBSETS[self.config.subset_id]["label_dict"][label],
|
| 257 |
+
"metadata": {
|
| 258 |
+
"context": "",
|
| 259 |
+
"labels": [label] * len(image_files),
|
| 260 |
+
},
|
| 261 |
+
}
|