fqa-cyber
commited on
Commit
·
809eb72
1
Parent(s):
8767446
Upload Dataset Builder
Browse files- Handwritten_Khatt.py +77 -0
Handwritten_Khatt.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
_DESCRIPTION = """\
|
| 7 |
+
Arabic Handwritten dataset.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
_REPO = "https://huggingface.co/datasets/eDaraty/Handwritten_Khatt"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# logging.basicConfig(level=logging.INFO)
|
| 14 |
+
# logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Khatt(datasets.GeneratorBasedBuilder):
|
| 18 |
+
"""Handwritten arabic image-text pairs"""
|
| 19 |
+
|
| 20 |
+
def _info(self):
|
| 21 |
+
return datasets.DatasetInfo(
|
| 22 |
+
description=_DESCRIPTION,
|
| 23 |
+
features=datasets.Features(
|
| 24 |
+
{
|
| 25 |
+
'image': datasets.Image(),
|
| 26 |
+
'text': datasets.Value("string"),
|
| 27 |
+
}
|
| 28 |
+
),
|
| 29 |
+
# supervised_keys=None,
|
| 30 |
+
homepage=_REPO,
|
| 31 |
+
# citation=_CITATION,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
def _split_generators(self, dl_manager):
|
| 35 |
+
train_archive = dl_manager.download(f"{_REPO}/resolve/main/data/train.zip")
|
| 36 |
+
test_archive = dl_manager.download(f"{_REPO}/resolve/main/data/test.zip")
|
| 37 |
+
val_archive = dl_manager.download(f"{_REPO}/resolve/main/data/validation.zip")
|
| 38 |
+
# split_metadata_paths = dl_manager.download(_METADATA_URLS)
|
| 39 |
+
|
| 40 |
+
return [
|
| 41 |
+
datasets.SplitGenerator(
|
| 42 |
+
name=datasets.Split.TRAIN,
|
| 43 |
+
gen_kwargs={
|
| 44 |
+
"images": dl_manager.iter_archive(train_archive)
|
| 45 |
+
},
|
| 46 |
+
),
|
| 47 |
+
|
| 48 |
+
datasets.SplitGenerator(
|
| 49 |
+
name=datasets.Split.TEST,
|
| 50 |
+
gen_kwargs={
|
| 51 |
+
"images": dl_manager.iter_archive(test_archive)
|
| 52 |
+
},
|
| 53 |
+
),
|
| 54 |
+
|
| 55 |
+
datasets.SplitGenerator(
|
| 56 |
+
name=datasets.Split.VALIDATION,
|
| 57 |
+
gen_kwargs={
|
| 58 |
+
"images": dl_manager.iter_archive(val_archive)
|
| 59 |
+
},
|
| 60 |
+
),
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
def _generate_examples(self, images):
|
| 64 |
+
""" This function returns the examples in the raw (text) form."""
|
| 65 |
+
df = pd.read_csv(f"{_REPO}/resolve/main/data/metadata.csv")
|
| 66 |
+
|
| 67 |
+
for idx, (filepath, image) in enumerate(images):
|
| 68 |
+
image_name = os.path.basename(filepath)
|
| 69 |
+
# logger.info(" filename of image is '%s' ", image_name)
|
| 70 |
+
|
| 71 |
+
description = df[df["file_name"] == image_name]['text'].values.tolist()[0]
|
| 72 |
+
# logger.info(" text of image is '%s' ", description)
|
| 73 |
+
# logger.info(" type of image is '%s' ", type(description))
|
| 74 |
+
yield idx, {
|
| 75 |
+
"image": {"path": filepath, "bytes": image.read()},
|
| 76 |
+
"text": description,
|
| 77 |
+
}
|