feat: OCR 服务 + 会议材料模块
ry-ocr/ (新)
本地发票识别微服务 (PaddleOCR 3.x + FastAPI, 8801)
- QR 优先: 扫到二维码即取开票时间/发票号/金额; 没扫到/格式不合法直接判非发票, 不跑 OCR
- 配置 QR_FULL_OCR 控制快路径(false, 0.2s)还是全字段(true, 4.5s)
- /recognize/invoice (multipart) + /recognize/invoice/by-path (本地路径, 白名单) + /recognize/text
- is_invoice / from_qr / qr_raw / qr_error / error_code 字段
- 12 字段发票抽取 (regex + 启发式, 左右主体识别)
- 超时保护 (15s 单页 / 60s 总流程) + PaddleOCR 单例 + ThreadPoolExecutor
ry-api/ruoyi-business/
- pom.xml: 加 hutool-http/json/core 5.8.27, lombok 1.18.30 (OcrClient @Slf4j 所需)
- ocr/: OcrClient + InvoiceResult/Fields/Line + ZipExtractor + InvoiceOcrScheduler
- oss/: OssUploader + OssConfMeta (OCR 识别后重传 OSS)
- config/: OcrConfig + OcrExecutorConfig (后台线程池)
- service/impl/InvoiceOcrService: 后台提交 OCR, ZIP 路径解压识别, 替换场景先清旧
- 会议材料 CRUD 全套 (BizMeetingAuditLog/Executor/Invoice/Material/Supervisor):
controller + service + mapper + domain + xml
ry-vue3/
- MeetingDetail.vue (新建): 会议详情页 (含评分维度章节, 改只读)
- Meetings.vue / OssFileUploader.vue / router / Login.vue: 适配新字段
ry-api/ruoyi-admin/
- RuoYiApplication.java + application.yml: 启用 @Async 异步支持
_self/
- manager_meetings.md / manager_meeting_detail.md: 文档
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""tests"""
|
||||
@@ -0,0 +1,44 @@
|
||||
"""基准测试:模型预热后,统计 PNG / PDF 单次识别耗时"""
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from app.core import warmup
|
||||
from app.services.recognize_service import recognize_file
|
||||
|
||||
|
||||
def bench(label: str, file_path: Path, runs: int = 5):
|
||||
content = file_path.read_bytes()
|
||||
print(f"\n=== {label}: {file_path.name} ({len(content)} bytes) ===")
|
||||
|
||||
# 预热(不计耗时) - 现在 warmup 会真跑一次 dummy 图预测
|
||||
print("预热中(不计入)...", flush=True)
|
||||
warmup()
|
||||
print("预热完成", flush=True)
|
||||
|
||||
times = []
|
||||
for i in range(runs):
|
||||
t0 = time.time()
|
||||
result = recognize_file(file_path.name, content)
|
||||
elapsed = (time.time() - t0) * 1000
|
||||
times.append(elapsed)
|
||||
print(f" 第 {i+1} 次: {elapsed:7.1f} ms success={result.success}")
|
||||
|
||||
times.sort()
|
||||
print(f"\n min: {times[0]:7.1f} ms")
|
||||
print(f" median: {times[len(times)//2]:7.1f} ms")
|
||||
print(f" max: {times[-1]:7.1f} ms")
|
||||
print(f" avg: {sum(times)/len(times):7.1f} ms")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bench("PNG", ROOT / "fapiao_1.png", runs=5)
|
||||
bench("PDF", ROOT / "fapiao.pdf", runs=5)
|
||||
@@ -0,0 +1,93 @@
|
||||
"""基准测试:不同 DPI 下的 PDF 渲染耗时 vs OCR 耗时 vs 识别准确度"""
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
import fitz
|
||||
from app.core import warmup
|
||||
from app.services.recognize_service import recognize_file
|
||||
|
||||
|
||||
def render(pdf_path: Path, dpi: int) -> Path:
|
||||
out = ROOT / f"_bench_{dpi}dpi.png"
|
||||
doc = fitz.open(str(pdf_path))
|
||||
page = doc[0]
|
||||
pix = page.get_pixmap(matrix=fitz.Matrix(dpi / 72, dpi / 72), alpha=False)
|
||||
pix.save(str(out))
|
||||
doc.close()
|
||||
return out
|
||||
|
||||
|
||||
def bench_dpi(pdf_path: Path, dpi: int, runs: int = 3):
|
||||
img_path = render(pdf_path, dpi)
|
||||
img_bytes = img_path.read_bytes()
|
||||
print(f"\n--- DPI={dpi} | 渲染图 {img_path.stat().st_size//1024}KB ---")
|
||||
|
||||
times = []
|
||||
fields_ok = None
|
||||
for i in range(runs):
|
||||
t0 = time.time()
|
||||
result = recognize_file(img_path.name, img_bytes)
|
||||
elapsed = (time.time() - t0) * 1000
|
||||
times.append(elapsed)
|
||||
if i == 0 and result.success:
|
||||
fields_ok = result.fields
|
||||
print(f" run {i+1}: {elapsed:7.1f}ms success={result.success}")
|
||||
|
||||
# 清理中间文件
|
||||
img_path.unlink(missing_ok=True)
|
||||
(ROOT / f".{img_path.stem}_pages").rmdir() if (ROOT / f".{img_path.stem}_pages").exists() else None
|
||||
# 上面可能删不了子目录,try 再清理 _pages 下文件
|
||||
pages_dir = ROOT / f".{img_path.stem}_pages"
|
||||
if pages_dir.exists():
|
||||
for f in pages_dir.glob("*"):
|
||||
f.unlink(missing_ok=True)
|
||||
pages_dir.rmdir()
|
||||
|
||||
times.sort()
|
||||
print(f" min={times[0]:.1f}ms median={times[len(times)//2]:.1f}ms max={times[-1]:.1f}ms")
|
||||
|
||||
return fields_ok
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pdf = ROOT / "fapiao.pdf"
|
||||
print(f"PDF: {pdf.name}, {pdf.stat().st_size//1024}KB, 1 页")
|
||||
|
||||
warmup()
|
||||
print("预热完成\n")
|
||||
|
||||
print("=" * 60)
|
||||
fields_100 = bench_dpi(pdf, 100)
|
||||
print("=" * 60)
|
||||
fields_150 = bench_dpi(pdf, 150)
|
||||
print("=" * 60)
|
||||
fields_200 = bench_dpi(pdf, 200)
|
||||
print("=" * 60)
|
||||
fields_300 = bench_dpi(pdf, 300)
|
||||
print("=" * 60)
|
||||
|
||||
# 汇总
|
||||
print("\n\n=== 关键字段对比 ===")
|
||||
print(f"{'字段':<15} {'100dpi':<25} {'150dpi':<25} {'200dpi':<25} {'300dpi':<25}")
|
||||
key_fields = ["invoice_no", "invoice_date", "amount", "amount_cn",
|
||||
"amount_pretax", "tax_amount", "seller_name", "buyer_name",
|
||||
"seller_tax_no", "buyer_tax_no", "amount_match"]
|
||||
for k in key_fields:
|
||||
row = [k]
|
||||
for f in [fields_100, fields_150, fields_200, fields_300]:
|
||||
v = getattr(f, k) if f else None
|
||||
if v is None:
|
||||
row.append("(空)")
|
||||
else:
|
||||
s = str(v)[:23]
|
||||
row.append(s)
|
||||
print(f"{row[0]:<15} {row[1]:<25} {row[2]:<25} {row[3]:<25} {row[4]:<25}")
|
||||
@@ -0,0 +1,91 @@
|
||||
"""单元测试:发票字段抽取(不依赖 PaddleOCR)
|
||||
|
||||
直接喂文本,验证字段抽取与金额校验逻辑。
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
# 强制 UTF-8 输出(Windows cp936 默认中文乱码)
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
from app.services import extract_invoice # noqa: E402
|
||||
|
||||
|
||||
SAMPLE_VAT_ELEC = """
|
||||
增值税电子普通发票
|
||||
发票代码: 011002000000
|
||||
发票号码: 12345678
|
||||
开票日期: 2024年05月20日
|
||||
名称: 上海某科技有限公司
|
||||
纳税人识别号: 91310115MA1K3X9Y8A
|
||||
名称: 北京某某贸易有限公司
|
||||
纳税人识别号: 91110108MA01K3X9Y8A
|
||||
金额 ¥1234.56
|
||||
税额 ¥74.07
|
||||
价税合计 ¥1308.63
|
||||
(贰仟零捌元陆角叁分)
|
||||
备注:
|
||||
"""
|
||||
|
||||
|
||||
SAMPLE_VAT_SPECIAL = """
|
||||
增值税专用发票
|
||||
发票代码 011002100111
|
||||
发票号码 87654321
|
||||
开 票 日 期: 2023年12月01日
|
||||
名 称: 深圳某有限公司
|
||||
纳税人识别号: 91440300MA5DCBA123
|
||||
名 称: 广州某科技股份公司
|
||||
纳税人识别号: 91440101MA59ABC987
|
||||
不含税价 ¥10000.00 税率 13%
|
||||
税 额 ¥1300.00
|
||||
价税合计(大写)壹万壹仟叁佰元整 (小写)¥11300.00
|
||||
"""
|
||||
|
||||
|
||||
def test_vat_electronic():
|
||||
fields = extract_invoice(SAMPLE_VAT_ELEC)
|
||||
print(fields.model_dump_json(indent=2))
|
||||
assert fields.invoice_type == "增值税电子普通发票"
|
||||
assert fields.invoice_code == "011002000000"
|
||||
assert fields.invoice_no == "12345678"
|
||||
assert fields.invoice_date == "2024-05-20"
|
||||
assert fields.amount is not None and abs(fields.amount - 1308.63) < 0.01
|
||||
|
||||
|
||||
def test_vat_special():
|
||||
fields = extract_invoice(SAMPLE_VAT_SPECIAL)
|
||||
print(fields.model_dump_json(indent=2))
|
||||
assert fields.invoice_type == "增值税专用发票"
|
||||
assert fields.invoice_no == "87654321"
|
||||
assert fields.invoice_date == "2023-12-01"
|
||||
assert fields.amount is not None and abs(fields.amount - 11300.0) < 0.01
|
||||
# 大写金额提取(壹万壹仟叁佰元整)
|
||||
assert fields.amount_cn is not None
|
||||
assert "壹万" in fields.amount_cn or "万" in fields.amount_cn
|
||||
# 大写 vs 小写一致
|
||||
assert fields.amount_match is True
|
||||
|
||||
|
||||
def test_amount_consistency():
|
||||
"""大写小写一致"""
|
||||
from app.utils import amount_consistent
|
||||
assert amount_consistent("贰佰元整", 200.0) is True
|
||||
assert amount_consistent("壹仟元整", 1000.0) is True
|
||||
# 不一致
|
||||
assert amount_consistent("贰佰元整", 300.0) is False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_vat_electronic()
|
||||
print("-" * 60)
|
||||
test_vat_special()
|
||||
print("-" * 60)
|
||||
test_amount_consistency()
|
||||
print("✅ all tests passed")
|
||||
Reference in New Issue
Block a user