| 502 | return x, y, w, h |
| 503 | |
| 504 | def check_ocr_box(image_source: Union[str, Image.Image], display_img = True, output_bb_format='xywh', goal_filtering=None, easyocr_args=None, use_paddleocr=False): |
| 505 | if isinstance(image_source, str): |
| 506 | image_source = Image.open(image_source) |
| 507 | if image_source.mode == 'RGBA': |
| 508 | # Convert RGBA to RGB to avoid alpha channel issues |
| 509 | image_source = image_source.convert('RGB') |
| 510 | image_np = np.array(image_source) |
| 511 | w, h = image_source.size |
| 512 | if use_paddleocr: |
| 513 | if easyocr_args is None: |
| 514 | text_threshold = 0.5 |
| 515 | else: |
| 516 | text_threshold = easyocr_args['text_threshold'] |
| 517 | result = paddle_ocr.ocr(image_np, cls=False)[0] |
| 518 | coord = [item[0] for item in result if item[1][1] > text_threshold] |
| 519 | text = [item[1][0] for item in result if item[1][1] > text_threshold] |
| 520 | else: # EasyOCR |
| 521 | if easyocr_args is None: |
| 522 | easyocr_args = {} |
| 523 | result = reader.readtext(image_np, **easyocr_args) |
| 524 | coord = [item[0] for item in result] |
| 525 | text = [item[1] for item in result] |
| 526 | if display_img: |
| 527 | opencv_img = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) |
| 528 | bb = [] |
| 529 | for item in coord: |
| 530 | x, y, a, b = get_xywh(item) |
| 531 | bb.append((x, y, a, b)) |
| 532 | cv2.rectangle(opencv_img, (x, y), (x+a, y+b), (0, 255, 0), 2) |
| 533 | # matplotlib expects RGB |
| 534 | plt.imshow(cv2.cvtColor(opencv_img, cv2.COLOR_BGR2RGB)) |
| 535 | else: |
| 536 | if output_bb_format == 'xywh': |
| 537 | bb = [get_xywh(item) for item in coord] |
| 538 | elif output_bb_format == 'xyxy': |
| 539 | bb = [get_xyxy(item) for item in coord] |
| 540 | return (text, bb), goal_filtering |
| 541 | |
| 542 | |