NodeJS 节点|Jimp写入(“图像. png”)|脚本结束前不保存图像

vatpfxk5  于 2022-12-22  发布在  Node.js
关注(0)|答案(1)|浏览(153)

我真的需要一些帮助,我是新的编码和我试图使一个脚本,它需要一张图片,找到文本从图像使用tesseract,搜索一个特定的字符串从tesseract图像,然后执行一个动作的基础上,如果字符串被发现。我的问题是,每次我运行脚本,它使用以前版本的图像保存,当时给了我错误的结果,我真的需要一些帮助。

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 will find the correct string from the 
previously saved screenshot, not the new one.

// Current output: 
Image 1 created
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 it over
rmbxnbpk

rmbxnbpk1#

使用image.write()时从writeImage()调用tesseractimage()的一种方法:

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

使用image.writeAsync()时,从writeImage()调用tesseractimage()的一种方法是:

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

还要从performRead()中删除函数调用。
有关参考,请查看"Writing to files and buffers"

相关问题