I really need some help, I'm new to coding and I'm trying to make a script
The script is supposed to achieve the following:
- Takes a picture
- Finds text within the image using tesseract
- Search for a specific string within the text founded
- Preforms an action based on if the specific string has been found or not
The problem I am having is that every time I run the script, it uses the previous version of the image saved, giving me the wrong result at the time.
I could really use some help.
const robot = require('robotjs')
const Jimp = require('jimp')
const Tesseract = require('tesseract.js');
const { Console, log } = require("console");
const fs = require('fs');
const {readFileSync, promises: fsPromises} = require('fs');
const { resolve } = require('path');
const myLogger = new Console({
stdout: fs.createWriteStream("normalStdout.txt")
});
const myLogger2 = new Console({
stdout: fs.createWriteStream("normalStdout2.txt")
});
//////////////////////////////////////////////////////////////////////////////////////////
function main(){
sleep(2000);
performRead();
}
//Edited function to sync instead of async - The problem is still persisting
//Edited function to include tesseractimage() in callback of writeimage()
function writeImage(){
var width = 498;
var height = 135;
var img4 = robot.screen.capture(0, 862, width, height).image;
new Jimp({data: img4, width, height}, (err, image) => {
image.write("image.png", function() {
tesseractimage();
});
});
console.log("Image 1 created");
}
function tesseractimage(){
Tesseract.recognize("image.png", 'eng')
.then(out => myLogger.log(out));
//Saves image to normalstdOut.txt
console.log("Tesseracted image")
}
function readTest(normalStdout, Viverz) {
var path = require('path');
const contents = readFileSync(path.resolve("normalStdout.txt"), 'utf-8');
const result = contents.includes("Viverz");
console.log(result);
}
//Edited performRead removing the call for tesseractimage();, it is now in writeimage();
function performRead(){
writeImage();
readTest();
}
function sleep(ms){
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
return null;
}
main();
I have tried changing functions to async functions, I've tried numerous methods, pauses, reiterations of functions multiple times, nothing saves the file until the script ends and then after it finds the correct string from the previously saved screenshot, not the new one.
Current output: Image 1 created a false Tesseracted image
Even when forcing tesseractimage()
to call before the result is published it still has the same problem of not reading the file until the script is over