diff --git a/src/openCvTest.js b/src/openCvTest.js index addcd79..5261295 100644 --- a/src/openCvTest.js +++ b/src/openCvTest.js @@ -1,40 +1,14 @@ -const cv = require('opencv4nodejs'); +const cmdExecute = require("./utiles/CmdUtils"); -// cv.imread('/Users/lpan/Downloads/Screenshot_2023-05-19-14-09-11-82_40deb401b9ffe8e1df2f1cc5ba480b12.jpg', function (err, im) { -// if (err) return console.error('error loading image'); + +// const findWaldo = async () => { +// // Load images +// let imagePath = "/Users/panlei/Downloads/to_handle/E91954512_cc1d1239-70ef-4265-861e-851517eb68f1.png" +// let baseDir = "/Users/panlei/Documents/workspace/MacOCI" +// let cmd = `${baseDir}/venv/bin/python ${baseDir}/MacOCR.py ${imagePath}` +// let stdOut = await cmdExecute(cmd); +// console.log(stdOut) // -// var output = im.matchTemplate('../assets/templates/valid_btn_template.png', 3); -// -// var matches = output.templateMatches(0.80, 1.0, 5, false); -// -// console.log(matches); -// }); - -const findWaldo = async () => { - // Load images - const originalMat = await cv.imreadAsync(`/Users/lpan/Downloads/Screenshot_2023-05-20-15-20-22-860_com.android.chrome.jpg`); - const waldoMat = await cv.imreadAsync(`${__dirname}/../assets/templates/welcome_later_btn.png`); - - // Match template (the brightest locations indicate the highest match) - const matched = originalMat.matchTemplate(waldoMat, 3); - - // Use minMaxLoc to locate the highest value (or lower, depending of the type of matching method) - const minMax = matched.minMaxLoc(); - const {maxLoc: {x, y}} = minMax; - console.log("x is " + x); - console.log("y is " + y); - // Draw bounding rectangle - originalMat.drawRectangle( - new cv.Rect(x, y, waldoMat.cols, waldoMat.rows), - new cv.Vec(0, 255, 0), - 2, - cv.LINE_8 - ); - - // Open result in new window - cv.imshow('We\'ve found Waldo!', originalMat); - cv.waitKey(); -}; - -// noinspection JSIgnoredPromiseFromCall -findWaldo(); \ No newline at end of file +// } +// // noinspection JSIgnoredPromiseFromCall +// findWaldo(); \ No newline at end of file diff --git a/src/utiles/CmdUtils.js b/src/utiles/CmdUtils.js new file mode 100644 index 0000000..b4cac8f --- /dev/null +++ b/src/utiles/CmdUtils.js @@ -0,0 +1,45 @@ +const {exec} = require("child_process"); +const baseDir = "/Users/panlei/Documents/workspace/MacOCI" + + +function cmdExecute(command) { + /** + * @param {Function} resolve A function that resolves the promise + * @param {Function} reject A function that fails the promise + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise + */ + return new Promise(function (resolve, reject) { + /** + * @param {Error} error An error triggered during the execution of the childProcess.exec command + * @param {string|Buffer} standardOutput The result of the shell command execution + * @param {string|Buffer} standardError The error resulting of the shell command execution + * @see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback + */ + exec(command, function (error, standardOutput, standardError) { + if (error) { + reject(); + + return; + } + + if (standardError) { + reject(standardError); + + return; + } + + resolve(standardOutput); + }); + }); +} + +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) + return stdOut +} + +module.exports = findText \ No newline at end of file diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index c018b82..ed77f40 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -1,6 +1,5 @@ const {SolveCaptcha, TWO_CAPTCHA_CONNECTION_FAILED} = require("./SolveCaptcha"); const ReserveResultPojo = require("../models/ReserveResultPojo"); -const BlackListContactPojo = require("../models/BlackListContactPojo"); const appointmentLogger = require("../utiles/LoggerUtils") const OCRResult = require("../models/OCRResult"); const PublishType = require("../models/PublishType"); diff --git a/src/workers/OCRChecker.js b/src/workers/OCRChecker.js index 3f15761..657846b 100644 --- a/src/workers/OCRChecker.js +++ b/src/workers/OCRChecker.js @@ -4,6 +4,7 @@ const OCRResult = require("../models/OCRResult"); const Jimp = require('jimp'); const fs = require("fs"); const {exec} = require("child_process"); +const findText = require("../utiles/CmdUtils"); function delay(delayInMs) { return new Promise(resolve => { @@ -70,19 +71,6 @@ const BRAVE_SKIP_DEFAULT_PAGE_2 = "définissant Brave" const BRAVE_SKIP_PRIVACY_PAGE = "Partagez des informations" const BRAVE_SKIP_PRIVACY_PAGE_2 = "Partagez des renseignements" -async function convertImageToWhiteBlack(image_path, device) { - console.log("convertImageToWhiteBlack for " + device.serial()) - try { - const image = await Jimp.read(image_path); - image.grayscale().write(image_path + ".wb"); - return image_path + ".wb"; - } catch (e) { - console.log(e) - return image_path - } - -} - class OCRChecker { constructor(device, contact) { @@ -98,49 +86,52 @@ class OCRChecker { async get_result() { let fileName = await this.take_screen_shot() - let screenShot = await convertImageToWhiteBlack(fileName, this.device); + // let screenShot = await convertImageToWhiteBlack(fileName, this.device); await delay(1000) try { - let result = await tesseract - .recognize(screenShot, config) + 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) + // await this.deleteFile(screenShot) return OCRResult.SUCCESS } 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) + // 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) + // 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) + // 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) - await this.deleteFile(screenShot) + // await this.deleteFile(screenShot) // if (result.includes("rac")) // return OCRResult.SLIDING_CAPTCHA_REFRESH return OCRResult.SLIDING_CAPTCHA } else if (result.includes(WELCOME_MESSAGE_FR)) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) + // 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) + // await this.deleteFile(screenShot) return OCRResult.GOOGLE_DISCONNECT } else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) + // 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) @@ -149,11 +140,11 @@ class OCRChecker { || result.includes(PAGE_OPTIMIZATION_CHROME_FR_7) ) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) + // await this.deleteFile(screenShot) return OCRResult.PAGE_OPTIMIZATION } else if (result.includes(CONFIRM_RESEND_FORM_FR)) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) + // await this.deleteFile(screenShot) return OCRResult.CONFIRM_RESEND_FORM } else if (result.includes(CLOSED_MESSAGE_FR)) { return OCRResult.CLOSED @@ -161,7 +152,7 @@ class OCRChecker { return OCRResult.TERMINAED } else if (result.includes(DIALOG_TO_SKIP)) { await this.deleteFile(fileName) - await this.deleteFile(screenShot) + // await this.deleteFile(screenShot) return OCRResult.TO_SKIP } else if (result.includes(RECAPTCHA_FAILED_FR)) { return OCRResult.TERMINAED @@ -169,19 +160,15 @@ 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) + // 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) + // await this.deleteFile(screenShot) return OCRResult.BRAVE_PRIVACY - } else if (result.includes(BRAVE_NOTIFICATION)) { - await this.deleteFile(fileName) - await this.deleteFile(screenShot) - return OCRResult.BRAVE_NOTIFICATION } 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) + // await this.deleteFile(screenShot) return OCRResult.BRAVE_PRIVACY_PUB } else { return OCRResult.TERMINAED