繁体   English   中英

Node.js - | 吉普写(“image.png”)| 在脚本结束之前不保存图像

[英]NodeJS - | Jimp Write("image.png") | not saving the image until the script ends

我真的需要一些帮助,我是编码新手,我正在尝试制作脚本

该脚本应该实现以下目标:

  1. 拍照
  2. 使用 tesseract 在图像中查找文本
  3. 在创建的文本中搜索特定字符串
  4. 根据是否找到特定字符串执行操作

我遇到的问题是,每次运行脚本时,它都会使用保存的图像的先前版本,当时给我错误的结果。

我真的需要一些帮助。

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();

我尝试过将函数更改为异步函数,我尝试过多种方法、暂停、多次重复函数,在脚本结束之前没有保存文件,然后从之前保存的屏幕截图中找到正确的字符串,而不是新的.

当前输出:Image 1 创建了一个虚假的 Tesseracted 图像

即使在结果发布之前强制调用tesseractimage()时,它仍然存在相同的问题,即在脚本结束之前不读取文件

使用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()中删除函数调用。

作为参考,请查看“写入文件和缓冲区”

已解决** 我将 readTest() 全部删除,并将 tesseractimage 重组为一个新函数

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;
 });
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM