| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """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 |