Upload cvdataset-layoutlmv3.py (#5)
Browse files- Upload cvdataset-layoutlmv3.py (99ec04f5ef5054baf038d8285bc4b84c189701b5)
- cvdataset-layoutlmv3.py +128 -0
cvdataset-layoutlmv3.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import datasets
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
logger = datasets.logging.get_logger(__name__)
|
| 9 |
+
_CITATION = """\
|
| 10 |
+
@article{LayoutLmv3 for CV extractions,
|
| 11 |
+
title={LayoutLmv3for Key Information Extraction},
|
| 12 |
+
author={MisaR&D Team},
|
| 13 |
+
year={2022},
|
| 14 |
+
}
|
| 15 |
+
"""
|
| 16 |
+
_DESCRIPTION = """\
|
| 17 |
+
CV is a collection of receipts. It contains, for each photo about cv personal, a list of OCRs - with the bounding box, text, and class. The goal is to benchmark "key information extraction" - extracting key information from documents
|
| 18 |
+
https://arxiv.org/abs/2103.14470
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def load_image(image_path):
|
| 22 |
+
image = Image.open(image_path)
|
| 23 |
+
w, h = image.size
|
| 24 |
+
return image, (w,h)
|
| 25 |
+
|
| 26 |
+
def normalize_bbox(bbox, size):
|
| 27 |
+
return [
|
| 28 |
+
int(1000 * bbox[0] / size[0]),
|
| 29 |
+
int(1000 * bbox[1] / size[1]),
|
| 30 |
+
int(1000 * bbox[2] / size[0]),
|
| 31 |
+
int(1000 * bbox[3] / size[1]),
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
def _get_drive_url(url):
|
| 35 |
+
base_url = 'https://drive.google.com/uc?id='
|
| 36 |
+
split_url = url.split('/')
|
| 37 |
+
|
| 38 |
+
return base_url + split_url[5]
|
| 39 |
+
|
| 40 |
+
_URLS = [
|
| 41 |
+
_get_drive_url("https://drive.google.com/file/d/1KdDBmGP96lFc7jv2Bf4eqrO121ST-TCh/"),
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
class DatasetConfig(datasets.BuilderConfig):
|
| 45 |
+
"""BuilderConfig for WildReceipt Dataset"""
|
| 46 |
+
def __init__(self, **kwargs):
|
| 47 |
+
"""BuilderConfig for WildReceipt Dataset.
|
| 48 |
+
Args:
|
| 49 |
+
**kwargs: keyword arguments forwarded to super.
|
| 50 |
+
"""
|
| 51 |
+
super(DatasetConfig, self).__init__(**kwargs)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class WildReceipt(datasets.GeneratorBasedBuilder):
|
| 55 |
+
BUILDER_CONFIGS = [
|
| 56 |
+
DatasetConfig(name="CV Extractions", version=datasets.Version("1.0.0"), description="CV dataset"),
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
def _info(self):
|
| 60 |
+
return datasets.DatasetInfo(
|
| 61 |
+
description=_DESCRIPTION,
|
| 62 |
+
features=datasets.Features(
|
| 63 |
+
{
|
| 64 |
+
"id": datasets.Value("string"),
|
| 65 |
+
"words": datasets.Sequence(datasets.Value("string")),
|
| 66 |
+
"bboxes": datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
|
| 67 |
+
"ner_tags": datasets.Sequence(
|
| 68 |
+
datasets.features.ClassLabel(
|
| 69 |
+
names=['person_name', 'dob_key', 'dob_value', 'gender_key', 'gender_value', 'phonenumber_key', 'phonenumber_value', 'email_key', 'email_value', 'address_key', 'address_value', 'socical_address_value', 'education', 'education_name', 'education_time', 'experience', 'experience_name', 'experience_time', 'information', 'undefined']
|
| 70 |
+
)
|
| 71 |
+
),
|
| 72 |
+
"image_path": datasets.Value("string"),
|
| 73 |
+
}
|
| 74 |
+
),
|
| 75 |
+
supervised_keys=None,
|
| 76 |
+
citation=_CITATION,
|
| 77 |
+
homepage="",
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def _split_generators(self, dl_manager):
|
| 81 |
+
"""Returns SplitGenerators."""
|
| 82 |
+
"""Uses local files located with data_dir"""
|
| 83 |
+
downloaded_file = dl_manager.download_and_extract(_URLS)
|
| 84 |
+
dest = Path(downloaded_file[0])/'data1'
|
| 85 |
+
|
| 86 |
+
return [
|
| 87 |
+
datasets.SplitGenerator(
|
| 88 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepath": dest/"train.txt", "dest": dest}
|
| 89 |
+
),
|
| 90 |
+
datasets.SplitGenerator(
|
| 91 |
+
name=datasets.Split.TEST, gen_kwargs={"filepath": dest/"test.txt", "dest": dest}
|
| 92 |
+
),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
def _generate_examples(self, filepath, dest):
|
| 96 |
+
|
| 97 |
+
df = pd.read_csv(dest/'class_list.txt', delimiter='\s', header=None)
|
| 98 |
+
id2labels = dict(zip(df[0].tolist(), df[1].tolist()))
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
| 102 |
+
|
| 103 |
+
item_list = []
|
| 104 |
+
with open(filepath, 'r') as f:
|
| 105 |
+
for line in f:
|
| 106 |
+
item_list.append(line.rstrip('\n\r'))
|
| 107 |
+
|
| 108 |
+
for guid, fname in enumerate(item_list):
|
| 109 |
+
|
| 110 |
+
data = json.loads(fname)
|
| 111 |
+
image_path = dest/data['file_name']
|
| 112 |
+
image, size = load_image(image_path)
|
| 113 |
+
boxes = [[i['box'][6], i['box'][7], i['box'][2], i['box'][3]] for i in data['annotations']]
|
| 114 |
+
|
| 115 |
+
text = [i['text'] for i in data['annotations']]
|
| 116 |
+
label = [id2labels[i['label']] for i in data['annotations']]
|
| 117 |
+
|
| 118 |
+
boxes = [normalize_bbox(box, size) for box in boxes]
|
| 119 |
+
|
| 120 |
+
flag=0
|
| 121 |
+
for i in boxes:
|
| 122 |
+
for j in i:
|
| 123 |
+
if j>1000:
|
| 124 |
+
flag+=1
|
| 125 |
+
pass
|
| 126 |
+
if flag>0: print(image_path)
|
| 127 |
+
|
| 128 |
+
yield guid, {"id": str(guid), "words": text, "bboxes": boxes, "ner_tags": label, "image_path": image_path}
|