From 8f8106a0a3bf0698e1edc45a98b3cdc21d592213 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Sat, 3 May 2025 02:26:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20tesseract=20=E7=9A=84?= =?UTF-8?q?=E6=A0=B8=E5=BF=83=E9=83=A8=E5=88=86=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/src/main.ts | 4 ++++ service/src/mcp/ocr.service.ts | 14 ++++++++++---- src/hook/hook.controller.ts | 9 ++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/service/src/main.ts b/service/src/main.ts index bc63db3..2df7fd2 100644 --- a/service/src/main.ts +++ b/service/src/main.ts @@ -5,6 +5,7 @@ import { routeMessage } from './common/router'; import { VSCodeWebViewLike } from './hook/adapter'; import path from 'node:path'; import * as fs from 'node:fs'; +import { setRunningCWD } from './hook/setting'; export interface VSCodeMessage { command: string; @@ -94,6 +95,9 @@ function updateConnectionOption(data: any) { } } +const devHome = path.join(__dirname, '..', '..'); +setRunningCWD(devHome); + wss.on('connection', ws => { // 仿造 webview 进行统一接口访问 diff --git a/service/src/mcp/ocr.service.ts b/service/src/mcp/ocr.service.ts index 74058f5..91225ce 100644 --- a/service/src/mcp/ocr.service.ts +++ b/service/src/mcp/ocr.service.ts @@ -47,17 +47,23 @@ export async function tesseractOCR( logger: (message: Tesseract.LoggerMessage) => void, lang: string = 'eng+chi_sim' ) { - + const ocrHome = path.join(RUNNING_CWD, 'resources', 'ocr'); + + if (!ocrHome) { + console.log('ocr 目录不存在'); + return '安装包已经损坏'; + } + try { const { data: { text } } = await Tesseract.recognize( imagePath, lang, { logger, - langPath: './', gzip: false, - cacheMethod: 'cache', - cachePath: RUNNING_CWD + langPath: ocrHome, + corePath: ocrHome, + workerPath: path.join(ocrHome, 'worker.js'), } ); diff --git a/src/hook/hook.controller.ts b/src/hook/hook.controller.ts index 551ff8d..dfa582e 100644 --- a/src/hook/hook.controller.ts +++ b/src/hook/hook.controller.ts @@ -14,8 +14,9 @@ export class HookController { console.log('test ocr begin'); const cachePath = context.extensionPath; - const workerPath = path.join(cachePath, 'resources', 'ocr', 'tesseract-core.js'); - const corePath = path.join(cachePath, 'resources', 'ocr', 'tesseract-core.wasm'); + const workerPath = path.join(cachePath, 'resources', 'ocr', 'worker.js'); + const corePath = path.join(cachePath, 'resources', 'ocr'); + const langPath = path.join(cachePath,'resources', 'ocr'); console.log('workerPath', workerPath); console.log('corePath', corePath); @@ -26,10 +27,8 @@ export class HookController { 'eng+chi_sim', { logger: (m) => console.log(m), - langPath: './', + langPath, gzip: false, - cacheMethod: 'cache', - cachePath, workerPath, corePath }