2

当框架中没有牌照时,程序打印完美,但如果有,我得到SyntaxError. Node.js 和 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) {

                var data = JSON.parse(stdout);

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

错误及其发生的位置是:

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":[]}
4

1 回答 1

0

JSON 末尾有一个空格。

try {
   var data = JSON.parse(stdout.trim())
} catch (e) {
   console.error('Failed to Parse JSON!', e)
}
于 2020-05-01T18:11:35.063 回答