NodeJS - Object Undefined?

时间:2018-01-15 18:10:11

标签: node.js camera raspberry-pi

I've got a raspberry pi v3 and am using node-raspistill and pixel-getter npm packages.

For some reason, when I get the pixels, I can print the full object, I can also access the object occasionally. I need to convert the object into a csv string to pass to processing down stream. I was wondering if anybody might be able to tell why my object consistently returns back: 'cannot read property '0' of undefined.

'use strict';

const Raspistill = require('node-raspistill').Raspistill;
const pixel_getter = require('pixel-getter')

const camera = new Raspistill({ verticalFlip: true,
                                horizontalFlip: true,
                                width: 50,
                                height: 50,
                                encoding: 'jpg',
                                noFileSave: true,
                                time: 5 });
                                
function PixelsToArray(pixels) {
  var arr_pixels = '';
  //console.log(pixels);
  for(var i =0; i < 50; i++){
    for(var j=0; j < 50; j++){
      p = pixels[i][j];
      arr_pixels += String(p.r) + ',';
      arr_pixels += String(p.g) + ',';
      arr_pixels += String(p.b) + ',';
    }
  }
  arr_pixels = arr_pixels.slice(0, -1); //Slice off last comma.
  return arr_pixels;
}

function TakePictureLoop() {
    console.log('taking picture');
    camera.takePhoto().then((photo) => {
        console.log('got photo');
        pixel_getter.get(photo, function(err, pixels) {
            console.log('got pixels');
           // console.log(pixels);
           // console.log(pixels[0][0].r);
            pix = PixelsToArray(pixels);
            console.log(pix);
            TakePictureLoop();
        });
    });
}

TakePictureLoop();

the line p = pixels[i][j]; is failing for me. console.log(pixels) works great; shows an object of the structure I want.

Any tips or hints on what is going on here or how to resolve?


UPDATE --> I added some logging to print i & j and then the values it is getting back using the code:

  for(var i =0; i < width; i++){
    for(var j=0; j < height; j++){
      console.log(String(i) + ' ' + String(j));
      console.log(pixels[i][j]);
    }
  }

I get output as below:

pi@campi:~/sample $ node app.js
taking picture
got photo
got pixels
0 0
{ r: 113, g: 85, b: 95 }
0 1
{ r: 113, g: 85, b: 95 }
0 2
{ r: 114, g: 87, b: 94 }
0 3
{ r: 114, g: 87, b: 94 }
0 4
{ r: 114, g: 87, b: 94 }
0 5

etc etc... Eventually, when j hits 50 and it is time to flip i to 1 from 0, (consistently) it hits "got pixels" again. But it never indicates it is attempting to 'take picture'.

{ r: 112, g: 92, b: 103 }
0 46
{ r: 115, g: 92, b: 106 }
0 47
{ r: 116, g: 93, b: 107 }
0 48
{ r: 117, g: 94, b: 108 }
0 49
{ r: 118, g: 95, b: 109 }
1 0
got pixels
0 0
/home/pi/sample/app.js:47
      console.log(pixels[i][j]);
                    ^

TypeError: Cannot read property '0' of undefined
at /home/pi/sample/app.js:47:25
at JPGGetter.parse (/home/pi/sample/node_modules/pixel-
getter/lib/jpg.js:59:9)
at /home/pi/sample/node_modules/pixel-getter/index.js:31:16
at doNTCallback0 (node.js:417:9)
at process._tickCallback (node.js:346:13)

It appears to be behaving as if it is entering the handler for taking the picture all over again and the currently processing handler gets nuked some how.

0 个答案:

没有答案