Create funsd-iob-original.py
Browse files- funsd-iob-original.py +127 -0
funsd-iob-original.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
logger = datasets.logging.get_logger(__name__)
|
11 |
+
|
12 |
+
|
13 |
+
_CITATION = """\
|
14 |
+
@article{Jaume2019FUNSDAD,
|
15 |
+
title={FUNSD: A Dataset for Form Understanding in Noisy Scanned Documents},
|
16 |
+
author={Guillaume Jaume and H. K. Ekenel and J. Thiran},
|
17 |
+
journal={2019 International Conference on Document Analysis and Recognition Workshops (ICDARW)},
|
18 |
+
year={2019},
|
19 |
+
volume={2},
|
20 |
+
pages={1-6}
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
_DESCRIPTION = """\
|
24 |
+
https://guillaumejaume.github.io/FUNSD/
|
25 |
+
"""
|
26 |
+
|
27 |
+
def load_image(image_path):
|
28 |
+
image = Image.open(image_path).convert("RGB")
|
29 |
+
w, h = image.size
|
30 |
+
return image, (w, h)
|
31 |
+
|
32 |
+
def normalize_bbox(bbox, size):
|
33 |
+
return [
|
34 |
+
int(1000 * bbox[0] / size[0]),
|
35 |
+
int(1000 * bbox[1] / size[1]),
|
36 |
+
int(1000 * bbox[2] / size[0]),
|
37 |
+
int(1000 * bbox[3] / size[1]),
|
38 |
+
]
|
39 |
+
|
40 |
+
class FunsdConfig(datasets.BuilderConfig):
|
41 |
+
"""BuilderConfig for FUNSD"""
|
42 |
+
|
43 |
+
def __init__(self, **kwargs):
|
44 |
+
"""BuilderConfig for FUNSD.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
**kwargs: keyword arguments forwarded to super.
|
48 |
+
"""
|
49 |
+
super(FunsdConfig, self).__init__(**kwargs)
|
50 |
+
|
51 |
+
class Funsd(datasets.GeneratorBasedBuilder):
|
52 |
+
"""FUNSD dataset."""
|
53 |
+
|
54 |
+
BUILDER_CONFIGS = [
|
55 |
+
FunsdConfig(name="funsd", version=datasets.Version("1.0.0"), description="FUNSD dataset"),
|
56 |
+
]
|
57 |
+
|
58 |
+
def _info(self):
|
59 |
+
return datasets.DatasetInfo(
|
60 |
+
description=_DESCRIPTION,
|
61 |
+
features=datasets.Features(
|
62 |
+
{
|
63 |
+
"id": datasets.Value("string"),
|
64 |
+
"words": datasets.Sequence(datasets.Value("string")),
|
65 |
+
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
66 |
+
"original_bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
67 |
+
"ner_tags": datasets.Sequence(
|
68 |
+
datasets.features.ClassLabel(
|
69 |
+
names=["O", "B-HEADER", "I-HEADER", "B-QUESTION", "I-QUESTION", "B-ANSWER", "I-ANSWER"]
|
70 |
+
)
|
71 |
+
),
|
72 |
+
"image": datasets.features.Image(),
|
73 |
+
}
|
74 |
+
),
|
75 |
+
supervised_keys=None,
|
76 |
+
homepage="https://guillaumejaume.github.io/FUNSD/",
|
77 |
+
citation=_CITATION,
|
78 |
+
)
|
79 |
+
|
80 |
+
def _split_generators(self, dl_manager):
|
81 |
+
"""Returns SplitGenerators."""
|
82 |
+
downloaded_file = dl_manager.download_and_extract("https://guillaumejaume.github.io/FUNSD/dataset.zip")
|
83 |
+
return [
|
84 |
+
datasets.SplitGenerator(
|
85 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepath": f"{downloaded_file}/dataset/training_data/"}
|
86 |
+
),
|
87 |
+
datasets.SplitGenerator(
|
88 |
+
name=datasets.Split.TEST, gen_kwargs={"filepath": f"{downloaded_file}/dataset/testing_data/"}
|
89 |
+
),
|
90 |
+
]
|
91 |
+
|
92 |
+
def _generate_examples(self, filepath):
|
93 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
94 |
+
ann_dir = os.path.join(filepath, "annotations")
|
95 |
+
img_dir = os.path.join(filepath, "images")
|
96 |
+
for guid, file in enumerate(sorted(os.listdir(ann_dir))):
|
97 |
+
words = []
|
98 |
+
bboxes = []
|
99 |
+
original_bboxes = []
|
100 |
+
ner_tags = []
|
101 |
+
file_path = os.path.join(ann_dir, file)
|
102 |
+
with open(file_path, "r", encoding="utf8") as f:
|
103 |
+
data = json.load(f)
|
104 |
+
image_path = os.path.join(img_dir, file)
|
105 |
+
image_path = image_path.replace("json", "png")
|
106 |
+
image, size = load_image(image_path)
|
107 |
+
for item in data["form"]:
|
108 |
+
words_example, label = item["words"], item["label"]
|
109 |
+
words_example = [w for w in words_example if w["text"].strip() != ""]
|
110 |
+
if len(words_example) == 0:
|
111 |
+
continue
|
112 |
+
if label == "other":
|
113 |
+
for w in words_example:
|
114 |
+
words.append(w["text"])
|
115 |
+
ner_tags.append("O")
|
116 |
+
bboxes.append(normalize_bbox(w["box"], size))
|
117 |
+
original_bboxes.append(w["box"])
|
118 |
+
else:
|
119 |
+
words.append(words_example[0]["text"])
|
120 |
+
ner_tags.append("B-" + label.upper())
|
121 |
+
boxes.append(words_example[0]["box"])
|
122 |
+
for w in words_example[1:]:
|
123 |
+
words.append(w["text"])
|
124 |
+
ner_tags.append("I-" + label.upper())
|
125 |
+
bboxes.append(normalize_bbox(w["box"], size))
|
126 |
+
original_bboxes.append(w["box"])
|
127 |
+
yield guid, {"id": str(guid), "words": words, "boxes": boxes, "original_bboxes": original_bboxes, "ner_tags": ner_tags, "image": image}
|