|
import json |
|
import os |
|
import ast |
|
from pathlib import Path |
|
import datasets |
|
from PIL import Image |
|
import pandas as pd |
|
import glob |
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
_CITATION = """\ |
|
@article{, |
|
title={}, |
|
author={}, |
|
journal={}, |
|
year={}, |
|
volume={} |
|
} |
|
""" |
|
_DESCRIPTION = """\ |
|
This is a sample dataset for training layoutlmv3 model on custom annotated data. |
|
""" |
|
|
|
def load_image(image_path): |
|
image = Image.open(image_path).convert("RGB") |
|
w, h = image.size |
|
return image, (w,h) |
|
|
|
def normalize_bbox(bbox, size): |
|
return [ |
|
int(1000 * bbox[0] / size[0]), |
|
int(1000 * bbox[1] / size[1]), |
|
int(1000 * bbox[2] / size[0]), |
|
int(1000 * bbox[3] / size[1]), |
|
] |
|
|
|
_URLS = [] |
|
data_dir = r"D:\Study\LayoutLMV3\data_ne" |
|
|
|
class DatasetConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for InvoiceExtraction Dataset""" |
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for InvoiceExtraction Dataset. |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(DatasetConfig, self).__init__(**kwargs) |
|
|
|
class InvoiceExtraction(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
DatasetConfig(name="InvoiceExtraction", version=datasets.Version("1.0.0"), description="InvoiceExtraction dataset"), |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))), |
|
"ner_tags": datasets.Sequence( |
|
datasets.features.ClassLabel( |
|
names = [ 'address', 'company_name', 'customer_id', 'invoice_id', 'invoice_date', 'invoice_total', 'sub_total', 'total_tax', 'item', 'amount', |
|
] |
|
) |
|
), |
|
"image_path": datasets.Value("string"), |
|
"image": datasets.features.Image() |
|
} |
|
), |
|
supervised_keys=None, |
|
citation=_CITATION, |
|
homepage="", |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
"""Uses local files located with data_dir""" |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "dataset/training_data/")} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "dataset/testing_data/")} |
|
), |
|
] |
|
|
|
def get_line_bbox(self, bboxs): |
|
x = [bboxs[i][j] for i in range(len(bboxs)) for j in range(0, len(bboxs[i]), 2)] |
|
y = [bboxs[i][j] for i in range(len(bboxs)) for j in range(1, len(bboxs[i]), 2)] |
|
|
|
x0, y0, x1, y1 = min(x), min(y), max(x), max(y) |
|
|
|
assert x1 >= x0 and y1 >= y0 |
|
bbox = [[x0, y0, x1, y1] for _ in range(len(bboxs))] |
|
return bbox |
|
|
|
def _generate_examples(self, filepath): |
|
logger.info("⏳ Generating examples from = %s", filepath) |
|
ann_dir = os.path.join(filepath, "annotations") |
|
img_dir = os.path.join(filepath, "images") |
|
|
|
for guid, file in enumerate(sorted(os.listdir(ann_dir))): |
|
tokens = [] |
|
bboxes = [] |
|
ner_tags = [] |
|
|
|
file_path = os.path.join(ann_dir, file) |
|
with open(file_path, "r", encoding="utf8") as f: |
|
data = json.load(f) |
|
|
|
image_path = os.path.join(img_dir, file.replace('.json', '.png')) |
|
print("Image Path:", image_path) |
|
image, size = load_image(image_path) |
|
|
|
for item in data["form"]: |
|
cur_line_bboxes = [] |
|
words, label = item["words"], item["label"] |
|
words = [w for w in words if w["text"].strip() != ""] |
|
|
|
if len(words) == 0: |
|
continue |
|
|
|
if label == "other": |
|
for w in words: |
|
tokens.append(w["text"]) |
|
ner_tags.append("O") |
|
cur_line_bboxes.append(normalize_bbox(w["box"], size)) |
|
else: |
|
tokens.append(words[0]["text"]) |
|
ner_tags.append("B-" + label.upper()) |
|
cur_line_bboxes.append(normalize_bbox(words[0]["box"], size)) |
|
|
|
for w in words[1:]: |
|
tokens.append(w["text"]) |
|
ner_tags.append("I-" + label.upper()) |
|
cur_line_bboxes.append(normalize_bbox(w["box"], size)) |
|
|
|
cur_line_bboxes = self.get_line_bbox(cur_line_bboxes) |
|
bboxes.extend(cur_line_bboxes) |
|
|
|
yield guid, {"id": str(guid), "tokens": tokens, "bboxes": bboxes, "ner_tags": ner_tags, "image": image} |
|
|