From 871c6b526ae209ea7af57ad6a9277c6d9eb2deb3 Mon Sep 17 00:00:00 2001 From: yihuineng <471110230@qq.com> Date: Fri, 14 Apr 2023 10:58:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ocr=E6=94=B9=E6=88=90=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/macaca-macos.ts | 4 ++-- package.json | 2 +- src/driver/mouse.ts | 4 ++-- src/driver/screen.ts | 19 ++++++++++--------- test/macaca-macos.test.ts | 4 ++-- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/bin/macaca-macos.ts b/bin/macaca-macos.ts index db904a3..58bf85c 100755 --- a/bin/macaca-macos.ts +++ b/bin/macaca-macos.ts @@ -78,14 +78,14 @@ program let res; if (target.endsWith('.png') || target.endsWith('.jpg')) { target = target.startsWith('/') ? target : path.resolve(process.cwd(), target); - res = driver.screenOcr({ + res = await driver.screenOcr({ picFile: target, }); } else { const appName = target; await driver.focusApp(appName); const rect = await driver.getAppSizePosition(appName); - res = driver.screenOcr({ + res = await driver.screenOcr({ rectangle: `${rect.topLeftX},${rect.topLeftY},${rect.width},${rect.height}`, }); } diff --git a/package.json b/package.json index 3d949d8..a4e8790 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-macos", - "version": "0.2.31", + "version": "0.2.32", "description": "Macaca MacOS driver", "keywords": [ "macos", diff --git a/src/driver/mouse.ts b/src/driver/mouse.ts index f491d2a..1959502 100644 --- a/src/driver/mouse.ts +++ b/src/driver/mouse.ts @@ -38,7 +38,7 @@ export default class MouseDriver { * 依赖 ocr * @param opts */ - mouseClickText(opts: { + async mouseClickText(opts: { text: string; // 目标文案 index?: number; // 重复项指针 rectangle?: string; // 截图目标区域 通过矩形框 x,y,width,height 默认全屏 @@ -52,7 +52,7 @@ export default class MouseDriver { shiftX = 0, shiftY = 0, } = opts; - const res = new ScreenDriver().getTextsPosition({ + const res = await new ScreenDriver().getTextsPosition({ texts: [ text ], index, rectangle, diff --git a/src/driver/screen.ts b/src/driver/screen.ts index 2ca5a98..8d59532 100644 --- a/src/driver/screen.ts +++ b/src/driver/screen.ts @@ -10,10 +10,10 @@ export default class ScreenDriver { /** * 暴露ocr方法,支持通过重写使用三方能力替代 */ - fileOcr(imgFile: string): { + async fileOcr(imgFile: string): Promise<{ rect: { left, top, height, width }; word: string; - }[] { + }[]> { const resStr = shell.exec(`${Helper.getResourcePath()}/swift/ocr ${imgFile}`, { silent: true }).stdout; return JSON.parse(resStr); } @@ -21,7 +21,7 @@ export default class ScreenDriver { /** * 使用系统ocr能力 */ - screenOcr(opts: { + async screenOcr(opts: { picFile?: string; rectangle?: string; // 通过矩形框 x,y,width,height count?: number; // 支持多次结果合并返回,增强识别率 @@ -33,7 +33,8 @@ export default class ScreenDriver { } const ocrRes = []; for (let i = 0; i < count; i++) { - ocrRes.push(...this.fileOcr(saveFile)); + const res = await this.fileOcr(saveFile); + ocrRes.push(...res); } return { imgFile: saveFile, @@ -44,13 +45,13 @@ export default class ScreenDriver { /** * 检查文案存在 */ - checkTextExist(opts: { + async checkTextExist(opts: { text: string; picFile?: string; // 可直接指定图片 rectangle?: string; // 截图目标区域 通过矩形框 x,y,width,height 默认全屏 - }): boolean { + }): Promise { const { text, picFile, rectangle } = opts; - const res = this.getTextsPosition({ + const res = await this.getTextsPosition({ texts: [ text ], picFile, rectangle, }); @@ -61,7 +62,7 @@ export default class ScreenDriver { * 获取文案在截图区域的位置 * @param opts */ - getTextsPosition(opts: { + async getTextsPosition(opts: { texts: string[]; // 目标文案 index?: number; // 重复项指针 contains?: boolean; // 包含即可 @@ -74,7 +75,7 @@ export default class ScreenDriver { contains = true, } = opts; // 获取文案位置 - const { ocrRes } = this.screenOcr({ picFile, rectangle }); + const { ocrRes } = await this.screenOcr({ picFile, rectangle }); const resultList = []; // 找多个目标 for (const text of texts) { diff --git a/test/macaca-macos.test.ts b/test/macaca-macos.test.ts index 3116502..852af3b 100644 --- a/test/macaca-macos.test.ts +++ b/test/macaca-macos.test.ts @@ -43,7 +43,7 @@ describe('macaca-macos unit testing', function() { it.skip('overwrite should be ok', async () => { this.timeout(0); - driver.fileOcr = (imgFile) => { + driver.fileOcr = async (imgFile) => { console.log(imgFile); return [ { @@ -74,7 +74,7 @@ describe('macaca-macos unit testing', function() { it.skip('screen ocr should be ok', async function() { this.timeout(0); - const res = driver.screenOcr(); + const res = await driver.screenOcr(); console.log(JSON.stringify(res, null, 2)); assert(res.ocrRes); });