Create reading_order.py
Browse files- reading_order.py +45 -0
reading_order.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import copy
|
3 |
+
|
4 |
+
from surya.detection import batch_text_detection
|
5 |
+
from surya.input.load import load_from_folder, load_from_file
|
6 |
+
from surya.layout import batch_layout_detection
|
7 |
+
from surya.model.detection.model import load_model as load_det_model, load_processor as load_det_processor
|
8 |
+
from surya.model.ordering.model import load_model
|
9 |
+
from surya.model.ordering.processor import load_processor
|
10 |
+
from surya.ordering import batch_ordering
|
11 |
+
from surya.postprocessing.heatmap import draw_polys_on_image
|
12 |
+
from surya.settings import settings
|
13 |
+
|
14 |
+
|
15 |
+
def main(input_path, max_pages=None):
|
16 |
+
model = load_model()
|
17 |
+
processor = load_processor()
|
18 |
+
|
19 |
+
layout_model = load_det_model(checkpoint=settings.LAYOUT_MODEL_CHECKPOINT)
|
20 |
+
layout_processor = load_det_processor(checkpoint=settings.LAYOUT_MODEL_CHECKPOINT)
|
21 |
+
|
22 |
+
det_model = load_det_model()
|
23 |
+
det_processor = load_det_processor()
|
24 |
+
|
25 |
+
if os.path.isdir(input_path):
|
26 |
+
images, names = load_from_folder(input_path, max_pages)
|
27 |
+
|
28 |
+
else:
|
29 |
+
images, names = load_from_file(input_path, max_pages)
|
30 |
+
|
31 |
+
|
32 |
+
line_predictions = batch_text_detection(images, det_model, det_processor)
|
33 |
+
layout_predictions = batch_layout_detection(images, layout_model, layout_processor, line_predictions)
|
34 |
+
bboxes = []
|
35 |
+
for layout_pred in layout_predictions:
|
36 |
+
bbox = [l.bbox for l in layout_pred.bboxes]
|
37 |
+
bboxes.append(bbox)
|
38 |
+
|
39 |
+
order_predictions = batch_ordering(images, bboxes, model, processor)
|
40 |
+
|
41 |
+
for idx, (image, layout_pred, order_pred, name) in enumerate(zip(images, layout_predictions, order_predictions, names)):
|
42 |
+
polys = [l.polygon for l in order_pred.bboxes]
|
43 |
+
labels = [str(l.position) for l in order_pred.bboxes]
|
44 |
+
bbox_image = draw_polys_on_image(polys, copy.deepcopy(image), labels=labels, label_font_size=20)
|
45 |
+
return bbox_image
|