File size: 7,041 Bytes
e7ee5e4
 
 
 
 
 
 
d3e509e
e7ee5e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e509e
e7ee5e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58e0310
e7ee5e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e509e
 
 
 
 
 
 
 
 
 
e7ee5e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e509e
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# coding=utf-8

import json
import os

import datasets
import gdown

logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@article{park2019cord,
  title={CORD: A Consolidated Receipt Dataset for Post-OCR Parsing},
  author={Park, Seunghyun and Shin, Seung and Lee, Bado and Lee, Junyeop and Surh, Jaeheung and Seo, Minjoon and Lee, Hwalsuk}
  booktitle={Document Intelligence Workshop at Neural Information Processing Systems}
  year={2019}
}
"""

_DESCRIPTION = """\
https://github.com/clovaai/cord
"""
_URL = "https://drive.google.com/uc?id=1MqhTbcj-AHXOqYoeoh12aRUwIprzTJYI"


def gdrive_downloader(url, path):
    gdown.download(url, path, quiet=False)


class CordConfig(datasets.BuilderConfig):
    """BuilderConfig for CORD"""

    def __init__(self, **kwargs):
        """BuilderConfig for CORD.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(CordConfig, self).__init__(**kwargs)


class Cord(datasets.GeneratorBasedBuilder):
    """Conll2003 dataset."""

    BUILDER_CONFIGS = [
        CordConfig(name="cord", 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"))),
                    "roi": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
                    "ner_tags": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=['menu.cnt',
                                   'menu.discountprice',
                                   'menu.etc',
                                   'menu.itemsubtotal',
                                   'menu.nm',
                                   'menu.num',
                                   'menu.price',
                                   'menu.sub_cnt',
                                   'menu.sub_etc',
                                   'menu.sub_nm',
                                   'menu.sub_price',
                                   'menu.sub_unitprice',
                                   'menu.unitprice',
                                   'menu.vatyn',
                                   'sub_total.discount_price',
                                   'sub_total.etc',
                                   'sub_total.othersvc_price',
                                   'sub_total.service_price',
                                   'sub_total.subtotal_price',
                                   'sub_total.tax_price',
                                   'total.cashprice',
                                   'total.changeprice',
                                   'total.creditcardprice',
                                   'total.emoneyprice',
                                   'total.menuqty_cnt',
                                   'total.menutype_cnt',
                                   'total.total_etc',
                                   'total.total_price',
                                   'void_menu.nm',
                                   'void_menu.price']
                        )
                    ),
                    "image_path": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/clovaai/cord",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        """Returns SplitGenerators."""
        url_or_urls = ['https://drive.google.com/uc?id=1MqhTbcj-AHXOqYoeoh12aRUwIprzTJYI',
                       'https://drive.google.com/uc?id=1wYdp5nC9LnHQZ2FcmOoC0eClyWvcuARU']

        downloaded_file = dl_manager.extract(
            dl_manager.download_custom(url_or_urls, gdrive_downloader))

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={
                    "filepaths": downloaded_file, "mode": "/CORD/train"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={
                    "filepaths": downloaded_file, "mode": "/CORD/test"}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, gen_kwargs={
                    "filepaths": downloaded_file, "mode": "/CORD/dev"}
            ),
        ]

    def _generate_examples(self, filepaths, mode):
        guid = -1
        for filepath in filepaths:
            filepath_folder  = filepath + mode
            logger.info("⏳ Generating examples from = %s", filepath_folder)
            ann_dir = os.path.join(filepath_folder, "json")
            if not os.path.exists(ann_dir):
                continue
            img_dir = os.path.join(filepath_folder, "image")
            for file in sorted(os.listdir(ann_dir)):
                guid +=1
                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")

                if not os.path.exists(image_path):
                    other_dir_idx = int(not (filepaths.index(filepath)+2)%2)
                    image_path = image_path.replace(
                        filepath, filepaths[other_dir_idx])

                roi = data["roi"]
                if roi:
                    top_left = [roi["x1"], roi["y1"]]
                    bottom_right = [roi["x3"], roi["y3"]]
                    bottom_left = [roi["x4"], roi["y4"]]
                    top_right = [roi["x2"], roi["y2"]]
                    roi = [top_left, top_right, bottom_right, bottom_left]
                else:
                    roi = []


                for item in data["valid_line"]:
                    for word in item['words']:
                        # get word
                        txt = word['text']

                        # get bounding box
                        x1 = word['quad']['x1']
                        y1 = word['quad']['y1']
                        x3 = word['quad']['x3']
                        y3 = word['quad']['y3']

                        box = [x1, y1, x3, y3]

                        # ADDED
                        # skip empty word
                        if len(txt) < 1:
                            continue

                        tokens.append(txt)
                        bboxes.append(box)
                        ner_tags.append(item['category'])

                yield guid, {"id": str(guid), "tokens": tokens, "bboxes": bboxes, "ner_tags": ner_tags, "image_path": image_path, "roi":roi}