# -*- coding: utf-8 -*- """同一张渲染 PNG 分别喂给 两个 Python 后端 的 ocr_worker,对比识别结果。""" import base64 import json import subprocess import sys ROOT = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\pdfbt" IMG = ROOT + r"\bin\Debug\realtest\selftest_render.png" WORKER = ROOT + r"\bin\Debug\ocr_worker.py" PYENV = ROOT + r"\pyenv\python.exe" PYDEV = r"F:\PythonDev\python\python\python.exe" def run_ocr(python_exe, label): with open(IMG, "rb") as f: b64 = base64.b64encode(f.read()).decode() p = subprocess.Popen([python_exe, "-u", WORKER], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, encoding="utf-8") # 等 warm+ready 两条就绪消息 line1 = p.stdout.readline() line2 = p.stdout.readline() if 'ready' not in line2: # 兼容只发一条的情况 if 'ready' not in line1 and 'warm' not in line1: print(label, '未就绪:', line1[:120], line2[:120]); p.kill(); return None req = json.dumps({"id": 1, "image": b64}, ensure_ascii=False) p.stdin.write(req + "\n") p.stdin.flush() resp = p.stdout.readline() p.kill() try: obj = json.loads(resp) except Exception as e: print(label, "解析失败:", e, "| 原始:", resp[:200]) return None items = obj.get("items") or [] texts = [it.get("text", "") for it in items] print("%s: %d 条, 耗时 %s ms" % (label, len(items), obj.get("ms"))) return texts t_pyenv = run_ocr(PYENV, "pyenv(CPU)") t_dev = run_ocr(PYDEV, "开发机(DML)") if t_pyenv is not None and t_dev is not None: s1, s2 = set(t_pyenv), set(t_dev) only_env = [t for t in t_pyenv if t not in s2] only_dev = [t for t in t_dev if t not in s1] print("\n仅 pyenv 识别到(%d):" % len(only_env)) for t in only_env[:10]: print(" +", t) print("仅 开发机 识别到(%d):" % len(only_dev)) for t in only_dev[:10]: print(" -", t)