forked from spyysalo/taggedpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate.py
More file actions
525 lines (442 loc) · 15.5 KB
/
Copy pathannotate.py
File metadata and controls
525 lines (442 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python3
# Annotate PDF files based on their logical structure.
import sys
import re
import json
import uuid
from collections import defaultdict
from argparse import ArgumentParser
from pdfminer.layout import LTLine, LTRect
from PyPDF2 import PdfFileWriter, PdfFileReader
from taggedpdf import TaggedPdf
from taggedpdf.bbox import BBox
from taggedpdf.ltitem import layout_item_xml_string
from taggedpdf.layout import split_into_columns
from taggedpdf.pawls import load_pawls_structure
from taggedpdf.utils import file_sha256
from taggedpdf.annotation import Annotation, render_annotations
from taggedpdf.config import COCO_CATEGORIES, STRUCT_TYPE_TO_LABEL_MAP
from taggedpdf.logger import logger
def argparser():
ap = ArgumentParser()
ap.add_argument(
'--no-text-bbox-trim',
default=False,
action='store_true',
help='do not trim bounding boxes of text elements'
)
ap.add_argument(
'--no-table-bbox-extension',
default=False,
action='store_true',
help='do not extend bounding boxes of tables to outline'
)
ap.add_argument(
'--no-crop',
default=False,
action='store_true',
help='do not crop annotations to page cropboxes'
)
ap.add_argument(
'--no-captions',
default=False,
action='store_true',
help='do not assign Caption labels'
)
ap.add_argument(
'--keep-overlaps',
default=False,
action='store_true',
help='do not eliminate overlapping annotations'
)
ap.add_argument(
'--include-content',
default=False,
action='store_true',
help='include content items in output (XML output only)'
)
ap.add_argument(
'--pawls-structure',
default=None,
metavar='JSON',
help='PAWLS pdf_structure.json for document (PAWLS output only)'
)
ap.add_argument(
'--format',
choices={ 'pdf', 'xml', 'pawls', 'coco' },
default='pdf',
help='output format'
)
ap.add_argument('input')
ap.add_argument('output')
return ap
# Set of structure types that are annotated
ANNOTATED_TYPES = set(STRUCT_TYPE_TO_LABEL_MAP.keys())
# Set of structure types that can potentially span multiple columns
MULTICOLUMN_STRUCT_TYPES = { 'P', 'LI' }
# Set of annotation labels with trimmable text boxes
TRIMMABLE_STRUCT_TYPES = {
'P',
'H1',
'H2',
'H3',
'H4',
'H5',
'H6',
'Note',
'Caption',
# consider also:
# 'TableOfContents',
# 'TocItem',
# 'Figure',
}
# Structure types to recurse into for generating annotations
STRUCT_TYPE_TO_RECURSE_INTO = {
'Document',
'NonStruct',
'Part',
'Art',
'Sect',
'Div',
'P',
'L',
'Unknown',
}
# Structure types that may have captions
STRUCT_TYPES_WITH_CAPTIONS = {
'Table',
'Figure',
}
# Structure type to assign to captions
CAPTION_STRUCT_TYPE = 'Caption'
# Number of units to expand minimal annnotation bboxes by
EXPAND_BBOX_BY = {
'Paragraph': 3,
'ListItem': 3,
'Title': 3,
'TableOfContents': 3,
'Footnote': 2, # assume slightly smaller font than usual
}
def _annotations_for_node(page, node, args):
type_ = str(node.struct_type)[1:]
content = node.get_content(page, recursive=True)
if node.get_bbox(page) is None:
pass
elif not content:
logger.warning(f'no content for {type_} on page {page}')
pass
elif type_ not in ANNOTATED_TYPES:
pass
elif type_ not in MULTICOLUMN_STRUCT_TYPES:
yield Annotation(type_, node.get_bbox(page), page, content)
else:
# potentially multicolumn
columns = split_into_columns(
node.get_content(page, recursive=True),
node.get_bbox(page)
)
if len(columns) > 1:
print(f'{len(columns)} columns on page {page} for {type_}')
print(f'"{node.get_content_text(page, recursive=True)}"')
for items in columns:
bbox = BBox.from_layout_items(items)
yield Annotation(type_, bbox, page, items)
def _get_annotations(page, node, args):
yield from _annotations_for_node(page, node, args)
type_ = str(node.struct_type)[1:]
if type_ in STRUCT_TYPE_TO_RECURSE_INTO:
for child in node.children:
yield from _get_annotations(page, child, args)
def find_overlaps(annotations):
overlaps = []
for i in range(len(annotations)):
for j in range(i+1, len(annotations)):
a1, a2 = annotations[i], annotations[j]
if a1.bbox.overlaps(a2.bbox):
overlaps.append((a1, a2))
return overlaps
def find_duplicates(page, overlaps, ignore=None):
if ignore is None:
ignore = set()
duplicates = set()
for a1, a2 in overlaps:
if a1.bbox == a2.bbox and a1 not in ignore and a2 not in ignore:
duplicates.add(a2) # arbitrary choice
return duplicates
def find_contained(page, overlaps, ignore=None):
if ignore is None:
ignore = set()
contained = set()
for a1, a2 in overlaps:
if a1.bbox == a2.bbox:
pass # identical bboxes
elif a1.bbox.contains(a2.bbox) and a1 not in ignore:
contained.add(a2)
elif a2.bbox.contains(a1.bbox) and a2 not in ignore:
contained.add(a1)
return contained
def eliminate_overlaps(page, annotations):
overlaps = find_overlaps(annotations)
eliminated = find_duplicates(page, overlaps)
eliminated |= find_contained(page, overlaps, eliminated)
annotations = [a for a in annotations if a not in eliminated]
# remaining overlaps
for i in range(len(annotations)):
for j in range(i+1, len(annotations)):
a1, a2 = annotations[i], annotations[j]
if a1.bbox.overlaps(a2.bbox):
print(
'OVERLAP: page', page,
a1.type, a2.type,
a1.bbox.jaccard(a2.bbox),
a1.bbox.relative_overlap(a2.bbox),
a1.bbox.contains(a2.bbox),
a2.bbox.relative_overlap(a1.bbox),
a2.bbox.contains(a1.bbox)
)
return annotations
def extend_table_bboxes(page, annotations, nonmarked, margin=4):
# Consider lines and recangles that were not marked content as
# candidate table outlines
candidates = [i for i in nonmarked if isinstance(i, (LTLine, LTRect))]
tables = [a for a in annotations if a.type == 'Table']
while True:
extended = False
# Find potential outline items that overlap exactly one table,
# keep non-overlapping for subsequent extension. (Discard
# candidates that overlap multiple tables to avoid introducing
# new overlaps)
outline_items_by_table, remaining = defaultdict(list), []
for item in candidates:
item_bbox = BBox.from_layout_item(item)
overlaps = [
table for table in tables
if item_bbox.overlaps(table.bbox.padded(margin))
]
if not overlaps:
remaining.append(item)
elif len(overlaps) == 1:
table = overlaps[0]
outline_items_by_table[table].append(item)
# Apply extensions (TODO: avoid if this increases overlaps)
for table, outline_items in outline_items_by_table.items():
outline_bbox = BBox.from_layout_items(outline_items)
bbox_with_outline = table.bbox.union(outline_bbox)
relative_area = bbox_with_outline.area / table.bbox.area
if relative_area > 1.0:
if relative_area > 1.5:
# Maybe a bit much?
logger.warning(f'extended table to {relative_area:.1%} '
f'on page {page}')
table.bbox = bbox_with_outline
extended = True
if not extended:
break
candidates = remaining
return annotations
CAPTION_STRING_RE = re.compile(r'''
^\s*
(
Figure|FIGURE|
Fig\.|FIG\.|
Chart|CHART|
Kaava|KAAVA|
Picture|PICTURE|
Table|TABLE|
Kuva|KUVA|
Kaavio|KAAVIO|
Kuvio|KUVIO|
Taulukko|TAULUKKO|
Bild|BILD|
Figur|FIGUR|
Tabell|TABELL
)
\s+\d+
''', re.VERBOSE)
def is_caption_text(string):
m = CAPTION_STRING_RE.search(string)
if m:
logger.info(f'marking as Caption: "{string}" ("{m.group(1)}")')
return True
else:
return False
def assign_caption_labels(page, annotations):
for a in annotations:
if a.type not in STRUCT_TYPES_WITH_CAPTIONS:
continue
candidates = [
c for c in annotations
if c is not a and c.bbox.horizontally_overlaps(a.bbox)
]
above = [c for c in candidates if c.bbox.is_above(a.bbox)]
below = [c for c in candidates if c.bbox.is_below(a.bbox)]
above.sort(key=lambda c: c.bbox.vertical_distance(a.bbox))
below.sort(key=lambda c: c.bbox.vertical_distance(a.bbox))
if above and is_caption_text(above[0].text_content()):
above[0].type = CAPTION_STRUCT_TYPE
elif below and is_caption_text(below[0].text_content()):
below[0].type = CAPTION_STRUCT_TYPE
return annotations
def get_annotations(page, tagged_pdf, args):
annotations = [
annotation for node in tagged_pdf.struct_tree_root.children
for annotation in _get_annotations(page, node, args)
]
if not args.no_captions:
annotations = assign_caption_labels(page, annotations)
if not args.no_text_bbox_trim:
trimmed_annotations = []
for a in annotations:
if a.type in TRIMMABLE_STRUCT_TYPES:
a.trim_bbox()
if a.bbox is not None:
trimmed_annotations.append(a)
annotations = trimmed_annotations
if not args.no_crop:
cropbox = tagged_pdf.get_cropbox(page)
cropped_annotations = []
for a in annotations:
a.crop(cropbox)
if a.bbox is not None:
cropped_annotations.append(a)
annotations = cropped_annotations
if not args.keep_overlaps:
annotations = eliminate_overlaps(page, annotations)
if not args.no_table_bbox_extension:
nonmarked = tagged_pdf.nonmarked_by_page[page]
annotations = extend_table_bboxes(page, annotations, nonmarked)
return annotations
def can_annotate(pdf, fn):
# check that we have a tagged PDF
if pdf.struct_tree_root is None:
logger.warning(f'cannot annotate {fn}: no StructTreeRoot')
return False
elif pdf.mark_info is None:
logger.warning('cannot annotate {fn}: no MarkInfo')
return False
elif not pdf.mark_info.marked:
logger.warning('cannot annotate {fn}: not marked')
return False
elif pdf.struct_tree_root.parent_tree is None:
logger.warning('cannot annotate {fn}: no parent tree')
return False
else:
return True
def annotate_to_pdf(infn, outfn, tagged_pdf, args):
# write marked PDF to output
in_pdf = PdfFileReader(open(infn, 'rb'))
out_pdf = PdfFileWriter()
for page_idx, page in enumerate(in_pdf.pages):
print(f'processing page {page_idx} ...', file=sys.stderr, flush=True)
annotations = get_annotations(page_idx, tagged_pdf, args)
if annotations:
ann_pdf = render_annotations(annotations)
ann_page = ann_pdf.getPage(0)
page.mergePage(ann_page)
out_pdf.addPage(page)
with open(outfn, 'wb') as output:
out_pdf.write(output)
def annotate_to_xml(infn, outfn, tagged_pdf, args):
with open(outfn, 'w') as out:
print(f'<document>', file=out)
for page_idx in range(tagged_pdf.page_count):
print(f' <page index="{page_idx}">', file=out)
for a in get_annotations(page_idx, tagged_pdf, args):
for s in a.xml_string(args.include_content).splitlines():
print(f' {s}', file=out)
print(f' </page>', file=out)
print(f'</document>', file=out)
def annotate_to_pawls(infn, outfn, tagged_pdf, args):
if args.pawls_structure is None:
print(
'Please provide the path to the pdf_structure.json generated'
'by `pawls preprocess pdfplumber` with --pawls-structure',
file=sys.stderr
)
return None
pawls_document = load_pawls_structure(args.pawls_structure)
if len(pawls_document.pages) != tagged_pdf.page_count:
raise ValueError('page count mismatch')
pawls_annotations = []
for page_idx in range(tagged_pdf.page_count):
pawls_page = pawls_document.pages[page_idx]
annotations = get_annotations(page_idx, tagged_pdf, args)
# Find annotation-token overlaps and assign tokens to the
# annotation with which they have maximal relative overlap.
tokens_by_annotation = defaultdict(list)
for token in pawls_page.tokens:
overlaps = []
for a in annotations:
overlap = token.bbox.relative_overlap(a.bbox)
if overlap:
overlaps.append((overlap, a))
if overlaps:
a = sorted(overlaps, reverse=True)[0][1]
a.tokens.append(token)
for a in annotations:
# PAWLS applies some padding (see preannotate.py), match
a.bbox = a.bbox.padded(3) # TODO parameterize
pawls_annotations.append(a.pawls_dict(pawls_page.height))
with open(outfn, 'w') as out:
data = { 'annotations': pawls_annotations, 'relations': [] }
json.dump(data, out)
def annotate_to_coco(infn, outfn, tagged_pdf, args):
paper_id = 0
coco = {
'annotations': [],
'categories': COCO_CATEGORIES,
'images': [],
'papers': [
{
'id': paper_id,
'pages': tagged_pdf.page_count,
'paper_sha': file_sha256(infn),
}
],
}
next_ann_id = 0
for page_idx in range(tagged_pdf.page_count):
page_id = page_idx
page_size = tagged_pdf.get_mediabox(page_idx)
coco['images'].append({
'id': page_id,
'page_number': page_idx,
'paper_id': paper_id,
'width': page_size.width,
'height': page_size.height,
})
for a in get_annotations(page_idx, tagged_pdf, args):
coco['annotations'].append({
'id': next_ann_id,
'image_id': page_idx,
'category_id': a.coco_category_id(),
'bbox': a.bbox.padded(3).to_coco(page_size.height),
'area': a.bbox.area,
'iscrowd': False,
})
next_ann_id += 1
with open(outfn, 'w') as out:
json.dump(coco, out, indent=2)
def annotate(infn, outfn, args):
tagged_pdf = TaggedPdf(infn)
if not can_annotate(tagged_pdf, infn):
return
if args.format == 'pdf':
return annotate_to_pdf(infn, outfn, tagged_pdf, args)
elif args.format == 'xml':
return annotate_to_xml(infn, outfn, tagged_pdf, args)
elif args.format == 'pawls':
return annotate_to_pawls(infn, outfn, tagged_pdf, args)
elif args.format == 'coco':
return annotate_to_coco(infn, outfn, tagged_pdf, args)
else:
raise NotImplementedError(f'format {args.format}')
def main(argv):
args = argparser().parse_args(argv[1:])
try:
annotate(args.input, args.output, args)
except Exception as e:
print(f'{args.input}: ERROR: {e}')
raise
if __name__ == '__main__':
sys.exit(main(sys.argv))