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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 |
+
import os
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from typing import Dict, List, Tuple
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
from seacrowd.utils import schemas
|
| 23 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 24 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
| 25 |
+
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@INPROCEEDINGS{8584876,
|
| 28 |
+
author={Joseph, Ferdin Joe John and Anantaprayoon, Panatchakorn},
|
| 29 |
+
booktitle={2018 International Conference on Information Technology (InCIT)},
|
| 30 |
+
title={Offline Handwritten Thai Character Recognition Using Single Tier Classifier and Local Features},
|
| 31 |
+
year={2018},
|
| 32 |
+
volume={},
|
| 33 |
+
number={},
|
| 34 |
+
pages={1-4},
|
| 35 |
+
abstract={Handwritten character recognition is a conversion process of handwriting into machine-encoded text. Currently,
|
| 36 |
+
several techniques and methods are proposed to enhance accuracy of handwritten character recognition for many languages
|
| 37 |
+
spoken across the globe. In this project, a local feature-based approach is proposed to enhance the accuracy of handwritten
|
| 38 |
+
offline character recognition for Thai alphabets. In the experiment, through MATLAB, 100 images for each class of Thai
|
| 39 |
+
alphabets are collected and k-fold cross validation is applied to manage datasets to train and test. A gradient invariant
|
| 40 |
+
feature set consisting of LBP and shape features is extracted. The classification is operated by using query matching based
|
| 41 |
+
on Euclidean distance. The accuracy would be the percentage of correct classification for each class. For the result, the
|
| 42 |
+
highest accuracy is 68.96% which has 144-bit shape features and uniform pattern LBP for the features.},
|
| 43 |
+
keywords={Character recognition;Feature extraction;Shape;Genetic algorithms;Matlab;Gray-scale;Optical character recognition
|
| 44 |
+
software;Offline Character Recognition;Local Binary Pattern;Thai Handwriting},
|
| 45 |
+
doi={10.23919/INCIT.2018.8584876},
|
| 46 |
+
ISSN={},
|
| 47 |
+
month={Oct},
|
| 48 |
+
url={https://ieeexplore.ieee.org/document/8584876}}
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
_DATASETNAME = "kvis_th_ocr"
|
| 53 |
+
|
| 54 |
+
_DESCRIPTION = """\
|
| 55 |
+
The KVIS Thai OCR Dataset contains scanned handwritten version of all 44 Thai characters obtained from 27 individuals. It
|
| 56 |
+
consisted of 1079 images from 44 classes (letters). This dataset consists of all Thai consonants with different writing
|
| 57 |
+
styles of various people from ages between 16 and 75. Vowels and intonation are not taken into consideration for the dataset
|
| 58 |
+
collected.
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
_HOMEPAGE = "https://data.mendeley.com/datasets/8nr3pbdk5c/1"
|
| 62 |
+
|
| 63 |
+
_LANGUAGES = ["tha"]
|
| 64 |
+
|
| 65 |
+
_LICENSE = Licenses.CC_BY_4_0.value
|
| 66 |
+
|
| 67 |
+
_LOCAL = False
|
| 68 |
+
|
| 69 |
+
_URLS = "https://prod-dcd-datasets-cache-zipfiles.s3.eu-west-1.amazonaws.com/8nr3pbdk5c-1.zip"
|
| 70 |
+
|
| 71 |
+
_SUPPORTED_TASKS = [Tasks.OPTICAL_CHARACTER_RECOGNITION]
|
| 72 |
+
|
| 73 |
+
_SOURCE_VERSION = "1.0.0"
|
| 74 |
+
|
| 75 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class KVISThaiOCRDataset(datasets.GeneratorBasedBuilder):
|
| 79 |
+
"""
|
| 80 |
+
KVIS Thai OCR is a dataset for optical character recognition for Thai characters from https://data.mendeley.com/datasets/8nr3pbdk5c/1.
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 84 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 85 |
+
labels = ["ก", "ข", "ฃ", "ค", "ฅ", "ฆ", "ง", "จ", "ฉ", "ช", "ซ", "ฌ", "ญ", "ฎ", "ฏ", "ฐ", "ฑ", "ฒ", "ณ", "ด", "ต", "ถ", "ท", "ธ", "น", "บ", "ป", "ผ", "ฝ", "พ", "ฟ", "ภ", "ม", "ย", "ร", "ล", "ว", "ศ", "ษ", "ส", "ห", "ฬ", "อ", "ฮ"]
|
| 86 |
+
|
| 87 |
+
BUILDER_CONFIGS = [
|
| 88 |
+
SEACrowdConfig(
|
| 89 |
+
name=f"{_DATASETNAME}_source",
|
| 90 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 91 |
+
description=f"{_DATASETNAME} source schema",
|
| 92 |
+
schema="source",
|
| 93 |
+
subset_id=f"{_DATASETNAME}",
|
| 94 |
+
),
|
| 95 |
+
SEACrowdConfig(
|
| 96 |
+
name=f"{_DATASETNAME}_seacrowd_imtext",
|
| 97 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 98 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
| 99 |
+
schema="seacrowd_imtext",
|
| 100 |
+
subset_id=f"{_DATASETNAME}",
|
| 101 |
+
),
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 105 |
+
if self.config.schema == "source":
|
| 106 |
+
features = datasets.Features(
|
| 107 |
+
{
|
| 108 |
+
"id": datasets.Value("string"),
|
| 109 |
+
"file_path": datasets.Value("string"),
|
| 110 |
+
"character": datasets.Value("string"),
|
| 111 |
+
}
|
| 112 |
+
)
|
| 113 |
+
elif self.config.schema == "seacrowd_imtext":
|
| 114 |
+
features = schemas.image_text_features(label_names=self.labels)
|
| 115 |
+
else:
|
| 116 |
+
raise ValueError(f"Invalid schema: {self.config.schema}")
|
| 117 |
+
|
| 118 |
+
return datasets.DatasetInfo(
|
| 119 |
+
description=_DESCRIPTION,
|
| 120 |
+
features=features,
|
| 121 |
+
homepage=_HOMEPAGE,
|
| 122 |
+
license=_LICENSE,
|
| 123 |
+
citation=_CITATION,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 127 |
+
"""
|
| 128 |
+
Returns SplitGenerators.
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
dir = dl_manager.download_and_extract(_URLS)
|
| 132 |
+
path = dl_manager.extract(os.path.join(dir, "KVIS TOCR Dataset.zip"))
|
| 133 |
+
|
| 134 |
+
return [
|
| 135 |
+
datasets.SplitGenerator(
|
| 136 |
+
name=datasets.Split.TRAIN,
|
| 137 |
+
gen_kwargs={
|
| 138 |
+
"path": path,
|
| 139 |
+
"split": "train",
|
| 140 |
+
},
|
| 141 |
+
),
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
def _generate_examples(self, path: Path, split: str) -> Tuple[int, Dict]:
|
| 145 |
+
"""
|
| 146 |
+
Yields examples as (key, example) tuples.
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
idx = 0
|
| 150 |
+
path = list(os.walk(path))
|
| 151 |
+
for directory in path[1:]:
|
| 152 |
+
label = directory[0][-3:-2]
|
| 153 |
+
for file in directory[2]:
|
| 154 |
+
file_extension = str(file[-3:])
|
| 155 |
+
if file_extension == "jpg":
|
| 156 |
+
file_id = str(file[:-4])
|
| 157 |
+
file_path = os.path.join(directory[0], file)
|
| 158 |
+
if self.config.schema == "source":
|
| 159 |
+
data = {
|
| 160 |
+
"id": file_id,
|
| 161 |
+
"file_path": file_path,
|
| 162 |
+
"character": label,
|
| 163 |
+
}
|
| 164 |
+
yield idx, data
|
| 165 |
+
idx += 1
|
| 166 |
+
elif self.config.schema == "seacrowd_imtext":
|
| 167 |
+
data = {
|
| 168 |
+
"id": file_id,
|
| 169 |
+
"image_paths": [file_path],
|
| 170 |
+
"texts": "",
|
| 171 |
+
"metadata": {
|
| 172 |
+
"context": "",
|
| 173 |
+
"labels": [label],
|
| 174 |
+
},
|
| 175 |
+
}
|
| 176 |
+
yield idx, data
|
| 177 |
+
idx += 1
|
| 178 |
+
else:
|
| 179 |
+
raise ValueError(f"Invalid schema: {self.config.schema}")
|