去云端: OCR 纯本地(winsdk), 删除 remote 后端与 ocr_server
This commit is contained in:
@@ -25,8 +25,8 @@ wait_for_text("结算") -> 轮询直到文字出现/消失
|
|||||||
|
|
||||||
`LCP_PLATFORM=win|linux|mac` 强制指定,默认按系统自动检测。
|
`LCP_PLATFORM=win|linux|mac` 强制指定,默认按系统自动检测。
|
||||||
|
|
||||||
OCR 服务默认 http://35.212.193.150:8599/(gcp1 上的 RapidOCR,客户端零安装),
|
OCR 全本地:`ocr_local.py` 调 Windows 自带 OCR(Windows.Media.Ocr,需 `pip install --user winsdk`)。
|
||||||
可 `OCR_URL` 覆盖。
|
无任何云端/远端服务依赖——MCP 是纯本地"外骨骼"。
|
||||||
|
|
||||||
## 部署
|
## 部署
|
||||||
|
|
||||||
@@ -54,8 +54,7 @@ OCR 服务默认 http://35.212.193.150:8599/(gcp1 上的 RapidOCR,客户端
|
|||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `LCP_PLATFORM` | 自动 | win/linux/mac |
|
| `LCP_PLATFORM` | 自动 | win/linux/mac |
|
||||||
| `INPUT_BACKEND` | 平台默认 | mock/sendinput/interception/xdotool/uinput/cg |
|
| `INPUT_BACKEND` | 平台默认 | mock/sendinput/interception/xdotool/uinput/cg |
|
||||||
| `OCR_BACKEND` | auto | local(Windows自带OCR,winsdk) / remote(gcp1) / auto(本地可用优先) |
|
| `OCR_BACKEND` | local | 本地 OCR(仅 local;Win 需 winsdk) |
|
||||||
| `OCR_URL` | gcp1:8599 | remote 后端的 OCR 服务(OCR_BACKEND=local 时用不到) |
|
|
||||||
| `OCR_MIN_SCORE` | 0.5 | 过滤低置信度 OCR 结果 |
|
| `OCR_MIN_SCORE` | 0.5 | 过滤低置信度 OCR 结果 |
|
||||||
|
|
||||||
## 工具
|
## 工具
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import urllib.request
|
|
||||||
|
|
||||||
from platform import detect_platform, get_input_backend, get_screenshot_provider
|
from platform import detect_platform, get_input_backend, get_screenshot_provider
|
||||||
|
|
||||||
BACKEND_NAME = os.environ.get('INPUT_BACKEND', '').strip().lower()
|
BACKEND_NAME = os.environ.get('INPUT_BACKEND', '').strip().lower()
|
||||||
OCR_URL = os.environ.get('OCR_URL', 'http://35.212.193.150:8599/').strip().rstrip('/') + '/'
|
OCR_BACKEND = os.environ.get('OCR_BACKEND', 'local').strip().lower()
|
||||||
OCR_BACKEND = os.environ.get('OCR_BACKEND', 'auto').strip().lower()
|
|
||||||
MIN_SCORE = float(os.environ.get('OCR_MIN_SCORE', '0.5'))
|
MIN_SCORE = float(os.environ.get('OCR_MIN_SCORE', '0.5'))
|
||||||
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
|
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
|
||||||
os.makedirs(BASE, exist_ok=True)
|
os.makedirs(BASE, exist_ok=True)
|
||||||
@@ -38,36 +36,14 @@ def log(msg):
|
|||||||
|
|
||||||
|
|
||||||
def ocr_image(img_path):
|
def ocr_image(img_path):
|
||||||
"""OCR 分发:local(winsdk/Win自带) / remote(gcp1)。
|
"""本地 OCR(Windows 自带 OCR,winsdk;无任何云端依赖)。"""
|
||||||
OCR_BACKEND=auto: 本地可用则本地, 否则远端。"""
|
try:
|
||||||
impl = None
|
import ocr_local
|
||||||
if OCR_BACKEND == 'local' or OCR_BACKEND == 'auto':
|
except Exception as e:
|
||||||
try:
|
return None, 'ocr_local import fail: %s' % e
|
||||||
import ocr_local
|
if not ocr_local.available():
|
||||||
if ocr_local.available():
|
return None, 'winsdk 不可用: pip install --user winsdk'
|
||||||
impl = ('local', ocr_local)
|
items, err = ocr_local.ocr_image(img_path)
|
||||||
except Exception:
|
|
||||||
impl = None
|
|
||||||
if impl is None and OCR_BACKEND != 'local':
|
|
||||||
impl = ('remote', None)
|
|
||||||
if impl is None:
|
|
||||||
return None, 'no usable OCR backend (OCR_BACKEND=%s)' % OCR_BACKEND
|
|
||||||
|
|
||||||
kind, mod = impl
|
|
||||||
if kind == 'local':
|
|
||||||
items, err = mod.ocr_image(img_path)
|
|
||||||
else:
|
|
||||||
with open(img_path, 'rb') as f:
|
|
||||||
body = f.read()
|
|
||||||
req = urllib.request.Request(OCR_URL, data=body, headers={'Content-Type': 'image/png'})
|
|
||||||
try:
|
|
||||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
||||||
data = json.loads(resp.read().decode('utf-8'))
|
|
||||||
except Exception as e:
|
|
||||||
return None, 'ocr call fail: %s' % e
|
|
||||||
if not data.get('ok'):
|
|
||||||
return None, data.get('error', 'ocr error')
|
|
||||||
items = data.get('items', [])
|
|
||||||
if items is None:
|
if items is None:
|
||||||
return None, err
|
return None, err
|
||||||
items = [it for it in items if float(it.get('score', 1)) >= MIN_SCORE]
|
items = [it for it in items if float(it.get('score', 1)) >= MIN_SCORE]
|
||||||
@@ -191,7 +167,7 @@ def send(obj):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
platform = detect_platform()
|
platform = detect_platform()
|
||||||
log('lc_mcp v0.5 platform=%s backend=%s ocr=%s' % (platform, BACKEND_NAME or 'auto', OCR_URL))
|
log('lc_mcp v0.5 platform=%s backend=%s ocr=%s' % (platform, BACKEND_NAME or 'auto', OCR_BACKEND))
|
||||||
backend = None
|
backend = None
|
||||||
try:
|
try:
|
||||||
provider = get_screenshot_provider(platform)
|
provider = get_screenshot_provider(platform)
|
||||||
|
|||||||
Reference in New Issue
Block a user