Skip to content

Commit

Permalink
Merge pull request #27 from macacajs/yhn-screen-enhance
Browse files Browse the repository at this point in the history
fix: ocr改成异步方法
  • Loading branch information
yihuineng authored Apr 14, 2023
2 parents 1c275f5 + 871c6b5 commit bce2a9a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions bin/macaca-macos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-macos",
"version": "0.2.31",
"version": "0.2.32",
"description": "Macaca MacOS driver",
"keywords": [
"macos",
Expand Down
4 changes: 2 additions & 2 deletions src/driver/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 默认全屏
Expand All @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions src/driver/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ 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);
}

/**
* 使用系统ocr能力
*/
screenOcr(opts: {
async screenOcr(opts: {
picFile?: string;
rectangle?: string; // 通过矩形框 x,y,width,height
count?: number; // 支持多次结果合并返回,增强识别率
Expand All @@ -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,
Expand All @@ -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<boolean> {
const { text, picFile, rectangle } = opts;
const res = this.getTextsPosition({
const res = await this.getTextsPosition({
texts: [ text ],
picFile, rectangle,
});
Expand All @@ -61,7 +62,7 @@ export default class ScreenDriver {
* 获取文案在截图区域的位置
* @param opts
*/
getTextsPosition(opts: {
async getTextsPosition(opts: {
texts: string[]; // 目标文案
index?: number; // 重复项指针
contains?: boolean; // 包含即可
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/macaca-macos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit bce2a9a

Please sign in to comment.