Datasets:
Upload GenDocVQA2024.py with huggingface_hub
Browse files- GenDocVQA2024.py +140 -0
GenDocVQA2024.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""This is a data loader for the GenDocVQA-2024 Dataset."""
|
15 |
+
|
16 |
+
|
17 |
+
import csv
|
18 |
+
import json
|
19 |
+
import os
|
20 |
+
import ast
|
21 |
+
import pandas as pd
|
22 |
+
|
23 |
+
import datasets
|
24 |
+
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
This dataset is dedicated to the non-extractive document visual question challenge GenDocVQA-2024.
|
27 |
+
"""
|
28 |
+
|
29 |
+
_URLS = {
|
30 |
+
'img_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA2024/resolve/main/archives/gendocvqa2024_imgs.tar.gz?download=true',
|
31 |
+
'ocr_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA2024/resolve/main/archives/gendocvqa2024_ocr.tar.gz?download=true',
|
32 |
+
'annotations_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA2024/resolve/main/archives/gendocvqa2024_annotations.tar.gz?download=true'
|
33 |
+
}
|
34 |
+
|
35 |
+
_LICENSE = "Other"
|
36 |
+
|
37 |
+
class GenDocVQA2024Small(datasets.GeneratorBasedBuilder):
|
38 |
+
|
39 |
+
VERSION = datasets.Version("1.0.0")
|
40 |
+
|
41 |
+
BUILDER_CONFIGS = [
|
42 |
+
datasets.BuilderConfig(name="default", version=VERSION, description="Whole dataset config"),
|
43 |
+
]
|
44 |
+
|
45 |
+
DEFAULT_CONFIG_NAME = "default"
|
46 |
+
|
47 |
+
def _info(self):
|
48 |
+
features = datasets.Features(
|
49 |
+
{
|
50 |
+
"unique_id": datasets.Value("int64"),
|
51 |
+
"image_path": datasets.Value("string"),
|
52 |
+
"ocr": datasets.Sequence(feature={"text": datasets.Value("string"), "bbox": datasets.Sequence(datasets.Value("int64")),
|
53 |
+
'block_id': datasets.Value("int64"),
|
54 |
+
'text_id': datasets.Value("int64"),
|
55 |
+
'par_id': datasets.Value("int64"),
|
56 |
+
'line_id': datasets.Value("int64"),
|
57 |
+
'word_id': datasets.Value("int64")
|
58 |
+
}),
|
59 |
+
"question": datasets.Value("string"),
|
60 |
+
"answer": datasets.Sequence(datasets.Value("string")),
|
61 |
+
|
62 |
+
}
|
63 |
+
)
|
64 |
+
|
65 |
+
return datasets.DatasetInfo(
|
66 |
+
|
67 |
+
features=features,
|
68 |
+
description=_DESCRIPTION,
|
69 |
+
license=_LICENSE
|
70 |
+
)
|
71 |
+
|
72 |
+
def _split_generators(self, dl_manager):
|
73 |
+
imgs_dir = dl_manager.download_and_extract(_URLS["img_tar"])
|
74 |
+
ocr_dir = dl_manager.download_and_extract(_URLS["ocr_tar"])
|
75 |
+
annotations_dir = dl_manager.download_and_extract(_URLS["annotations_tar"])
|
76 |
+
|
77 |
+
return [
|
78 |
+
datasets.SplitGenerator(
|
79 |
+
name=datasets.Split.TRAIN,
|
80 |
+
gen_kwargs={
|
81 |
+
"annot_path": annotations_dir,
|
82 |
+
"imgs_dir": imgs_dir,
|
83 |
+
"ocr_dir": ocr_dir,
|
84 |
+
"split": "train",
|
85 |
+
},
|
86 |
+
),
|
87 |
+
datasets.SplitGenerator(
|
88 |
+
name=datasets.Split.VALIDATION,
|
89 |
+
gen_kwargs={
|
90 |
+
"annot_path": annotations_dir,
|
91 |
+
"imgs_dir": imgs_dir,
|
92 |
+
"ocr_dir": ocr_dir,
|
93 |
+
"split": "dev",
|
94 |
+
},
|
95 |
+
)
|
96 |
+
]
|
97 |
+
|
98 |
+
|
99 |
+
def _generate_examples(self, annot_path, imgs_dir, ocr_dir, split):
|
100 |
+
df = pd.read_csv(os.path.join(annot_path, 'gendocvqa2024_annotations', f'{split}_v1.csv'))
|
101 |
+
for _, row in df.iterrows():
|
102 |
+
img_path = os.path.join(imgs_dir, 'gendocvqa2024_imgs', split, row['image_filename'])
|
103 |
+
q_id = row['unique_id']
|
104 |
+
ocr_path = os.path.join(ocr_dir, 'gendocvqa2024_ocr', split, row['ocr_filename'])
|
105 |
+
question = row['question']
|
106 |
+
answer = row['answer']
|
107 |
+
with open(ocr_path, 'r') as f:
|
108 |
+
ocr = json.load(f)
|
109 |
+
ocr_list = []
|
110 |
+
for item in ocr:
|
111 |
+
ocr_dict = {
|
112 |
+
'block_id': item[0],
|
113 |
+
'text_id': item[1],
|
114 |
+
'par_id': item[2],
|
115 |
+
'line_id': item[3],
|
116 |
+
'word_id': item[4],
|
117 |
+
'bbox': item[5],
|
118 |
+
'text': item[6]
|
119 |
+
}
|
120 |
+
ocr_list.append(ocr_dict)
|
121 |
+
if split != "test":
|
122 |
+
answer = ast.literal_eval(answer)
|
123 |
+
else:
|
124 |
+
answer = []
|
125 |
+
|
126 |
+
yield q_id, {
|
127 |
+
"unique_id": q_id,
|
128 |
+
"image_path": img_path,
|
129 |
+
"ocr": ocr_list,
|
130 |
+
"answer": answer,
|
131 |
+
"question": question,
|
132 |
+
|
133 |
+
}
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
def read_image(img_path):
|
138 |
+
with Image.open(img_path) as f:
|
139 |
+
original_image = f.convert("RGB")
|
140 |
+
return original_image
|