From 01f8339051e719f38c3bb2f45f79d39913cbee09 Mon Sep 17 00:00:00 2001 From: Lei PAN Date: Thu, 26 Oct 2023 17:13:25 +0200 Subject: [PATCH] use macocr --- src/models/OCRResult.js | 1 + src/utiles/CmdUtils.js | 1 - src/workers/CommandorPage.js | 26 +++++++------------------- src/workers/OCRChecker.js | 29 ++++------------------------- 4 files changed, 12 insertions(+), 45 deletions(-) diff --git a/src/models/OCRResult.js b/src/models/OCRResult.js index 3459c1e..ea471d8 100644 --- a/src/models/OCRResult.js +++ b/src/models/OCRResult.js @@ -22,5 +22,6 @@ const OCRResult = { BRAVE_PRIVACY: Symbol("BRAVE_PRIVACY"), BRAVE_PRIVACY_PUB: Symbol("BRAVE_PRIVACY_PUB"), BRAVE_NOTIFICATION: Symbol("BRAVE_NOTIFICATION"), + CHOOSE_POSITION: Symbol("CHOOSE_POSITION"), } module.exports = OCRResult \ No newline at end of file diff --git a/src/utiles/CmdUtils.js b/src/utiles/CmdUtils.js index b4cac8f..e9e5834 100644 --- a/src/utiles/CmdUtils.js +++ b/src/utiles/CmdUtils.js @@ -35,7 +35,6 @@ function cmdExecute(command) { async function findText(imgPath) { // Load images - let baseDir = "/Users/panlei/Documents/workspace/MacOCI" let cmd = `${baseDir}/venv/bin/python ${baseDir}/MacOCR.py ${imgPath}` let stdOut = await cmdExecute(cmd); console.log(stdOut) diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index 02252bf..a493558 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -874,7 +874,6 @@ class CommandorPage { if (isSuccessful) { checkResult = await this.ocrChecker.get_result(); } else { - // await this.checkResultWithOcr() checkResult = OCRResult.TERMINAED this.isTerminated = true } @@ -1034,6 +1033,13 @@ class CommandorPage { await delay(2000); await this.checkResultWithOcr(); break; + case OCRResult.CHOOSE_POSITION: + logWithDevice("CHOOSE_POSITION", this.device) + await this.tapForDevice(this.device, 300, 1300) + await delay(2000); + await this.tapForDevice(this.device, 300, 1700) + await this.checkResultWithOcr(); + break; case OCRResult.TO_SKIP : logWithDevice("TO_SKIP", this.device) @@ -1124,11 +1130,9 @@ class CommandorPage { } else { logWithDevice("handleBraveSkipBtn", this.device) await this.tapForDevice(this.device, 470, 1160) - // await this.device.shell("input tap " + 470 + " " + 1160) } await delay(2000); await this.tapForDevice(this.device, 455, 1920) - // await this.device.shell("input tap " + 455 + " " + 1920) await delay(1000); } @@ -1152,22 +1156,6 @@ class CommandorPage { async closePage() { await this.resetBrowser(); - // while (!this.browser.isConnected()) { - // logWithDevice("trying to connect to browser", this.device) - // this.browser = await puppeteer.connect({ - // browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", - // headless: false, defaultViewport: null - // }) - // await delay(2 * 1000); - // } - // console.log("will close all pages"); - // let pages = await this.browser.pages(); - // await pages[0].close() - // console.log("page closed"); - // pages.forEach((currentPage) => { - // currentPage.close(); - // }) - // await this.browser.close(); } async skipOptimizationPage() { diff --git a/src/workers/OCRChecker.js b/src/workers/OCRChecker.js index 6c4158e..06bf344 100644 --- a/src/workers/OCRChecker.js +++ b/src/workers/OCRChecker.js @@ -1,7 +1,5 @@ const {v4: uuidv4} = require("uuid"); -const tesseract = require("node-tesseract-ocr"); const OCRResult = require("../models/OCRResult"); -const Jimp = require('jimp'); const fs = require("fs"); const {exec} = require("child_process"); const findText = require("../utiles/CmdUtils"); @@ -14,15 +12,11 @@ function delay(delayInMs) { }); } -const config = { - lang: "eng", - oem: 1, - psm: 3, -} const NO_INTERNET_FR = "Aucun accés a Internet" const MESSAGE_URL_VALIDATION_FR = "envoyé un lien par e-mail." const MESSAGE_URL_VALIDATION_FR_2 = "un lien par e-mail" const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email" +const CHOOSE_POSITION_GOOGLE_FR = " Choisir la position pour les résultats de recherche" const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again" const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué" const BLOCKED_MSG_EN = "have been blocked" @@ -83,41 +77,36 @@ class OCRChecker { return this.contact.passportNumber + "_" + uuid + ".png" } - async get_result() { let fileName = await this.take_screen_shot() - // let screenShot = await convertImageToWhiteBlack(fileName, this.device); await delay(1000) try { let result = await findText(fileName) console.log(result) if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR) || result.includes(MESSAGE_URL_VALIDATION_FR_2)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.SUCCESS } else if (result.includes(BRAVE_NOTIFICATION)) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) return OCRResult.BRAVE_NOTIFICATION + } else if (result.includes(CHOOSE_POSITION_GOOGLE_FR)) { + await this.deleteFile(fileName) + return OCRResult.CHOOSE_POSITION } else if (result.includes(MESSAGE_FILL_FIELD_FR) || result.includes(MESSAGE_FILL_FIELD_FR_2) || result.includes(MESSAGE_FILL_FIELD_FR_3) || result.includes(MESSAGE_FILL_FIELD_FR_4) || result.includes(MESSAGE_FILL_FIELD_FR_5)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.FILL_FIELD } else if (result.includes(CAPTCHA_ERROR_MESSAGE) || result.includes(CAPTCHA_ERROR_MESSAGE_FR)) { return OCRResult.RECAPTCHA_ERROR } else if (result.includes(BLOCKED_MSG_EN) || result.includes(BLOCKED_MSG_FR) || result.includes(BLOCKED_MSG_FR_2)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.BLOCKED } else if (result.includes(ERR_CACHE_MISS) || result.includes(ERR_CACHE_MISS_2) || result.includes(ERR_CACHE_MISS_3) || result.includes(ERR_CACHE_MISS_4)) { return OCRResult.TO_REFRESH } else if (result.includes(CHECKING_MSG_FR) || result.includes(CHECKING_MSG_FR_2)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.RECHECK } else if (result.includes(BRAVE_NOTIFICATION)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.BRAVE_NOTIFICATION } else if (result.includes(SLIDING_CAPTCHA_FR) || result.includes(SLIDING_CAPTCHA_FR_2) || result.includes(SLIDING_CAPTCHA_FR_3) || result.includes(SLIDING_CAPTCHA_FR_4)) { await this.deleteFile(fileName) @@ -127,15 +116,12 @@ class OCRChecker { return OCRResult.SLIDING_CAPTCHA } else if (result.includes(WELCOME_MESSAGE_FR)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.NEED_TO_CLICK_LATE_BTN } else if (result.includes(GOOGLE_DISCONNECT_FR) || result.includes(GOOGLE_DISCONNECT_FR_1)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.GOOGLE_DISCONNECT } else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.ONLINE_APPOINTMENT } else if (result.includes(PAGE_OPTIMIZATION_CHROME_FR) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_2) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_3) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_4) || result.includes(PAGE_OPTIMIZATION_CHROME_FR_5) @@ -144,11 +130,9 @@ class OCRChecker { || result.includes(PAGE_OPTIMIZATION_CHROME_FR_7) ) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.PAGE_OPTIMIZATION } else if (result.includes(CONFIRM_RESEND_FORM_FR)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.CONFIRM_RESEND_FORM } else if (result.includes(CLOSED_MESSAGE_FR)) { return OCRResult.CLOSED @@ -156,7 +140,6 @@ class OCRChecker { return OCRResult.TERMINAED } else if (result.includes(DIALOG_TO_SKIP)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.TO_SKIP } else if (result.includes(RECAPTCHA_FAILED_FR)) { return OCRResult.TERMINAED @@ -164,15 +147,12 @@ class OCRChecker { return OCRResult.NO_INTERNET } else if (result.includes(BRAVE_SKIP_DEFAULT_PAGE) || result.includes(BRAVE_SKIP_DEFAULT_PAGE_2)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.BRAVE_SKIP } else if (result.includes(BRAVE_SKIP_PRIVACY_PAGE) || result.includes(BRAVE_SKIP_PRIVACY_PAGE_2)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.BRAVE_PRIVACY } else if (result.includes(BRAVE_SKIP_PUB) || result.includes(BRAVE_SKIP_PUB_2) || result.includes(BRAVE_SKIP_PUB_3) || result.includes(BRAVE_SKIP_PUB_4) || result.includes(BRAVE_SKIP_PUB_5)) { await this.deleteFile(fileName) - // await this.deleteFile(screenShot) return OCRResult.BRAVE_PRIVACY_PUB } else { return OCRResult.TERMINAED @@ -198,7 +178,6 @@ class OCRChecker { let stdout1 = await exec("adb -s " + this.device.serial() + " exec-out screencap -p > " + name) await delay(5000); console.log(`stdout: ${stdout1}`); - // await this.device.screenshot({path: name}); return name } }