Create detect_layout.py
Browse files- detect_layout.py +34 -0
detect_layout.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
|
3 |
+
from surya.detection import batch_text_detection
|
4 |
+
from surya.input.load import load_from_folder, load_from_file
|
5 |
+
from surya.layout import batch_layout_detection
|
6 |
+
from surya.model.detection.model import load_model, load_processor
|
7 |
+
from surya.postprocessing.heatmap import draw_polys_on_image
|
8 |
+
from surya.settings import settings
|
9 |
+
import os
|
10 |
+
|
11 |
+
def main(input_path, max_pages=None):
|
12 |
+
|
13 |
+
model = load_model(checkpoint=settings.LAYOUT_MODEL_CHECKPOINT)
|
14 |
+
processor = load_processor(checkpoint=settings.LAYOUT_MODEL_CHECKPOINT)
|
15 |
+
det_model = load_model()
|
16 |
+
det_processor = load_processor()
|
17 |
+
|
18 |
+
if os.path.isdir(input_path):
|
19 |
+
images, names = load_from_folder(input_path, max_pages)
|
20 |
+
|
21 |
+
else:
|
22 |
+
images, names = load_from_file(input_path, max_pages)
|
23 |
+
|
24 |
+
|
25 |
+
line_predictions = batch_text_detection(images, det_model, det_processor)
|
26 |
+
|
27 |
+
layout_predictions = batch_layout_detection(images, model, processor, line_predictions)
|
28 |
+
|
29 |
+
|
30 |
+
for idx, (image, layout_pred, name) in enumerate(zip(images, layout_predictions, names)):
|
31 |
+
polygons = [p.polygon for p in layout_pred.bboxes]
|
32 |
+
labels = [p.label for p in layout_pred.bboxes]
|
33 |
+
bbox_image = draw_polys_on_image(polygons, copy.deepcopy(image), labels=labels)
|
34 |
+
return bbox_image
|