创建适用于Frame的OCR函数,用于集成到VerifierOptions中 Args: processor: 高级OCR处理器实例 Returns: callable: 接受Frame参数的OCR函数
(processor: AdvancedOCRProcessor)
| 539 | |
| 540 | |
| 541 | def create_frame_ocr_function(processor: AdvancedOCRProcessor) -> Callable: |
| 542 | """ |
| 543 | 创建适用于Frame的OCR函数,用于集成到VerifierOptions中 |
| 544 | |
| 545 | Args: |
| 546 | processor: 高级OCR处理器实例 |
| 547 | |
| 548 | Returns: |
| 549 | callable: 接受Frame参数的OCR函数 |
| 550 | """ |
| 551 | def frame_ocr(frame: Dict[str, Any]) -> Optional[str]: |
| 552 | """ |
| 553 | 从Frame中提取并识别图像文字 |
| 554 | |
| 555 | Args: |
| 556 | frame: 包含图像路径的Frame字典 |
| 557 | |
| 558 | Returns: |
| 559 | str: 识别的文字,失败时返回XML文本或None |
| 560 | """ |
| 561 | # 获取图像路径 |
| 562 | image_path = frame.get("image") |
| 563 | if not image_path or not os.path.exists(image_path): |
| 564 | # 退化到改进的XML提取 |
| 565 | xml_text = frame.get("xml_text", "") |
| 566 | if xml_text: |
| 567 | xml_processed = extract_text_from_xml(xml_text) |
| 568 | if xml_processed.cleaned: |
| 569 | frame['_xml_processed'] = xml_processed |
| 570 | logger.info(f"图像不可用,使用改进XML提取: {xml_processed.cleaned[:100]}...") |
| 571 | logger.debug(f"XML提取词语数: {len(xml_processed.words)}") |
| 572 | return f"{xml_processed.cleaned} {xml_processed.no_spaces} {' '.join(xml_processed.words)}" |
| 573 | else: |
| 574 | logger.warning("图像不可用且XML提取失败") |
| 575 | return xml_text[:200] if xml_text else None |
| 576 | return None |
| 577 | |
| 578 | # 使用OCR识别 |
| 579 | ocr_text, backup_text = processor.extract_text_from_image(image_path) |
| 580 | xml_text = frame.get("xml_text", "") |
| 581 | merged_parts: List[str] = [] |
| 582 | |
| 583 | if ocr_text: |
| 584 | # 处理OCR文本 |
| 585 | processed = processor.process_text(ocr_text) |
| 586 | frame['_ocr_processed'] = processed |
| 587 | merged_parts.extend([processed.cleaned, processed.no_spaces] + processed.words) |
| 588 | logger.debug(f"识别图像 {os.path.basename(image_path)} -> 词语数: {len(processed.words)}") |
| 589 | |
| 590 | # 融合XML文本 |
| 591 | if xml_text: |
| 592 | xml_processed = extract_text_from_xml(xml_text) |
| 593 | if xml_processed.cleaned: |
| 594 | frame['_xml_processed'] = xml_processed |
| 595 | merged_parts.extend([xml_processed.cleaned, xml_processed.no_spaces] + xml_processed.words) |
| 596 | logger.debug(f"融合XML文本 -> 词语数: {len(xml_processed.words)}") |
| 597 | |
| 598 | if merged_parts: |
no outgoing calls
no test coverage detected