refactor: 升级OCR依赖与适配rapidocr v3+,补充业务字段展示
主要变更: 1. 统一替换rapidocr-onnxruntime为rapidocr+onnxruntime依赖包 2. 适配rapidocr v3+ API变更:调整OCR结果解析逻辑 3. 补充展示员工岗位信息到OA审批通知 4. 优化OA详情链接格式为Markdown可点击样式 5. 修复OCR图片EXIF旋转问题与文件名解码 6. 新增RPA开发提成评分规则文档 7. 重构OA登录入口参数,简化调用逻辑 8. 删除临时测试脚本
This commit is contained in:
+54
-9
@@ -14,11 +14,11 @@ import re
|
||||
from datetime import date, timedelta
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from PIL import Image, ImageEnhance
|
||||
from PIL import Image, ImageEnhance, ImageOps
|
||||
import numpy as np
|
||||
|
||||
try:
|
||||
from rapidocr_onnxruntime import RapidOCR
|
||||
from rapidocr import RapidOCR
|
||||
except ImportError:
|
||||
RapidOCR = None
|
||||
|
||||
@@ -60,26 +60,32 @@ def _read_text(file_path: str, debug: bool = False) -> Tuple[str, str]:
|
||||
- 去空格与特殊符号,便于关键词匹配
|
||||
"""
|
||||
with Image.open(file_path) as img:
|
||||
# 应用 EXIF 方向信息,修正手机横拍导致的旋转
|
||||
img = ImageOps.exif_transpose(img)
|
||||
gray_img = img.convert("L")
|
||||
enhancer = ImageEnhance.Contrast(gray_img)
|
||||
enhanced_img = enhancer.enhance(2.0)
|
||||
img_array = np.array(enhanced_img)
|
||||
|
||||
if _ocr_engine is None:
|
||||
raise RuntimeError("RapidOCR 未安装,请执行: pip install rapidocr-onnxruntime")
|
||||
raise RuntimeError("RapidOCR 未安装,请执行: pip install rapidocr onnxruntime")
|
||||
|
||||
ocr_result, _ = _ocr_engine(img_array)
|
||||
result = _ocr_engine(img_array)
|
||||
|
||||
boxes = result.boxes if result and result.boxes is not None else []
|
||||
txts = result.txts if result and result.txts is not None else ()
|
||||
scores = result.scores if result and result.scores is not None else ()
|
||||
|
||||
if debug:
|
||||
print("=" * 60)
|
||||
print("【OCR原始识别片段(含置信度)】")
|
||||
for idx, (box, txt, score) in enumerate(ocr_result or []):
|
||||
for idx, (txt, score) in enumerate(zip(txts, scores)):
|
||||
print(f" [{idx:02d}] 置信度:{score:.3f} | 文本: {txt}")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
raw_text = ""
|
||||
full_text = ""
|
||||
for box, text, score in ocr_result or []:
|
||||
for text, score in zip(txts, scores):
|
||||
if score < CONFIDENCE_THRESHOLD:
|
||||
continue
|
||||
raw_text += text
|
||||
@@ -197,6 +203,39 @@ def extract_dates(text: str) -> List[date]:
|
||||
return dates
|
||||
|
||||
|
||||
def _extract_registration_date(text: str) -> Optional[date]:
|
||||
"""
|
||||
从结婚证文本中提取"登记日期"字段对应的日期。
|
||||
|
||||
优先查找"登记日期"关键词后面紧跟的日期,如果找不到则返回 None。
|
||||
|
||||
Args:
|
||||
text: OCR 识别的全文
|
||||
|
||||
Returns:
|
||||
登记日期(如果找到),否则返回 None
|
||||
"""
|
||||
# 查找"登记日期"关键词
|
||||
reg_match = re.search(r'登记日期', text)
|
||||
if not reg_match:
|
||||
return None
|
||||
|
||||
# 从"登记日期"位置开始,提取后面紧跟的日期
|
||||
text_after = text[reg_match.end():]
|
||||
date_patterns = [
|
||||
r'(\d{4})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日?',
|
||||
r'(\d{4})\s*[-/.年]\s*(\d{1,2})\s*[-/.月]\s*(\d{1,2})\s*日?',
|
||||
]
|
||||
for pat in date_patterns:
|
||||
m = re.search(pat, text_after)
|
||||
if m:
|
||||
try:
|
||||
return date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def marriage_within_one_year(
|
||||
file_path: str,
|
||||
today: Optional[date] = None,
|
||||
@@ -205,8 +244,7 @@ def marriage_within_one_year(
|
||||
"""
|
||||
识别结婚证上的登记日期并判断是否在当前日期 1 年内。
|
||||
|
||||
结婚证上通常同时出现出生日期与登记日期,登记日期为最近日期,
|
||||
因此取识别到的最大日期作为登记日期。
|
||||
优先查找"登记日期"关键词附近的日期,如果找不到则取识别到的最大日期作为兜底。
|
||||
|
||||
Args:
|
||||
file_path: 结婚证图片路径
|
||||
@@ -234,7 +272,14 @@ def marriage_within_one_year(
|
||||
print("【未识别到任何日期】")
|
||||
return False, None
|
||||
|
||||
reg_date = max(dates)
|
||||
# 优先查找"登记日期"关键词附近的日期
|
||||
reg_date = _extract_registration_date(full_text)
|
||||
if reg_date is None:
|
||||
# 找不到"登记日期"关键词,fallback 到最大日期
|
||||
reg_date = max(dates)
|
||||
if debug:
|
||||
print("【未找到登记日期关键词,使用最大日期兜底】")
|
||||
|
||||
ok = reg_date <= today and (today - reg_date).days <= 365
|
||||
if debug:
|
||||
print(f"【识别日期】: {dates}")
|
||||
|
||||
Reference in New Issue
Block a user