本地OCR: winsdk(Windows.Media.Ocr) 后端, OCR_BACKEND=local/auto/remote

Browse Source
This commit is contained in:
yws
2026-10-02 00:47:44 +00:00
parent 8d24276511
commit 7eef32fc06
4 changed files with 198 additions and 13 deletions
+2 -1
View File
@@ -54,7 +54,8 @@ OCR 服务默认 http://35.212.193.150:8599/(gcp1 上的 RapidOCR,客户端
|------|------|------|
| `LCP_PLATFORM` | 自动 | win/linux/mac |
| `INPUT_BACKEND` | 平台默认 | mock/sendinput/interception/xdotool/uinput/cg |
| `OCR_URL` | gcp1:8599 | 远端 OCR 服务 |
| `OCR_BACKEND` | auto | local(Windows自带OCR,winsdk) / remote(gcp1) / auto(本地可用优先) |
| `OCR_URL` | gcp1:8599 | remote 后端的 OCR 服务(OCR_BACKEND=local 时用不到) |
| `OCR_MIN_SCORE` | 0.5 | 过滤低置信度 OCR 结果 |
## 工具
+34 -12
View File
@@ -23,6 +23,7 @@ from platform import detect_platform, get_input_backend, get_screenshot_provider
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', 'auto').strip().lower()
MIN_SCORE = float(os.environ.get('OCR_MIN_SCORE', '0.5'))
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
os.makedirs(BASE, exist_ok=True)
@@ -37,18 +38,39 @@ def log(msg):
def ocr_image(img_path):
"""把本地 png POST 给远端 OCR,返回 items 列表。"""
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 = [it for it in data.get('items', []) if float(it.get('score', 0)) >= MIN_SCORE]
"""OCR 分发:local(winsdk/Win自带) / remote(gcp1)。
OCR_BACKEND=auto: 本地可用则本地, 否则远端。"""
impl = None
if OCR_BACKEND == 'local' or OCR_BACKEND == 'auto':
try:
import ocr_local
if ocr_local.available():
impl = ('local', ocr_local)
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:
return None, err
items = [it for it in items if float(it.get('score', 1)) >= MIN_SCORE]
return items, None
+75
View File
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""本地 OCR:Windows.Media.Ocr(winsdk),零远端依赖。
只支持 Windows;Linux/mac 上 import winsdk 会失败 -> 用 remote 后端。
"""
import asyncio
def available():
try:
from winsdk.windows.media.ocr import OcrEngine # noqa: F401
return True
except Exception:
return False
def _engine():
from winsdk.windows.media.ocr import OcrEngine
from winsdk.windows.globalization import Language
try:
eng = OcrEngine.try_create_from_language(Language('zh-Hans'))
if eng:
return eng
except Exception:
pass
return OcrEngine.try_create_from_user_profile_languages()
async def _ocr_async(path):
from winsdk.windows.media.ocr import OcrEngine # noqa: F401
from winsdk.windows.storage import StorageFile, FileAccessMode
from winsdk.windows.graphics.imaging import BitmapDecoder
file = await StorageFile.get_file_from_path_async(path)
stream = await file.open_async(FileAccessMode.READ)
decoder = await BitmapDecoder.create_async(stream)
bitmap = await decoder.get_software_bitmap_async()
eng = _engine()
if eng is None:
return None, 'no OCR engine (language pack missing?)'
result = await eng.recognize_async(bitmap)
items = []
for line in result.lines:
words = line.words
if not words:
continue
text = ' '.join(w.text for w in words)
w0 = words[0].bounding_rect
x0, y0 = w0.x, w0.y
x1, y1 = w0.x + w0.width, w0.y + w0.height
for w in words[1:]:
r = w.bounding_rect
if r.x < x0:
x0 = r.x
if r.y < y0:
y0 = r.y
if r.x + r.width > x1:
x1 = r.x + r.width
if r.y + r.height > y1:
y1 = r.y + r.height
items.append({
'text': text,
'x': int(x0), 'y': int(y0),
'w': int(x1 - x0), 'h': int(y1 - y0),
})
return items, None
def ocr_image(path):
"""同步接口。返回 (items, None) 或 (None, error)。"""
try:
return asyncio.run(_ocr_async(path))
except Exception as e:
return None, 'local ocr fail: %s' % e
+87
View File
@@ -0,0 +1,87 @@
# win_ocr.ps1 - Windows 自带 OCR (Windows.Media.Ocr) 识别本地图片
# 用法: powershell -NoProfile -ExecutionPolicy Bypass -File win_ocr.ps1 -Image <png> [-Lang zh-Hans]
# 输出: UTF-8 JSON => {"ok":true,"items":[{"text","x","y","w","h"},...]}
param(
[Parameter(Mandatory=$true)][string]$Image,
[string]$Lang = "zh-Hans"
)
$ErrorActionPreference = "Stop"
$path = (Resolve-Path $Image -ErrorAction SilentlyContinue)
if (-not $path) { Write-Output '{"ok":false,"error":"image not found"}'; exit 1 }
$path = $path.Path
# ---- 正确加载 WinRT 类型 ----
try {
Add-Type -AssemblyName System.Runtime.WindowsRuntime
[void][System.WindowsRuntimeSystemExtensions, System.Runtime.WindowsRuntime, ContentType=WindowsRuntime]
} catch {
Write-Output '{"ok":false,"error":"winrt load fail"}'; exit 1
}
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {
$_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and
$_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'
})[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
function Write-Json($obj) {
[Console]::OutputEncoding = [Text.Encoding]::UTF8
Write-Output ($obj | ConvertTo-Json -Compress -Depth 5)
}
try {
$file = [Windows.Storage.StorageFile, Windows.Storage, ContentType=WindowsRuntime]::GetFileFromPathAsync($path)
$storageFile = Await $file ([Windows.Storage.StorageFile])
$stream = Await ($storageFile.OpenAsync([Windows.Storage.FileAccessMode]::Read)) ([Windows.Storage.Streams.IRandomAccessStream])
$decoder = Await ([Windows.Graphics.Imaging.BitmapDecoder, Windows.Graphics.Imaging, ContentType=WindowsRuntime]::CreateAsync($stream)) ([Windows.Graphics.Imaging.BitmapDecoder])
$bitmap = Await ($decoder.GetSoftwareBitmapAsync()) ([Windows.Graphics.Imaging.SoftwareBitmap])
$engine = $null
try {
$langObj = New-Object Windows.Globalization.Language $Lang
$engine = [Windows.Media.Ocr.OcrEngine, Windows.Media.Ocr, ContentType=WindowsRuntime]::TryCreateFromLanguage($langObj)
} catch { $engine = $null }
if ($engine -eq $null) {
$engine = [Windows.Media.Ocr.OcrEngine, Windows.Media.Ocr, ContentType=WindowsRuntime]::TryCreateFromUserProfileLanguages()
}
if ($engine -eq $null) {
Write-Json @{ ok = $false; error = 'no OCR engine (need language pack?)' }
exit 1
}
$result = Await ($engine.RecognizeAsync($bitmap)) ([Windows.Media.Ocr.OcrResult])
$items = @()
foreach ($line in $result.Lines) {
if ($line.Words.Count -eq 0) { continue }
$t = ($line.Words | ForEach-Object { $_.Text }) -join " "
$minX = [math]::Floor($line.Words[0].BoundingRect.X)
$minY = [math]::Floor($line.Words[0].BoundingRect.Y)
$maxX = [math]::Ceiling($line.Words[0].BoundingRect.X + $line.Words[0].BoundingRect.Width)
$maxY = [math]::Ceiling($line.Words[0].BoundingRect.Y + $line.Words[0].BoundingRect.Height)
foreach ($word in $line.Words) {
$r = $word.BoundingRect
if ($r.X -lt $minX) { $minX = [math]::Floor($r.X) }
if ($r.Y -lt $minY) { $minY = [math]::Floor($r.Y) }
if (($r.X + $r.Width) -gt $maxX) { $maxX = [math]::Ceiling($r.X + $r.Width) }
if (($r.Y + $r.Height) -gt $maxY) { $maxY = [math]::Ceiling($r.Y + $r.Height) }
}
$items += [PSCustomObject]@{
text = $t
x = $minX; y = $minY; w = ($maxX - $minX); h = ($maxY - $minY)
}
}
Write-Json @{ ok = $true; items = $items }
} catch {
Write-Json @{ ok = $false; error = $_.Exception.Message }
exit 1
}