16
16
from typing import Optional , Union , Tuple , Iterator
17
17
import numpy as np
18
18
import re
19
+ import copy
19
20
20
21
from ....utils import logging
21
22
from ...common .batch_sampler import ImageBatchSampler
25
26
from ..base import BasePipeline
26
27
from ..ocr .result import OCRResult
27
28
from .result_v2 import LayoutParsingResultV2
28
- from .utils import get_single_block_parsing_res
29
- from .utils import get_sub_regions_ocr_res
29
+ from .utils import get_single_block_parsing_res , get_sub_regions_ocr_res , gather_imgs
30
30
31
31
32
32
class LayoutParsingPipelineV2 (BasePipeline ):
@@ -227,6 +227,7 @@ def get_layout_parsing_res(
227
227
table_res_list : list ,
228
228
seal_res_list : list ,
229
229
formula_res_list : list ,
230
+ imgs_in_doc : list ,
230
231
text_det_limit_side_len : Optional [int ] = None ,
231
232
text_det_limit_type : Optional [str ] = None ,
232
233
text_det_thresh : Optional [float ] = None ,
@@ -309,14 +310,17 @@ def get_layout_parsing_res(
309
310
del overall_ocr_res ["rec_polys" ][matched_idx ]
310
311
del overall_ocr_res ["rec_scores" ][matched_idx ]
311
312
312
- if sub_ocr_res ["rec_boxes" ] != []:
313
+ if sub_ocr_res ["rec_boxes" ].size > 0 :
314
+ sub_ocr_res ["rec_labels" ] = ["text" ] * len (sub_ocr_res ["rec_texts" ])
315
+
313
316
overall_ocr_res ["dt_polys" ].extend (sub_ocr_res ["dt_polys" ])
314
317
overall_ocr_res ["rec_texts" ].extend (sub_ocr_res ["rec_texts" ])
315
318
overall_ocr_res ["rec_boxes" ] = np .concatenate (
316
319
[overall_ocr_res ["rec_boxes" ], sub_ocr_res ["rec_boxes" ]], axis = 0
317
320
)
318
321
overall_ocr_res ["rec_polys" ].extend (sub_ocr_res ["rec_polys" ])
319
322
overall_ocr_res ["rec_scores" ].extend (sub_ocr_res ["rec_scores" ])
323
+ overall_ocr_res ["rec_labels" ].extend (sub_ocr_res ["rec_labels" ])
320
324
321
325
for formula_res in formula_res_list :
322
326
x_min , y_min , x_max , y_max = list (map (int , formula_res ["dt_polys" ]))
@@ -331,14 +335,17 @@ def get_layout_parsing_res(
331
335
overall_ocr_res ["rec_boxes" ] = np .vstack (
332
336
(overall_ocr_res ["rec_boxes" ], [formula_res ["dt_polys" ]])
333
337
)
338
+ overall_ocr_res ["rec_labels" ].append ("formula" )
334
339
overall_ocr_res ["rec_polys" ].append (poly_points )
335
340
overall_ocr_res ["rec_scores" ].append (1 )
336
341
337
342
parsing_res_list = get_single_block_parsing_res (
343
+ self .general_ocr_pipeline ,
338
344
overall_ocr_res = overall_ocr_res ,
339
345
layout_det_res = layout_det_res ,
340
346
table_res_list = table_res_list ,
341
347
seal_res_list = seal_res_list ,
348
+ imgs_in_doc = imgs_in_doc ,
342
349
)
343
350
344
351
return parsing_res_list
@@ -472,7 +479,7 @@ def predict(
472
479
if not self .check_model_settings_valid (model_settings ):
473
480
yield {"error" : "the input params for model settings are invalid!" }
474
481
475
- for img_id , batch_data in enumerate ( self .batch_sampler (input ) ):
482
+ for batch_data in self .batch_sampler (input ):
476
483
image_array = self .img_reader (batch_data .instances )[0 ]
477
484
478
485
if model_settings ["use_doc_preprocessor" ]:
@@ -497,6 +504,7 @@ def predict(
497
504
layout_merge_bboxes_mode = layout_merge_bboxes_mode ,
498
505
)
499
506
)
507
+ imgs_in_doc = gather_imgs (doc_preprocessor_image , layout_det_res ["boxes" ])
500
508
501
509
if model_settings ["use_formula_recognition" ]:
502
510
formula_res_all = next (
@@ -535,15 +543,55 @@ def predict(
535
543
else :
536
544
overall_ocr_res = {}
537
545
546
+ overall_ocr_res ["rec_labels" ] = ["text" ] * len (overall_ocr_res ["rec_texts" ])
547
+
538
548
if model_settings ["use_table_recognition" ]:
549
+ table_contents = copy .deepcopy (overall_ocr_res )
550
+ for formula_res in formula_res_list :
551
+ x_min , y_min , x_max , y_max = list (map (int , formula_res ["dt_polys" ]))
552
+ poly_points = [
553
+ (x_min , y_min ),
554
+ (x_max , y_min ),
555
+ (x_max , y_max ),
556
+ (x_min , y_max ),
557
+ ]
558
+ table_contents ["dt_polys" ].append (poly_points )
559
+ table_contents ["rec_texts" ].append (
560
+ f"${ formula_res ['rec_formula' ]} $"
561
+ )
562
+ table_contents ["rec_boxes" ] = np .vstack (
563
+ (table_contents ["rec_boxes" ], [formula_res ["dt_polys" ]])
564
+ )
565
+ table_contents ["rec_polys" ].append (poly_points )
566
+ table_contents ["rec_scores" ].append (1 )
567
+
568
+ for img in imgs_in_doc :
569
+ img_path = img ["path" ]
570
+ x_min , y_min , x_max , y_max = img ["coordinate" ]
571
+ poly_points = [
572
+ (x_min , y_min ),
573
+ (x_max , y_min ),
574
+ (x_max , y_max ),
575
+ (x_min , y_max ),
576
+ ]
577
+ table_contents ["dt_polys" ].append (poly_points )
578
+ table_contents ["rec_texts" ].append (
579
+ f'<div style="text-align: center;"><img src="{ img_path } " alt="Image" /></div>'
580
+ )
581
+ table_contents ["rec_boxes" ] = np .vstack (
582
+ (table_contents ["rec_boxes" ], img ["coordinate" ])
583
+ )
584
+ table_contents ["rec_polys" ].append (poly_points )
585
+ table_contents ["rec_scores" ].append (img ["score" ])
586
+
539
587
table_res_all = next (
540
588
self .table_recognition_pipeline (
541
589
doc_preprocessor_image ,
542
590
use_doc_orientation_classify = False ,
543
591
use_doc_unwarping = False ,
544
592
use_layout_detection = False ,
545
593
use_ocr_model = False ,
546
- overall_ocr_res = overall_ocr_res ,
594
+ overall_ocr_res = table_contents ,
547
595
layout_det_res = layout_det_res ,
548
596
cell_sort_by_y_projection = True ,
549
597
),
@@ -579,6 +627,7 @@ def predict(
579
627
table_res_list = table_res_list ,
580
628
seal_res_list = seal_res_list ,
581
629
formula_res_list = formula_res_list ,
630
+ imgs_in_doc = imgs_in_doc ,
582
631
text_det_limit_side_len = text_det_limit_side_len ,
583
632
text_det_limit_type = text_det_limit_type ,
584
633
text_det_thresh = text_det_thresh ,
@@ -603,6 +652,7 @@ def predict(
603
652
"seal_res_list" : seal_res_list ,
604
653
"formula_res_list" : formula_res_list ,
605
654
"parsing_res_list" : parsing_res_list ,
655
+ "imgs_in_doc" : imgs_in_doc ,
606
656
"model_settings" : model_settings ,
607
657
}
608
658
yield LayoutParsingResultV2 (single_img_res )
0 commit comments