Datasets:
Commit
·
d5abf6a
1
Parent(s):
9cbb1ca
Update DocLayNet-base.py
Browse files- DocLayNet-base.py +18 -17
DocLayNet-base.py
CHANGED
|
@@ -129,7 +129,6 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 129 |
"coco_height": datasets.Value("int32"), # with in pixels in PNG and COCO format
|
| 130 |
"collection": datasets.Value("string"), # sub-collection name
|
| 131 |
"doc_category": datasets.Value("string"), # category type of the document
|
| 132 |
-
|
| 133 |
}
|
| 134 |
)
|
| 135 |
|
|
@@ -163,7 +162,7 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 163 |
name=datasets.Split.TRAIN,
|
| 164 |
# These kwargs will be passed to _generate_examples
|
| 165 |
gen_kwargs={
|
| 166 |
-
"filepath": f"{downloaded_file}/
|
| 167 |
"split": "train",
|
| 168 |
},
|
| 169 |
),
|
|
@@ -171,7 +170,7 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 171 |
name=datasets.Split.VALIDATION,
|
| 172 |
# These kwargs will be passed to _generate_examples
|
| 173 |
gen_kwargs={
|
| 174 |
-
"filepath": f"{downloaded_file}/
|
| 175 |
"split": "dev",
|
| 176 |
},
|
| 177 |
),
|
|
@@ -179,7 +178,7 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 179 |
name=datasets.Split.TEST,
|
| 180 |
# These kwargs will be passed to _generate_examples
|
| 181 |
gen_kwargs={
|
| 182 |
-
"filepath": f"{downloaded_file}/
|
| 183 |
"split": "test"
|
| 184 |
},
|
| 185 |
),
|
|
@@ -195,33 +194,23 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 195 |
bboxes_block = []
|
| 196 |
bboxes_line = []
|
| 197 |
categories = []
|
|
|
|
| 198 |
# get json
|
| 199 |
file_path = os.path.join(ann_dir, file)
|
| 200 |
with open(file_path, "r", encoding="utf8") as f:
|
| 201 |
data = json.load(f)
|
|
|
|
| 202 |
# get image
|
| 203 |
image_path = os.path.join(img_dir, file)
|
| 204 |
image_path = image_path.replace("json", "png")
|
| 205 |
image, size = load_image(image_path)
|
|
|
|
| 206 |
# get pdf
|
| 207 |
pdf_path = os.path.join(pdf_dir, file)
|
| 208 |
pdf_path = pdf_path.replace("json", "pdf")
|
| 209 |
with open(pdf_path, "rb") as pdf_file:
|
| 210 |
pdf_bytes = pdf_file.read()
|
| 211 |
pdf_encoded_string = base64.b64encode(pdf_bytes)
|
| 212 |
-
# get all data
|
| 213 |
-
|
| 214 |
-
for item in data["metadata"]:
|
| 215 |
-
page_hash = item["page_hash"]
|
| 216 |
-
original_filename = item["original_filename"]
|
| 217 |
-
page_no = item["page_no"]
|
| 218 |
-
num_pages = item["num_pages"]
|
| 219 |
-
original_width = item["original_width"]
|
| 220 |
-
original_height = item["original_height"]
|
| 221 |
-
coco_width = item["coco_width"]
|
| 222 |
-
coco_height = item["coco_height"]
|
| 223 |
-
collection = item["collection"]
|
| 224 |
-
doc_category = item["doc_category"]
|
| 225 |
|
| 226 |
for item in data["form"]:
|
| 227 |
text_example, category_example, bbox_block_example, bbox_line_example = item["text"], item["category"], item["box"], item["box_line"]
|
|
@@ -230,4 +219,16 @@ class DocLayNet(datasets.GeneratorBasedBuilder):
|
|
| 230 |
bboxes_block.append(bbox_block_example)
|
| 231 |
bboxes_line.append(bbox_line_example)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
yield guid, {"id": str(guid), "texts": texts, "bboxes_block": bboxes_block, "bboxes_line": bboxes_line, "categories": categories, "image": image, "pdf": pdf_encoded_string, "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}
|
|
|
|
| 129 |
"coco_height": datasets.Value("int32"), # with in pixels in PNG and COCO format
|
| 130 |
"collection": datasets.Value("string"), # sub-collection name
|
| 131 |
"doc_category": datasets.Value("string"), # category type of the document
|
|
|
|
| 132 |
}
|
| 133 |
)
|
| 134 |
|
|
|
|
| 162 |
name=datasets.Split.TRAIN,
|
| 163 |
# These kwargs will be passed to _generate_examples
|
| 164 |
gen_kwargs={
|
| 165 |
+
"filepath": f"{downloaded_file}/base_dataset/train/",
|
| 166 |
"split": "train",
|
| 167 |
},
|
| 168 |
),
|
|
|
|
| 170 |
name=datasets.Split.VALIDATION,
|
| 171 |
# These kwargs will be passed to _generate_examples
|
| 172 |
gen_kwargs={
|
| 173 |
+
"filepath": f"{downloaded_file}/base_dataset/val/",
|
| 174 |
"split": "dev",
|
| 175 |
},
|
| 176 |
),
|
|
|
|
| 178 |
name=datasets.Split.TEST,
|
| 179 |
# These kwargs will be passed to _generate_examples
|
| 180 |
gen_kwargs={
|
| 181 |
+
"filepath": f"{downloaded_file}/base_dataset/test/",
|
| 182 |
"split": "test"
|
| 183 |
},
|
| 184 |
),
|
|
|
|
| 194 |
bboxes_block = []
|
| 195 |
bboxes_line = []
|
| 196 |
categories = []
|
| 197 |
+
|
| 198 |
# get json
|
| 199 |
file_path = os.path.join(ann_dir, file)
|
| 200 |
with open(file_path, "r", encoding="utf8") as f:
|
| 201 |
data = json.load(f)
|
| 202 |
+
|
| 203 |
# get image
|
| 204 |
image_path = os.path.join(img_dir, file)
|
| 205 |
image_path = image_path.replace("json", "png")
|
| 206 |
image, size = load_image(image_path)
|
| 207 |
+
|
| 208 |
# get pdf
|
| 209 |
pdf_path = os.path.join(pdf_dir, file)
|
| 210 |
pdf_path = pdf_path.replace("json", "pdf")
|
| 211 |
with open(pdf_path, "rb") as pdf_file:
|
| 212 |
pdf_bytes = pdf_file.read()
|
| 213 |
pdf_encoded_string = base64.b64encode(pdf_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
for item in data["form"]:
|
| 216 |
text_example, category_example, bbox_block_example, bbox_line_example = item["text"], item["category"], item["box"], item["box_line"]
|
|
|
|
| 219 |
bboxes_block.append(bbox_block_example)
|
| 220 |
bboxes_line.append(bbox_line_example)
|
| 221 |
|
| 222 |
+
# get all metadadata
|
| 223 |
+
page_hash = data["metadata"]["page_hash"]
|
| 224 |
+
original_filename = data["metadata"]["original_filename"]
|
| 225 |
+
page_no = data["metadata"]["page_no"]
|
| 226 |
+
num_pages = data["metadata"]["num_pages"]
|
| 227 |
+
original_width = data["metadata"]["original_width"]
|
| 228 |
+
original_height = data["metadata"]["original_height"]
|
| 229 |
+
coco_width = data["metadata"]["coco_width"]
|
| 230 |
+
coco_height = data["metadata"]["coco_height"]
|
| 231 |
+
collection = data["metadata"]["collection"]
|
| 232 |
+
doc_category = data["metadata"]["doc_category"]
|
| 233 |
+
|
| 234 |
yield guid, {"id": str(guid), "texts": texts, "bboxes_block": bboxes_block, "bboxes_line": bboxes_line, "categories": categories, "image": image, "pdf": pdf_encoded_string, "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}
|