File size: 5,949 Bytes
181c692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959d2c1
f532770
959d2c1
e7c3997
959d2c1
32ba0e6
959d2c1
 
181c692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9004744
 
a1616dd
9004744
 
 
959d2c1
181c692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# coding=utf-8
import json
import os
import datasets
from PIL import Image
import numpy as np
logger = datasets.logging.get_logger(__name__)
_CITATION = """\\n@article{Jaume2019FUNSDAD,
  title={FUNSD: A Dataset for Form Understanding in Noisy Scanned Documents},
  author={Guillaume Jaume and H. K. Ekenel and J. Thiran},
  journal={2019 International Conference on Document Analysis and Recognition Workshops (ICDARW)},
  year={2019},
  volume={2},
  pages={1-6}
}
"""
_DESCRIPTION = """\\nhttps://guillaumejaume.github.io/FUNSD/
"""
def load_image(image_path):
    image = Image.open(image_path).convert("RGB")
    w, h = image.size
    # resize image to 224x224
    image = image.resize((224, 224))
    image = np.asarray(image)  
    image = image[:, :, ::-1] # flip color channels from RGB to BGR
    image = image.transpose(2, 0, 1) # move channels to first dimension
    return image, (w, h)
def normalize_bbox(bbox, size):
    return [
        int(1000 * bbox[0] / size[0]),
        int(1000 * bbox[1] / size[1]),
        int(1000 * bbox[2] / size[0]),
        int(1000 * bbox[3] / size[1]),
    ]
    
def custom_download(url, dst_path):
    import boto3
    print(dst_path)
    s3 = boto3.client('s3')
    s3.download_file('modeldocuments', url, dst_path)
    
    
class FunsdConfig(datasets.BuilderConfig):
    """BuilderConfig for FUNSD"""
    def __init__(self, **kwargs):
        """BuilderConfig for FUNSD.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(FunsdConfig, self).__init__(**kwargs)
class Funsd(datasets.GeneratorBasedBuilder):
    """FUNSD dataset."""
    BUILDER_CONFIGS = [
        FunsdConfig(name="funsd", version=datasets.Version("1.0.0"), description="FUNSD dataset"),
    ]
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
                    "ner_tags": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=["O", 'S-HOSPITAL-NAME', 'S-MRN', 
                                   'S-PAID-AMOUNT', 'I-HOSPITAL-NAME', 'S-PATIENT-NAME', 
                                   'S-PATIENT-NRIC', 'S-RECEIPT-DATE', 'S-RECEIPT-NO', 'B-MRN',
                                   'S-TOTAL', 'S-TREATING-DOCTOR', 'S-TREATMENT-DATE', 
                                   'B-HOSPITAL-NAME', 'I-MRN', 'B-PATIENT-NRIC', 
                                   'I-PATIENT-NRIC', 'B-PAID-AMOUNT', 'I-PAID-AMOUNT', 
                                   'B-PATIENT-NAME', 'I-PATIENT-NAME', 'B-RECEIPT-DATE', 
                                   'I-RECEIPT-DATE', 'B-TOTAL', 'I-TOTAL', 'B-TREATING-DOCTOR', 
                                   'I-TREATING-DOCTOR', 'B-TREATMENT-DATE', 'B-RECEIPT-NO', 
                                   'I-RECEIPT-NO', 'I-TREATMENT-DATE']
                        )
                    ),
                    #"image": datasets.Array3D(shape=(3, 224, 224), dtype="uint8"),
                    "image_path": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://guillaumejaume.github.io/FUNSD/",
            citation=_CITATION,
        )
    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        url = 'https://transfer.sh/h1YqN8/datafiles.zip'
        #url = 'datafiles.zip'
        
        downloaded_file = dl_manager.download_and_extract(url)
        #downloaded_file = dl_manager.download_custom(url, custom_download)
        #downloaded_file = dl_manager.extract(downloaded_file)
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={"filepath": f"{downloaded_file}/data/training_data/"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"filepath": f"{downloaded_file}/data/testing_data/"}
            ),
        ]
    def _generate_examples(self, filepath):
        logger.info("⏳ Generating examples from = %s", filepath)
        ann_dir = os.path.join(filepath, "annotations")
        img_dir = os.path.join(filepath, "images")
        for guid, file in enumerate(sorted(os.listdir(ann_dir))):
            tokens = []
            bboxes = []
            ner_tags = []
            file_path = os.path.join(ann_dir, file)
            with open(file_path, "r", encoding="utf8") as f:
                data = json.load(f)
            image_path = os.path.join(img_dir, file)
            image_path = image_path.replace("json", "png")
            image, size = load_image(image_path)
            for item in data["form"]:
                words, label = item["words"], item["label"]
                words = [w for w in words if w["text"].strip() != ""]
                if len(words) == 0:
                    continue
                if label == "others":
                    for w in words:
                        tokens.append(w["text"])
                        ner_tags.append("O")
                        bboxes.append(normalize_bbox(w["box"], size))
                else:
                    tokens.append(words[0]["text"])
                    ner_tags.append("B-" + label.upper())
                    bboxes.append(normalize_bbox(words[0]["box"], size))
                    for w in words[1:]:
                        tokens.append(w["text"])
                        ner_tags.append("I-" + label.upper())
                        bboxes.append(normalize_bbox(w["box"], size))
            yield guid, {"id": str(guid), "tokens": tokens, 
                         "bboxes": bboxes, "ner_tags": ner_tags, 
                         "image_path": image_path}