File size: 2,760 Bytes
6fb1672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Medical Prescription OCR Dataset"""

import json
import os

import datasets
from PIL import Image


_DESCRIPTION = """
Medical Prescription OCR Dataset - A collection of synthetic handwritten medical prescriptions
with structured annotations for training OCR models.
"""

_CITATION = """
@dataset{shrivastava2024medicalprescription,
  author = {Chinmay Shrivastava},
  title = {Medical Prescription OCR Dataset},
  year = {2024},
  publisher = {Hugging Face},
  url = {https://huggingface.co/datasets/chinmays18/medical-prescription-dataset}
}
"""


class MedicalPrescriptionDataset(datasets.GeneratorBasedBuilder):
    """Medical Prescription OCR Dataset"""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "image": datasets.Image(),
                "ground_truth": datasets.Value("string"),
            }),
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # The dataset files are already in the repository
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images_path": "train/images",
                    "annotations_path": "train/annotations",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "images_path": "val/images",
                    "annotations_path": "val/annotations",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images_path": "test/images",
                    "annotations_path": "test/annotations",
                },
            ),
        ]

    def _generate_examples(self, images_path, annotations_path):
        # Get all image files
        image_files = sorted([f for f in os.listdir(images_path) if f.endswith('.png')])
        
        for idx, image_file in enumerate(image_files):
            # Get corresponding annotation file
            base_name = os.path.splitext(image_file)[0]
            annotation_file = f"{base_name}.json"
            
            # Read image
            image_path = os.path.join(images_path, image_file)
            
            # Read annotation
            annotation_path = os.path.join(annotations_path, annotation_file)
            with open(annotation_path, 'r') as f:
                annotation = json.load(f)
            
            yield idx, {
                "image": image_path,
                "ground_truth": annotation["ground_truth"],
            }