File size: 2,618 Bytes
216a045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ae38f2
216a045
 
1ae38f2
 
 
 
 
 
 
216a045
 
 
 
 
 
 
 
 
 
 
 
 
b81fbbe
 
 
 
 
 
216a045
 
 
 
 
 
 
 
 
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
import datasets
import json
from PIL import Image


def train_data_format(json_to_dict: list):
    final_list = []
    count = 0
    for item in json_to_dict:
        count = count + 1
        # print(item['annotations'])
        test_dict = {"id": int, "tokens": [], "bboxes": [], "ner_tags": []}
        # test_dict = {"tokens": [], "bboxes": [], "ner_tags": []}
        test_dict["id"] = count
        # test_dict["img_path"] = item["file_name"]
        # print(item)
        test_dict["image"] = Image.open(item["file_name"]).convert("RGB")
        # test_dict["image"] = item["file_name"]
        for cont in item["annotations"]:
            test_dict["tokens"].append(cont["text"])
            test_dict["bboxes"].append(cont["box"])
            test_dict["ner_tags"].append(cont["label"])

        final_list.append(test_dict)
    # print(final_list)
    return final_list


def read_json(json_path: str) -> dict:
    with open(json_path, "r") as fp:
        data = json.loads(fp.read())
    return data


class MyDataset(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "image": datasets.Image(),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "bboxes": datasets.Sequence(
                        datasets.Sequence(datasets.Value("int32"))
                    ),
                    "ner_tags": datasets.Sequence(
                        datasets.ClassLabel(
                            num_classes=3,
                            names=["Other", "Patient_name", "Patient_address"],
                        )
                    ),
                }
            )
        )

    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": "Training_layoutLMV3.json",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": "Training_layoutLMV3.json",
                },
            ),
        ]

    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        # print(filepath)
        # print(read_json(filepath))
        # print(train_data_format(read_json(filepath)))
        for id_, row in enumerate(train_data_format(read_json(filepath))):
            yield id_, row