jinho8345 commited on
Commit
8d22c51
·
1 Parent(s): 035ee24

add loading script for local files

Browse files
Files changed (1) hide show
  1. cdip-annotations-formnet.py +109 -0
cdip-annotations-formnet.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from pathlib import Path
4
+
5
+ from PIL import Image
6
+
7
+ import datasets
8
+
9
+ logger = datasets.logging.get_logger(__name__)
10
+ def load_json(json_path):
11
+ with open(json_path, "r") as f:
12
+ data = json.load(f)
13
+ return data
14
+
15
+
16
+
17
+ _CITATION = """\
18
+ @article{Jaume2019FUNSDAD,
19
+ title={FUNSD: A Dataset for Form Understanding in Noisy Scanned Documents},
20
+ author={Guillaume Jaume and H. K. Ekenel and J. Thiran},
21
+ journal={2019 International Conference on Document Analysis and Recognition Workshops (ICDARW)},
22
+ year={2019},
23
+ volume={2},
24
+ pages={1-6}
25
+ }
26
+ """
27
+
28
+ _DESCRIPTION = """\
29
+ https://guillaumejaume.github.io/FUNSD/
30
+ """
31
+
32
+
33
+ class CDIPOcrConfig(datasets.BuilderConfig):
34
+ """BuilderConfig for FUNSD"""
35
+
36
+ def __init__(self, **kwargs):
37
+ """BuilderConfig for FUNSD.
38
+ Args:
39
+ **kwargs: keyword arguments forwarded to super.
40
+ """
41
+ super(CDIPOcrConfig, self).__init__(**kwargs)
42
+
43
+
44
+ class CDIPOcr(datasets.GeneratorBasedBuilder):
45
+ """Conll2003 dataset."""
46
+
47
+ BUILDER_CONFIGS = [
48
+ CDIPOcrConfig(name="CDIP_OCR", version=datasets.Version("1.0.0"), description="CDIP OCR"),
49
+ ]
50
+
51
+ def _info(self):
52
+ return datasets.DatasetInfo(
53
+ description=_DESCRIPTION,
54
+ features=datasets.Features(
55
+ {
56
+ "id": datasets.Value("string"),
57
+ "words": datasets.Sequence(datasets.Value("string")),
58
+ "bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
59
+ }
60
+ ),
61
+ )
62
+
63
+ def _split_generators(self, dl_manager):
64
+ """Returns SplitGenerators."""
65
+ # downloaded_file = dl_manager.download_and_extract("https://guillaumejaume.github.io/FUNSD/dataset.zip")
66
+ return [
67
+ datasets.SplitGenerator(
68
+ name=datasets.Split.TRAIN, gen_kwargs={"annot_root": f"/home/jinho/datasets/IIT-CDIP-annotations"}
69
+ ),
70
+ ]
71
+
72
+ def _generate_examples(self, annot_root):
73
+ logger.info("⏳ Generating examples from = %s", annot_root)
74
+
75
+ cnt = 0
76
+ start_from = 'o.a.f'
77
+ start_flag = False
78
+ for subfolder in sorted(list(os.listdir(annot_root))): # b.a, b.b
79
+ subfolder_path = os.path.join(annot_root, subfolder)
80
+ logger.info(f"{subfolder = }")
81
+
82
+ for json_folder in os.listdir(subfolder_path): # b.a.p, b.a.c
83
+ json_folder_path = os.path.join(subfolder_path, json_folder)
84
+
85
+
86
+ if not start_flag:
87
+ if start_from != json_folder:
88
+ continue
89
+ else:
90
+ start_flag = True
91
+
92
+ json_paths = list(Path(json_folder_path).glob("**/*.json"))
93
+ logger.info(f"{json_folder, len(json_paths) = }")
94
+
95
+ for idx, json_path in enumerate(json_paths):
96
+ words = []
97
+ bboxes = []
98
+ data = load_json(json_path)
99
+ for block in data["blocks"]:
100
+ for paragraph in block["paragraphs"]:
101
+ for line in paragraph["lines"]:
102
+ line_words = line["words"]
103
+ line_texts = [w["text"] for w in line_words]
104
+ line_bboxes = [w["box"] for w in line_words]
105
+ words.extend(line_texts)
106
+ bboxes.extend(line_bboxes)
107
+ cnt += 1
108
+
109
+ yield cnt, {"id": str(json_path.name), "words": words, "bboxes": bboxes }