Datasets:
Commit
·
bdc0f06
1
Parent(s):
50140f1
Update DocLayNet-large.py
Browse files- DocLayNet-large.py +12 -11
DocLayNet-large.py
CHANGED
|
@@ -25,7 +25,7 @@ DocLayNet dataset:
|
|
| 25 |
|
| 26 |
import json
|
| 27 |
import os
|
| 28 |
-
import base64
|
| 29 |
from PIL import Image
|
| 30 |
import datasets
|
| 31 |
|
|
@@ -127,7 +127,7 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 127 |
)
|
| 128 |
),
|
| 129 |
"image": datasets.features.Image(),
|
| 130 |
-
"pdf": datasets.Value("string"),
|
| 131 |
"page_hash": datasets.Value("string"), # unique identifier, equal to filename
|
| 132 |
"original_filename": datasets.Value("string"), # original document filename
|
| 133 |
"page_no": datasets.Value("int32"), # page number in original document
|
|
@@ -209,9 +209,10 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 209 |
logger.info("⏳ Generating examples from = %s", filepath)
|
| 210 |
ann_dirs = [os.path.join(filepath_0, "annotations"), os.path.join(filepath_1, "annotations"), os.path.join(filepath_2, "annotations"), os.path.join(filepath_3, "annotations")]
|
| 211 |
img_dirs = [os.path.join(filepath_0, "images"), os.path.join(filepath_1, "images"), os.path.join(filepath_2, "images"), os.path.join(filepath_3, "images")]
|
| 212 |
-
pdf_dirs = [os.path.join(filepath_0, "pdfs"), os.path.join(filepath_1, "pdfs"), os.path.join(filepath_2, "pdfs"), os.path.join(filepath_3, "pdfs")]
|
| 213 |
|
| 214 |
-
for ann_dir, img_dir, pdf_dir in zip(ann_dirs, img_dirs, pdf_dirs):
|
|
|
|
| 215 |
|
| 216 |
ann_listdir = os.listdir(ann_dir)
|
| 217 |
|
|
@@ -231,12 +232,12 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 231 |
image_path = image_path.replace("json", "png")
|
| 232 |
image, size = load_image(image_path)
|
| 233 |
|
| 234 |
-
# get pdf
|
| 235 |
-
pdf_path = os.path.join(pdf_dir, file)
|
| 236 |
-
pdf_path = pdf_path.replace("json", "pdf")
|
| 237 |
-
with open(pdf_path, "rb") as pdf_file:
|
| 238 |
-
|
| 239 |
-
|
| 240 |
|
| 241 |
for item in data["form"]:
|
| 242 |
text_example, category_example, bbox_block_example, bbox_line_example = item["text"], item["category"], item["box"], item["box_line"]
|
|
@@ -257,4 +258,4 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 257 |
collection = data["metadata"]["collection"]
|
| 258 |
doc_category = data["metadata"]["doc_category"]
|
| 259 |
|
| 260 |
-
yield guid, {"id": str(guid), "texts": texts, "bboxes_block": bboxes_block, "bboxes_line": bboxes_line, "categories": categories, "image": image, "
|
|
|
|
| 25 |
|
| 26 |
import json
|
| 27 |
import os
|
| 28 |
+
# import base64
|
| 29 |
from PIL import Image
|
| 30 |
import datasets
|
| 31 |
|
|
|
|
| 127 |
)
|
| 128 |
),
|
| 129 |
"image": datasets.features.Image(),
|
| 130 |
+
# "pdf": datasets.Value("string"),
|
| 131 |
"page_hash": datasets.Value("string"), # unique identifier, equal to filename
|
| 132 |
"original_filename": datasets.Value("string"), # original document filename
|
| 133 |
"page_no": datasets.Value("int32"), # page number in original document
|
|
|
|
| 209 |
logger.info("⏳ Generating examples from = %s", filepath)
|
| 210 |
ann_dirs = [os.path.join(filepath_0, "annotations"), os.path.join(filepath_1, "annotations"), os.path.join(filepath_2, "annotations"), os.path.join(filepath_3, "annotations")]
|
| 211 |
img_dirs = [os.path.join(filepath_0, "images"), os.path.join(filepath_1, "images"), os.path.join(filepath_2, "images"), os.path.join(filepath_3, "images")]
|
| 212 |
+
# pdf_dirs = [os.path.join(filepath_0, "pdfs"), os.path.join(filepath_1, "pdfs"), os.path.join(filepath_2, "pdfs"), os.path.join(filepath_3, "pdfs")]
|
| 213 |
|
| 214 |
+
# for ann_dir, img_dir, pdf_dir in zip(ann_dirs, img_dirs, pdf_dirs):
|
| 215 |
+
for ann_dir, img_dir in zip(ann_dirs, img_dirs):
|
| 216 |
|
| 217 |
ann_listdir = os.listdir(ann_dir)
|
| 218 |
|
|
|
|
| 232 |
image_path = image_path.replace("json", "png")
|
| 233 |
image, size = load_image(image_path)
|
| 234 |
|
| 235 |
+
# # get pdf
|
| 236 |
+
# pdf_path = os.path.join(pdf_dir, file)
|
| 237 |
+
# pdf_path = pdf_path.replace("json", "pdf")
|
| 238 |
+
# with open(pdf_path, "rb") as pdf_file:
|
| 239 |
+
# pdf_bytes = pdf_file.read()
|
| 240 |
+
# pdf_encoded_string = base64.b64encode(pdf_bytes)
|
| 241 |
|
| 242 |
for item in data["form"]:
|
| 243 |
text_example, category_example, bbox_block_example, bbox_line_example = item["text"], item["category"], item["box"], item["box_line"]
|
|
|
|
| 258 |
collection = data["metadata"]["collection"]
|
| 259 |
doc_category = data["metadata"]["doc_category"]
|
| 260 |
|
| 261 |
+
yield guid, {"id": str(guid), "texts": texts, "bboxes_block": bboxes_block, "bboxes_line": bboxes_line, "categories": categories, "image": image, "page_hash": page_hash, "original_filename": original_filename, "page_no": page_no, "num_pages": num_pages, "original_width": original_width, "original_height": original_height, "coco_width": coco_width, "coco_height": coco_height, "collection": collection, "doc_category": doc_category}
|