Datasets:
File size: 5,105 Bytes
f1d001d 7cce71e f1d001d 7cce71e f1d001d 7cce71e f1d001d 673d94c f1d001d |
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 |
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This is a data loader for the GenDocVQA Dataset."""
import csv
import json
import os
import ast
import pandas as pd
import datasets
_DESCRIPTION = """\
This dataset is dedicated to the non-extractive document visual question challenge GenDocVQA-2024.
"""
_URLS = {
'img_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA/resolve/main/archives/gendocvqa2024_imgs.tar.gz?download=true',
'ocr_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA/resolve/main/archives/gendocvqa2024_ocr.tar.gz?download=true',
'annotations_tar': 'https://huggingface.co/datasets/lenagibee/GenDocVQA/resolve/main/archives/gendocvqa2024_annotations.tar.gz?download=true'
}
_LICENSE = "Other"
class GenDocVQA(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="default", version=VERSION, description="Whole dataset config"),
]
DEFAULT_CONFIG_NAME = "default"
def _info(self):
features = datasets.Features(
{
"unique_id": datasets.Value("int64"),
"image_path": datasets.Value("string"),
"ocr": datasets.Sequence(
feature={
'text': datasets.Value("string"),
'bbox': datasets.Sequence(datasets.Value("int64")),
'block_id': datasets.Value("int64"),
'text_id': datasets.Value("int64"),
'par_id': datasets.Value("int64"),
'line_id': datasets.Value("int64"),
'word_id': datasets.Value("int64")
}
),
"question": datasets.Value("string"),
"answer": datasets.Sequence(datasets.Value("string")),
}
)
return datasets.DatasetInfo(
features=features,
description=_DESCRIPTION,
license=_LICENSE
)
def _split_generators(self, dl_manager):
imgs_dir = dl_manager.download_and_extract(_URLS["img_tar"])
ocr_dir = dl_manager.download_and_extract(_URLS["ocr_tar"])
annotations_dir = dl_manager.download_and_extract(_URLS["annotations_tar"])
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"annot_path": annotations_dir,
"imgs_dir": imgs_dir,
"ocr_dir": ocr_dir,
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"annot_path": annotations_dir,
"imgs_dir": imgs_dir,
"ocr_dir": ocr_dir,
"split": "dev",
},
)
]
def _generate_examples(self, annot_path, imgs_dir, ocr_dir, split):
df = pd.read_csv(os.path.join(annot_path, 'gendocvqa2024_annotations', f'{split}_v1.csv'))
for _, row in df.iterrows():
img_path = os.path.join(imgs_dir, 'gendocvqa2024_imgs', split, row['image_filename'])
q_id = row['unique_id']
ocr_path = os.path.join(ocr_dir, 'gendocvqa2024_ocr', split, row['ocr_filename'])
question = row['question']
answer = row['answer']
with open(ocr_path, 'r') as f:
ocr = json.load(f)
ocr_list = []
for item in ocr:
ocr_dict = {
'block_id': item[0],
'text_id': item[1],
'par_id': item[2],
'line_id': item[3],
'word_id': item[4],
'bbox': item[5],
'text': item[6]
}
ocr_list.append(ocr_dict)
if split != "test":
answer = ast.literal_eval(answer)
else:
answer = []
yield q_id, {
"unique_id": q_id,
"image_path": img_path,
"ocr": ocr_list,
"answer": answer,
"question": question,
}
def read_image(img_path):
with Image.open(img_path) as f:
original_image = f.convert("RGB")
return original_image |