@@ -50,16 +50,21 @@ def __call__(
5050 self ,
5151 img : InputType ,
5252 ocr_result : Optional [List [Union [List [List [float ]], str , str ]]] = None ,
53+ ** kwargs ,
5354 ) -> Tuple [str , float , Any , Any , Any ]:
5455 if self .ocr is None and ocr_result is None :
5556 raise ValueError (
5657 "One of two conditions must be met: ocr_result is not empty, or rapidocr_onnxruntime is installed."
5758 )
5859
5960 s = time .perf_counter ()
60-
61+ rec_again = True
62+ need_ocr = True
63+ if kwargs :
64+ rec_again = kwargs .get ("rec_again" , True )
65+ need_ocr = kwargs .get ("need_ocr" , True )
6166 img = self .load_img (img )
62- polygons = self .table_line_rec (img )
67+ polygons = self .table_line_rec (img , ** kwargs )
6368 if polygons is None :
6469 logging .warning ("polygons is None." )
6570 return "" , 0.0 , None , None , None
@@ -71,12 +76,22 @@ def __call__(
7176 polygons [:, 3 , :].copy (),
7277 polygons [:, 1 , :].copy (),
7378 )
74- if ocr_result is None :
79+ if not need_ocr :
80+ sorted_polygons , idx_list = sorted_ocr_boxes (
81+ [box_4_2_poly_to_box_4_1 (box ) for box in polygons ]
82+ )
83+ return (
84+ "" ,
85+ time .perf_counter () - s ,
86+ sorted_polygons ,
87+ logi_points [idx_list ],
88+ [],
89+ )
90+ if ocr_result is None and need_ocr :
7591 ocr_result , _ = self .ocr (img )
7692 cell_box_det_map , not_match_orc_boxes = match_ocr_cell (ocr_result , polygons )
7793 # 如果有识别框没有ocr结果,直接进行rec补充
78- # cell_box_det_map = self.re_rec_high_precise(img, polygons, cell_box_det_map)
79- cell_box_det_map = self .re_rec (img , polygons , cell_box_det_map )
94+ cell_box_det_map = self .re_rec (img , polygons , cell_box_det_map , rec_again )
8095 # 转换为中间格式,修正识别框坐标,将物理识别框,逻辑识别框,ocr识别框整合为dict,方便后续处理
8196 t_rec_ocr_list = self .transform_res (cell_box_det_map , polygons , logi_points )
8297 # 将每个单元格中的ocr识别结果排序和同行合并,输出的html能完整保留文字的换行格式
@@ -139,11 +154,11 @@ def transform_res(
139154 def sort_and_gather_ocr_res (self , res ):
140155 for i , dict_res in enumerate (res ):
141156 _ , sorted_idx = sorted_ocr_boxes (
142- [ocr_det [0 ] for ocr_det in dict_res ["t_ocr_res" ]], threhold = 0.5
157+ [ocr_det [0 ] for ocr_det in dict_res ["t_ocr_res" ]], threhold = 0.3
143158 )
144159 dict_res ["t_ocr_res" ] = [dict_res ["t_ocr_res" ][i ] for i in sorted_idx ]
145160 dict_res ["t_ocr_res" ] = gather_ocr_list_by_row (
146- dict_res ["t_ocr_res" ], threhold = 0.5
161+ dict_res ["t_ocr_res" ], threhold = 0.3
147162 )
148163 return res
149164
@@ -152,12 +167,16 @@ def re_rec(
152167 img : np .ndarray ,
153168 sorted_polygons : np .ndarray ,
154169 cell_box_map : Dict [int , List [str ]],
170+ rec_again = True ,
155171 ) -> Dict [int , List [Any ]]:
156172 """找到poly对应为空的框,尝试将直接将poly框直接送到识别中"""
157- #
158173 for i in range (sorted_polygons .shape [0 ]):
159174 if cell_box_map .get (i ):
160175 continue
176+ if not rec_again :
177+ box = sorted_polygons [i ]
178+ cell_box_map [i ] = [[box , "" , 1 ]]
179+ continue
161180 crop_img = get_rotate_crop_image (img , sorted_polygons [i ])
162181 pad_img = cv2 .copyMakeBorder (
163182 crop_img , 5 , 5 , 100 , 100 , cv2 .BORDER_CONSTANT , value = (255 , 255 , 255 )
0 commit comments