|
1 | | -from rapidocr.ch_ppocr_det import TextDetOutput |
| 1 | +from typing import List, Dict, Any |
2 | 2 |
|
3 | 3 | from rapid_doc.model.ocr.ocr_patch import apply_ocr_patch |
4 | 4 |
|
@@ -157,13 +157,62 @@ def ocr(self, |
157 | 157 | op_record = {'padding_1': {'left': 0, 'top': 0}, 'preprocess': {'ratio_h': 1.0, 'ratio_w': 1.0}} |
158 | 158 | raw_h, raw_w = ori_img.shape[:2] |
159 | 159 | dt_boxes_np = [np.array(box, dtype=np.float32) for box in dt_boxes] |
160 | | - word_results = self.ocr_engine.calc_word_boxes(img, TextDetOutput(boxes=dt_boxes_np), rec_result, op_record, raw_h, raw_w) |
| 160 | + word_results = self.calc_word_boxes(img, dt_boxes_np, rec_result, op_record, raw_h, raw_w) |
161 | 161 | rec_res = list(zip(rec_result.txts, rec_result.scores, word_results)) |
162 | 162 | else: |
163 | 163 | rec_res = list(zip(rec_result.txts, rec_result.scores)) |
164 | 164 | ocr_res.append(rec_res) |
165 | 165 | return ocr_res |
166 | 166 |
|
| 167 | + def calc_word_boxes( |
| 168 | + self, |
| 169 | + img: List[np.ndarray], |
| 170 | + dt_boxes: np.ndarray, |
| 171 | + rec_res: TextRecOutput, |
| 172 | + op_record: Dict[str, Any], |
| 173 | + raw_h: int, |
| 174 | + raw_w: int, |
| 175 | + ) -> Any: |
| 176 | + rec_res = self.ocr_engine.cal_rec_boxes( |
| 177 | + img, dt_boxes, rec_res, self.ocr_engine.return_single_char_box |
| 178 | + ) |
| 179 | + |
| 180 | + origin_words = [] |
| 181 | + for word_line in rec_res.word_results: |
| 182 | + origin_words_item = [] |
| 183 | + for txt, score, bbox in word_line: |
| 184 | + if bbox is None: |
| 185 | + continue |
| 186 | + |
| 187 | + origin_words_points = self.map_boxes_to_original( |
| 188 | + np.array([bbox]).astype(np.float64), op_record, raw_h, raw_w |
| 189 | + ) |
| 190 | + origin_words_points = origin_words_points.astype(np.int32).tolist()[0] |
| 191 | + origin_words_item.append((txt, score, origin_words_points)) |
| 192 | + |
| 193 | + if origin_words_item: |
| 194 | + origin_words.append(tuple(origin_words_item)) |
| 195 | + return tuple(origin_words) |
| 196 | + |
| 197 | + def map_boxes_to_original( |
| 198 | + self, dt_boxes: np.ndarray, op_record: Dict[str, Any], ori_h: int, ori_w: int |
| 199 | + ) -> np.ndarray: |
| 200 | + for op in reversed(list(op_record.keys())): |
| 201 | + v = op_record[op] |
| 202 | + if "padding" in op: |
| 203 | + top, left = v.get("top"), v.get("left") |
| 204 | + dt_boxes[:, :, 0] -= left |
| 205 | + dt_boxes[:, :, 1] -= top |
| 206 | + elif "preprocess" in op: |
| 207 | + ratio_h = v.get("ratio_h") |
| 208 | + ratio_w = v.get("ratio_w") |
| 209 | + dt_boxes[:, :, 0] *= ratio_w |
| 210 | + dt_boxes[:, :, 1] *= ratio_h |
| 211 | + |
| 212 | + dt_boxes = np.where(dt_boxes < 0, 0, dt_boxes) |
| 213 | + dt_boxes[..., 0] = np.where(dt_boxes[..., 0] > ori_w, ori_w, dt_boxes[..., 0]) |
| 214 | + dt_boxes[..., 1] = np.where(dt_boxes[..., 1] > ori_h, ori_h, dt_boxes[..., 1]) |
| 215 | + return dt_boxes |
167 | 216 |
|
168 | 217 | def __call__(self, img, mfd_res=None): |
169 | 218 |
|
|
0 commit comments