use jimp
This commit is contained in:
+46
-26
@@ -1,6 +1,7 @@
|
||||
const {v4: uuidv4} = require("uuid");
|
||||
const tesseract = require("node-tesseract-ocr");
|
||||
const OCRResult = require("../models/OCRResult");
|
||||
const Jimp = require('jimp');
|
||||
|
||||
function delay(delayInMs) {
|
||||
return new Promise(resolve => {
|
||||
@@ -30,9 +31,18 @@ const WELCOME_MESSAGE_FR = "Bienvenue dans Chrome"
|
||||
const PAGE_OPTIMIZATION_CHROME_FR = "Vous pouvez changer d'avis a tout moment dans"
|
||||
const PAGE_OPTIMIZATION_CHROME_FR_2 = "Vous pouvez modifier vos options a tout moment"
|
||||
const PAGE_OPTIMIZATION_CHROME_FR_3 = "Vous pouvez effectuer des modifications"
|
||||
const PAGE_OPTIMIZATION_CHROME_FR_4 = "de nouvelles fonctionnalités"
|
||||
const PAGE_OPTIMIZATION_CHROME_FR_5 = "Avec la mesure des performance"
|
||||
const ONLINE_APPOINTMENT = "Online Appointment"
|
||||
const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi"
|
||||
const CLOSED_MESSAGE_FR = "Depuis plus de 130 ans"
|
||||
const ABOUT_BLANK = " about:blank"
|
||||
|
||||
async function convertImageToWhiteBlack(image_path) {
|
||||
const image = await Jimp.read(image_path);
|
||||
image.grayscale().write(image_path + ".wb");
|
||||
return image_path + ".wb";
|
||||
}
|
||||
|
||||
class OCRChecker {
|
||||
|
||||
@@ -46,36 +56,46 @@ class OCRChecker {
|
||||
return this.contact.passportNumber + "_" + uuid + ".png"
|
||||
}
|
||||
|
||||
|
||||
async get_result() {
|
||||
let fileName = await this.take_screen_shot()
|
||||
let result = await tesseract
|
||||
.recognize(fileName, config)
|
||||
console.log(result)
|
||||
if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR)) {
|
||||
return OCRResult.SUCCESS
|
||||
} 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)) {
|
||||
return OCRResult.BLOCKED
|
||||
} else if (result.includes(ERR_CACHE_MISS)) {
|
||||
return OCRResult.TO_REFRESH
|
||||
} else if (result.includes(CHECKING_MSG_FR)) {
|
||||
let screenShot = await convertImageToWhiteBlack(fileName);
|
||||
try {
|
||||
let result = await tesseract
|
||||
.recognize(screenShot, config)
|
||||
console.log(result)
|
||||
if (result.includes(MESSAGE_URL_VALIDATION_EN) || result.includes(MESSAGE_URL_VALIDATION_FR)) {
|
||||
return OCRResult.SUCCESS
|
||||
} 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)) {
|
||||
return OCRResult.BLOCKED
|
||||
} else if (result.includes(ERR_CACHE_MISS)) {
|
||||
return OCRResult.TO_REFRESH
|
||||
} else if (result.includes(CHECKING_MSG_FR)) {
|
||||
return OCRResult.RECHECK
|
||||
} else if (result.includes(SLIDING_CAPTCHA_FR)) {
|
||||
return OCRResult.SLIDING_CAPTCHA
|
||||
} else if (result.includes(MESSAGE_FILL_FIELD_FR)) {
|
||||
return OCRResult.FILL_FIELD
|
||||
} else if (result.includes(WELCOME_MESSAGE_FR)) {
|
||||
return OCRResult.NEED_TO_CLICK_LATE_BTN
|
||||
} else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) {
|
||||
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)) {
|
||||
return OCRResult.PAGE_OPTIMIZATION
|
||||
} else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
|
||||
return OCRResult.CONFIRM_RESEND_FORM
|
||||
} else if (result.includes(CLOSED_MESSAGE_FR)) {
|
||||
return OCRResult.CLOSED
|
||||
} else if (result.includes(ABOUT_BLANK)) {
|
||||
return OCRResult.TERMINAED
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
return OCRResult.RECHECK
|
||||
} else if (result.includes(SLIDING_CAPTCHA_FR)) {
|
||||
return OCRResult.SLIDING_CAPTCHA
|
||||
} else if (result.includes(MESSAGE_FILL_FIELD_FR)) {
|
||||
return OCRResult.FILL_FIELD
|
||||
} else if (result.includes(WELCOME_MESSAGE_FR)) {
|
||||
return OCRResult.NEED_TO_CLICK_LATE_BTN
|
||||
} else if (result.toLowerCase().includes(ONLINE_APPOINTMENT.toLowerCase())) {
|
||||
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)) {
|
||||
return OCRResult.PAGE_OPTIMIZATION
|
||||
} else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
|
||||
return OCRResult.CONFIRM_RESEND_FORM
|
||||
} else if (result.includes(CLOSED_MESSAGE_FR)) {
|
||||
return OCRResult.CLOSED
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async take_screen_shot() {
|
||||
|
||||
Reference in New Issue
Block a user