Home > Software design >  NodeJS - | Jimp Write("image.png") | not saving the image until the script ends
NodeJS - | Jimp Write("image.png") | not saving the image until the script ends

Time:12-23

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:

  1. Takes a picture
  2. Finds text within the image using tesseract
  3. Search for a specific string within the text founded
  4. 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

CodePudding user response:

One way to call tesseractimage() from writeImage() when using image.write():

new Jimp({data: img4, width, height}, (err, image) => {
    image.write("image.png", function() {
        tesseractimage();
    });
});

One way to call tesseractimage() from writeImage() when using image.writeAsync():

new Jimp({data: img4, width, height}, (err, image) => {
    image.writeAsync("image.png")
        .then((result) => {
            tesseractimage();
        }).catch((error) => {
            // Handle error
        })
});

Also remove the function call from within performRead().

For reference look under "Writing to files and buffers".

CodePudding user response:

Solved** I removed the readTest() alltogether, and restructured the tesseractimage to a new function

async function tesseracttest(){
const finalText = await Tesseract.recognize(
"image.png",
'eng',
    { logger: m => console.log(m) }
    ).then(({ data: { text } }) => {
        let extractedText = text.toString();
        let finalText = extractedText.includes("Prayer potion");
        console.log(extractedText)
        console.log(finalText);
        return finalText;
 });
}   
  • Related