1
+ import cv2
1
2
import numpy as np
2
3
3
4
from ....data_process .utils import cv_utils
@@ -10,19 +11,44 @@ def __init__(self, args, msg_queue):
10
11
super (DetPostNode , self ).__init__ (args , msg_queue )
11
12
self .text_detector = None
12
13
self .task_type = self .args .task_type
14
+ self .is_concat = self .args .is_concat
13
15
14
16
def init_self_args (self ):
15
17
self .text_detector = TextDetector (self .args )
16
18
self .text_detector .init (preprocess = False , model = False , postprocess = True )
17
19
super ().init_self_args ()
18
20
21
+ def concat_crops (self , crops : list ):
22
+ """
23
+ Concatenates the list of cropped images horizontally after resizing them to have the same height.
24
+
25
+ Args:
26
+ crops (list): A list of cropped images represented as numpy arrays.
27
+
28
+ Returns:
29
+ numpy.ndarray: A horizontally concatenated image array.
30
+ """
31
+ max_height = max (crop .shape [0 ] for crop in crops )
32
+ resized_crops = []
33
+ for crop in crops :
34
+ h , w , c = crop .shape
35
+ new_h = max_height
36
+ new_w = int ((w / h ) * new_h )
37
+
38
+ resized_img = cv2 .resize (crop , (new_w , new_h ), interpolation = cv2 .INTER_LINEAR )
39
+ resized_crops .append (resized_img )
40
+ crops_concated = np .concatenate (resized_crops , axis = 1 )
41
+ return crops_concated
42
+
19
43
def process (self , input_data ):
20
44
if input_data .skip :
21
45
self .send_to_next_module (input_data )
22
46
return
23
47
24
48
data = input_data .data
25
49
boxes = self .text_detector .postprocess (data ["pred" ], data ["shape_list" ])
50
+ if self .is_concat :
51
+ boxes = sorted (boxes , key = lambda points : (points [0 ][1 ], points [0 ][0 ]))
26
52
27
53
infer_res_list = []
28
54
for box in boxes :
@@ -39,6 +65,8 @@ def process(self, input_data):
39
65
for box in infer_res_list :
40
66
sub_image = cv_utils .crop_box_from_image (image , np .array (box ))
41
67
sub_image_list .append (sub_image )
68
+ if self .is_concat :
69
+ sub_image_list = len (sub_image_list ) * [self .concat_crops (sub_image_list )]
42
70
input_data .sub_image_list = sub_image_list
43
71
44
72
input_data .data = None
0 commit comments