Process a single text line region for text enhancement. Args: img: Input text image
(self, img)
| 206 | return img, orig_texts, enhanced_texts, debug_texts, pred_texts #, preds_locs_txt |
| 207 | |
| 208 | def _process_text_line(self, img): |
| 209 | """ |
| 210 | Process a single text line region for text enhancement. |
| 211 | |
| 212 | Args: |
| 213 | img: Input text image |
| 214 | |
| 215 | """ |
| 216 | |
| 217 | |
| 218 | height, width = img.shape[:2] |
| 219 | if height > width: |
| 220 | print(' ' * 25 + ' ... Can not handle vertical text temporarily') |
| 221 | return (None,) * 5 |
| 222 | |
| 223 | w_norm = int(self.insize * width / height) // 4 * 4 |
| 224 | h_norm = self.insize |
| 225 | |
| 226 | img = cv2.resize(img, (w_norm*4, h_norm*4), interpolation=cv2.INTER_LINEAR) |
| 227 | in_img = cv2.resize(img, (w_norm, h_norm), interpolation=cv2.INTER_LINEAR) |
| 228 | ShowLQ = img[:,:,::-1] |
| 229 | |
| 230 | LQ_HeightNorm = transforms.ToTensor()(in_img) |
| 231 | LQ_HeightNorm = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(LQ_HeightNorm).unsqueeze(0).to(self.device) |
| 232 | |
| 233 | |
| 234 | ''' |
| 235 | Step 1: Predicting the character labels, bounding boxes. |
| 236 | ''' |
| 237 | |
| 238 | recognized_boxes, pred_text, char_x_centers = get_yolo_ocr_xloc( |
| 239 | img, # input image, RGB 0~255 |
| 240 | yolo_model=self.yolo_character, # YOLO model instance for character detection |
| 241 | ocr_pipeline=self.modelscope_ocr_recognition, # OCR pipeline/model for character recognition |
| 242 | num_cropped_boxes=5, # Number of adjacent character boxes to include in each cropped segment (window size) |
| 243 | expand_px=1, # Number of pixels to expand each crop region on all sides (except first/last) |
| 244 | expand_px_for_first_last_cha=12, # Number of pixels to expand the crop region for the first and last character (left/right respectively) |
| 245 | yolo_iou=0.1, # IOU threshold for YOLO non-max suppression (NMS) |
| 246 | yolo_conf=0.07 # Confidence threshold for YOLO detection |
| 247 | ) |
| 248 | |
| 249 | print('{:>25s} ... Recognized chars: {}'.format(' ', ''.join(pred_text))) |
| 250 | loc_sr = torch.tensor(char_x_centers, device=self.device).unsqueeze(0) |
| 251 | |
| 252 | |
| 253 | # show character location |
| 254 | pad = 1 |
| 255 | ShowPredLoc = ShowLQ.copy() |
| 256 | for l in range(len(pred_text)): |
| 257 | center_pred_w = int(loc_sr[0][l].item()) |
| 258 | if center_pred_w > 0: |
| 259 | ShowPredLoc[:, max(0, center_pred_w-pad):min(center_pred_w+pad, ShowPredLoc.shape[1]), :] = 0 |
| 260 | ShowPredLoc[:, max(0, center_pred_w-pad):min(center_pred_w+pad, ShowPredLoc.shape[1]), 1] = 255 |
| 261 | |
| 262 | |
| 263 | ''' |
| 264 | Step 2: Character Prior Generation |
| 265 | ''' |
no test coverage detected