javascript - 语法错误: Unexpected end of JSON input: OpenALPR using Node and Javascript

标签 javascript node.js openalpr automatic-license-plate-recognition

当使用 try/catch 语句进行 var data 声明时,即使有车牌,程序也会输出“No License Plate Found”。如果删除 try/catch 语句,则当帧中没有车牌时,程序可以完美打印,但是当帧中有车牌时,我会收到语法错误。 Node.js 和 OpenALPR 已安装。照片也已成功拍摄。需要 OpenALPR 专家。

const PiCamera = require('pi-camera');


function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
}

setInterval(function() {

    var path = './' + getRandomInt(500) + '.jpg';

    const myCamera = new PiCamera({
        mode: 'photo',
        output: path,
        width: 1920,
        height: 1080,
        nopreview: false,
    });


    myCamera.snap()
        .then((result) => {

            var exec = require('child_process').exec;
            var cmd = 'alpr -c eu -n 1 --json ' + path;

            exec(cmd, function(error, stdout, stderr) {

            console.log(stdout);

            try {
               var data = JSON.parse(stdout.trim())
            } catch (e) {
               console.error('Failed to Parse JSON!', e)
            }

                if (data && data.results &&  data.results.length > 0) {
                    console.log(data.results[0].plate);
                } else {
                    console.log("\n\n\nNo license plate found.\n\n");
                }
            });

            console.log(result);

        })
        .catch((error) => {
            console.log(error);
        });

}, 2e3);

没有 try/catch 时的错误及其发生位置:

  undefined:1
  SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /home/pi/Project/project.js:28:33
    at ChildProcess.exithandler (child_process.js:301:5)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:970:16)
    at Socket.stream.socket.on (internal/child_process.js:389:11)
    at Socket.emit (events.js:189:13)
    at Pipe._handle.close (net.js:600:12)

var data 声明之前输出 console.log(stdout);:

{"version":2,"data_type":"alpr_results","epoch_time":1588355061888,"img_width":1920,"img_height":1080,"processing_time_ms":1447.340698,"regions_of_interest":[],"results":[]}

最佳答案

  1. 数据已经是经过解析的数据。抛出错误是因为您正在尝试解析 再来一次。

  2. .trim() 是字符串操作。你正在处理的是一个 json 对象。所以那就是 也无效。 JSON.stringify(stdout).trim() 是有效的。

关于javascript - 语法错误: Unexpected end of JSON input: OpenALPR using Node and Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61561339/

相关文章:

javascript - Javascript 中的原型(prototype)和非原型(prototype)继承(区别)

node.js - 在 ejs < 2.5.5 中检测到已知的中等严重性安全漏洞

opencv - 无法获取 RTSP 流 - 不匹配

c++ - 使用 C++ 设置 OpenALPR, undefined reference 。

javascript - Firebase 函数

javascript - 需要在单击时将本地数组传递给函数

javascript - 运行 tabs.executeScript : The tab was closed 时未经检查的 runtime.lastError

javascript - Node.JS:写入文件时 Promise 产生 null

javascript - 错误 : Can't set headers after they are sent. RabbitMQ 干扰 NodeJS 响应

opencv - 在一行OpenALPR中识别字符高度不同的俄文版