1

The program prints perfectly when there is no License Plate in the frame, but when there is, I get the SyntaxError. Node.js and OpenALPR is installed. Photos are successfully being taken also.

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

The error and where it occurs is:

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)

Output of console.log(stdout); before var data declaration:

{"version":2,"data_type":"alpr_results","epoch_time":1588355061888,"img_width":1920,"img_height":1080,"processing_time_ms":1447.340698,"regions_of_interest":[],"results":[]}
RO'D
  • 13
  • 6
  • 1
    The JSON being received from stdout is not valid JSON. I suggest you add a console.log(stdout) before the var data line and see what it looks like. – Michael Hobbs May 01 '20 at 17:35
  • {"version":2,"data_type":"alpr_results","epoch_time":1588355061888,"img_width":1920,"img_height":1080,"processing_time_ms":1447.340698,"regions_of_interest":[],"results":[]} – RO'D May 01 '20 at 17:46

1 Answers1

0

There is a space at the end of the JSON.

try {
   var data = JSON.parse(stdout.trim())
} catch (e) {
   console.error('Failed to Parse JSON!', e)
}
Michael Hobbs
  • 1,663
  • 1
  • 15
  • 26
  • Now when capturing a license plate, it says there is no license plate. – RO'D May 02 '20 at 13:20
  • Take a look at the sample data you provided. The results are empty thus your code is working as expected. There could be another issue unrelated to this issue. Assuming this fixed the original issue you should accept the answer and open a new issue for the new issue ie no/empty results. You are going to need someone with OpenALPR experience which is not me. – Michael Hobbs May 02 '20 at 13:47