【问题标题】:SyntaxError: Unexpected end of JSON input: OpenALPR using Node and JavascriptSyntaxError:JSON 输入意外结束:OpenALPR 使用 Node 和 Javascript
【发布时间】:2020-08-17 01:20:37
【问题描述】:

当对var data 声明使用try/catch 语句时,即使有,程序也会输出'No License Plate Found'。如果删除 try/catch 语句,当框架中没有牌照时,程序会完美打印,但是当有牌照时,我会收到 SyntaxError。 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)

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

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

【问题讨论】:

    标签: javascript node.js openalpr alpr


    【解决方案1】:
    1. 数据已经是解析过的。错误正在抛出,因为您正在尝试解析 再来一次。

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

    【讨论】:

    • 你会推荐什么替代的数据解析?
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2020-10-10
    • 2021-05-01
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多