Create arabic-digits.py
Browse files- arabic-digits.py +67 -0
arabic-digits.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from zipfile import ZipFile
|
| 5 |
+
import datasets
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class ArabicDigits(datasets.GeneratorBasedBuilder):
|
| 9 |
+
"""Arabic Handwritten Digits Dataset."""
|
| 10 |
+
|
| 11 |
+
VERSION = datasets.Version("1.0.0")
|
| 12 |
+
|
| 13 |
+
def _info(self):
|
| 14 |
+
return datasets.DatasetInfo(
|
| 15 |
+
description="""Arabic Handwritten Digits Dataset, composed of images of Arabic digits handwritten
|
| 16 |
+
by participants. This dataset is structured for use in machine learning tasks such
|
| 17 |
+
as digit classification.""",
|
| 18 |
+
features=datasets.Features(
|
| 19 |
+
{
|
| 20 |
+
"image": datasets.Image(),
|
| 21 |
+
"label": datasets.ClassLabel(names=[str(i) for i in range(10)])
|
| 22 |
+
}
|
| 23 |
+
),
|
| 24 |
+
supervised_keys=("image", "label"),
|
| 25 |
+
homepage="https://github.com/mloey/Arabic-Handwritten-Digits-Dataset",
|
| 26 |
+
citation="""@inproceedings{el2016cnn,
|
| 27 |
+
title={CNN for handwritten arabic digits recognition based on LeNet-5},
|
| 28 |
+
author={El-Sawy, Ahmed and Hazem, EL-Bakry and Loey, Mohamed},
|
| 29 |
+
booktitle={International conference on advanced intelligent systems and informatics},
|
| 30 |
+
pages={566--575},
|
| 31 |
+
year={2016},
|
| 32 |
+
organization={Springer}
|
| 33 |
+
}""",
|
| 34 |
+
license="odbl"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def _split_generators(self, dl_manager):
|
| 38 |
+
urls = {
|
| 39 |
+
"train": "https://github.com/mloey/Arabic-Handwritten-Digits-Dataset/raw/master/Train%20Images.zip",
|
| 40 |
+
"test": "https://github.com/mloey/Arabic-Handwritten-Digits-Dataset/raw/master/Test%20Images.zip"
|
| 41 |
+
}
|
| 42 |
+
downloaded_files = dl_manager.download(urls)
|
| 43 |
+
|
| 44 |
+
return [
|
| 45 |
+
datasets.SplitGenerator(
|
| 46 |
+
name=datasets.Split.TRAIN,
|
| 47 |
+
gen_kwargs={"archive_path": downloaded_files["train"], "split": "train"},
|
| 48 |
+
),
|
| 49 |
+
datasets.SplitGenerator(
|
| 50 |
+
name=datasets.Split.TEST,
|
| 51 |
+
gen_kwargs={"archive_path": downloaded_files["test"], "split": "test"},
|
| 52 |
+
)
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
def _generate_examples(self, archive_path, split):
|
| 56 |
+
"""Generate examples from the ZIP archives of images and labels."""
|
| 57 |
+
with ZipFile(archive_path, "r") as archive:
|
| 58 |
+
for entry in tqdm(archive.infolist(), desc=f"Processing {split} set"):
|
| 59 |
+
if entry.filename.endswith(".png"):
|
| 60 |
+
content = archive.read(entry)
|
| 61 |
+
image = Image.open(io.BytesIO(content))
|
| 62 |
+
label = int(entry.filename.split("_")[-1][:-4]) # Extract label from filename
|
| 63 |
+
|
| 64 |
+
yield entry.filename, {
|
| 65 |
+
"image": image,
|
| 66 |
+
"label": label
|
| 67 |
+
}
|