Coder Social home page Coder Social logo

node-raspicam's Introduction

node-raspicam

A Node.js-based controller module for the Raspberry Pi camera.

Note: This should work well for photo and timelapse, video has yet to be tested but may work as well.

To Install

npm install raspicam

To Use

Require raspicam in your node app, then used the exposed constructor to create a RaspiCam object that you can use to take photos, start a timelapse, or record video.

var RaspiCam = require("raspicam");

var camera = new RaspiCam({ opts });

//to take a snapshot, start a timelapse or video recording
camera.start( );

//to stop a timelapse or video recording
camera.stop( );

//listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
	//do stuff
});

//listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){ 
	//do stuff
});

//listen for the "stop" event triggered when the stop method was called
camera.on("stop", function(){
	//do stuff
});

//listen for the process to exit when the timeout has been reached
camera.on("exit", function(){
	//do stuff
});

RaspiCam Constructor

The RaspiCam() constructor creates an object that can then be triggered to take a snapshot or start a timelapse or video recording. The constructor options mirror those offered by the raspistill and raspivideo commands provided by the Raspberry Pi Camera API.

RaspiCam Constructor Options for Photo and Timelapse

Required

  • mode : String - "photo" or "timelapse"
  • output : String - the path and filename where you want to store the photos (use sprintf-style variables, like %d, for incrementing timelapse photos)

Optional

  • log : Function - logger function, defaults to console.log
Image parameter commands
Parameters with Values
  • w, width : Integer or String - Set image width
  • h, height : Integer or String - Set image height
  • q, quality : Integer or String - Set jpeg quality <0 to 100>
  • t, timeout : Integer or String - Time (in ms) before takes picture and shuts down (if not specified, set to 5s)
  • th, thumb : String - Set thumbnail parameters (x:y:quality)
  • e, encoding : String - Encoding to use for output file (jpg, bmp, gif, png)
  • x, exif : String - EXIF tag to apply to captures (format as 'key=value')
  • tl, timelapse : Integer or String - Timelapse mode. Takes a picture every ms
Flags
  • r, raw : Add raw bayer data to jpeg metadata
  • v, verbose : Output verbose information during run
  • d, demo : Run a demo mode (cycle through range of camera options, no capture)
Preview parameter commands
Flags

Note: I've kept these in for completeness, but I'm not sure how they will be useful.

  • p, preview : Preview window settings <'x,y,w,h'>
  • f, fullscreen : Fullscreen preview mode
  • op, opacity : Preview window opacity (0-255)
  • n, nopreview : Do not display a preview window
Image parameter commands
Parameters with Values
  • sh, sharpness : Integer or String - Set image sharpness (-100 to 100)
  • co, contrast : Integer or String - Set image contrast (-100 to 100)
  • br, brightness : Integer or String - Set image brightness (0 to 100)
  • sa, saturation : Integer or String - Set image saturation (-100 to 100)
  • ISO, ISO : Integer or String - Set capture ISO
  • ev, ev : Integer or String - Set EV compensation
  • ex, exposure : String - Set exposure mode (off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks)
  • awb, awb : String - Set AWB mode (off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon)
  • ifx, imxfx : String - Set image effect (none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalance,cartoon)
  • cfx, colfx : String - Set colour effect (U:V)
  • mm, metering : String - Set metering mode (average,spot,backlit,matrix)
  • rot, rotation : Integer or String - Set image rotation (0-359)
Flags
  • vs, vstab : Turn on video stablisation
  • hf, hflip : Set horizontal flip
  • vf, vflip : Set vertical flip

RaspiCam Constructor Options for Video

Required

  • mode : String - "video"
  • output : String - the path and filename where you want to store the video (to write to stdout, use '-')

Optional

  • log : Function - logger function, defaults to console.log
Image parameter commands
Parameters with Values
  • w, width : Integer or String - Set image width (Default is 1920)
  • h, height : Integer or String - Set image height (Default is 1080)
  • b, bitrate : Integer or String - Set bitrate. Use bits per second (e.g. 10MBits/s would be 10000000)
  • t, timeout : Integer or String - Time (in ms) to capture for. If not specified, set to 5s. Zero to disable
  • fps, framerate : Integer or String - Specify the frames per second to record
  • g, intra : Integer or String - Specify the intra refresh period (key frame rate/GoP size)
Flags
  • v, verbose : Output verbose information during run
  • d, demo : Run a demo mode (cycle through range of camera options, no capture)
  • e, penc : Display preview image after encoding (shows compression artifacts)
Preview parameter commands
Flags

Note: I've kept these in for completeness, but I'm not sure how they will be useful.

  • p, preview : Preview window settings <'x,y,w,h'>
  • f, fullscreen : Fullscreen preview mode
  • op, opacity : Preview window opacity (0-255)
  • n, nopreview : Do not display a preview window
Image parameter commands
Parameters with Values
  • sh, sharpness : Integer or String - Set image sharpness (-100 to 100)
  • co, contrast : Integer or String - Set image contrast (-100 to 100)
  • br, brightness : Integer or String - Set image brightness (0 to 100)
  • sa, saturation : Integer or String - Set image saturation (-100 to 100)
  • ISO, ISO : Integer or String - Set capture ISO
  • ev, ev : Integer or String - Set EV compensation
  • ex, exposure : String - Set exposure mode (off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks)
  • awb, awb : String - Set AWB mode (off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon)
  • ifx, imxfx : String - Set image effect (none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalance,cartoon)
  • cfx, colfx : String - Set colour effect (U:V)
  • mm, metering : String - Set metering mode (average,spot,backlit,matrix)
  • rot, rotation : Integer or String - Set image rotation (0-359)
Flags
  • vs, vstab : Turn on video stablisation
  • hf, hflip : Set horizontal flip
  • vf, vflip : Set vertical flip

RaspiCam Object Methods

RaspiCam.start( )

Depending on the mode, this will either take a snapshot ("photo"), start a timelapse ("timelapse") or start a video recording ("video").

You can only call start() once on a RaspiCam object, as the same physical camera cannot do multiple captures at once.

Returns false if any errors, otherwise returns true.

Emits the following signals:

  • start with payload (err, timestamp) when the capture process was started by a .start() method call
  • read with payload (err, timestamp, filename) when a new file is saved (very useful for timelapses)
  • exit with payload (timestamp) when the capture process exits via timeout

RaspiCam.stop( )

This stops any ongoing camera process.

Returns true if it stopped a process, otherwise returns false.

Emits the following signals:

  • stop with payload (err, timestamp) when the capture process was stopped by a .stop() method call

RaspiCam.set( opt, value )

This is a setter - it sets any option you give it. Opt must be a string (eg. "width").

RaspiCam.get( opt )

This is a getter - it returns any option you give it. Opt must be a string (eg. "width").

To develop

Add your tests in test/raspicam.js and start your developments.

To check syntax and start tests, run

node_modules/.bin/grunt

or (if grunt and grunt-cli are installed globally)

grunt

node-raspicam's People

Contributors

darfink avatar dsteinborn avatar jean-beru avatar lynnaloo avatar markelarizaga avatar mattmcneeney avatar orhankutlu avatar rutkai avatar troyth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-raspicam's Issues

How to...?

$ node raspicam.js photo /home/user/
Error: RaspiCam: must define mode and output

Cannot read property 'toString' of undefined

Here is what I am doing:
ivport.camera = new RaspiCam({
mode: IVPort.config.mode,
encoding: IVPort.config.encoding,
output: ivport.filepath,
t: 10,
rot: IVPort.config.rot
});

And I am getting an error:
node_modules/raspicam/lib/raspicam.js:218
if( this.opts[opt].toString() != "true" && this.opts[opt].toString() != "false"){
^

TypeError: Cannot read property 'toString' of undefined
at RaspiCam.start (/home/pi/pepsi-ingestor/pepsi-demo/node_modules/ivport/node_modules/raspicam/lib/raspicam.js:218:25)
at [object Object]._onTimeout (/home/pi/pepsi-ingestor/pepsi-demo/node_modules/ivport/index.js:129:17)
at Timer.listOnTimeout (timers.js:92:15)

Any idea why?

Event "changed" on video

Hi,
I'm trying node-raspicam for record video. Apparently it's working correctly, but when I'm recording appears a lot of logs messages in console that says "raspicam::watcher::event changed".
I think while raspicam is recording, the output file is changing, and it's produces that watcher detect that changes continuously. Maybe it can be filtered when the mode is video.

Thanks

event "read"

Hi troyth, raspicam is cool,

Follow the source-code, you only emit "read" went file created (not temp)

// only emit read event if it is not a temporary file
        if (filename.indexOf('~') === -1) {
          self.emit( "read", null, new Date().getTime(), filename );
        }

However, the raspicam.js from npm package emit "read" lack of check temp:

if(event === "rename"){
      console.log('raspicam::watcher::event ' + event);
      self.emit( "read", null, new Date().getTime(), filename );
    }else{

My cmd run install raspicam: "npm install raspicam", should i change it to dev, or ?

Use `debug` option for `console` output

I use the console a lot during testing, and raspicam fires a lot of events and logs them out to the console. I'd love to see this behind a debug flag, even if that flag were set to true by default.

Not possible to define multiple --exif attributes

Thanks for a nice module!
To set multiple EXIF attributes you must define them multiple time like this:

raspistill --exif IFD0.Copyright=XXX --exif IFD0.Artist=YYY -o out.jpg

Attributes in this module are passed as javascript object so it's not possible to define them multiple times. You should allow to specify array, like so:

new RaspiCam({
    exif: ['IFD0.Copyright=XXX', 'IFD0.Artist=YYY']
})

or some other way. Good luck!

Such a great module!

@troyth I'd love to help with issues/releases on this module if you want additional maintainers? I realize open-source can be really time-consuming. I've been using this for a few of my Raspberry Pi projects and I love it and would like to see it get even better.

Get images as Buffer

I'd be interested in getting the video as a NodeJS Buffer stream (or any kind of callbacks). Since I want to manipulate the video taken by the camera, saving 30 images per second on the SD card, and deleting them just after would just destroy it.
Thanks

Photo exit event never fires.

Hi @troyth @dsteinborn @markelarizaga @mattmcneeney !

Not sure if this repo is still being maintained, but I've run into an issue.

Using the example code in "eg/photo.js" for capturing a photo, but the "exit" event never seems to fire. In fact, it seems to be stuck in some kind of loop. Note that I had to run control-C to kill the process manually.

Output:
screen shot 2016-03-23 at 00 18 51

Code:

var RaspiCam = require("../lib/raspicam");

var camera = new RaspiCam({
    mode: "photo",
    output: "./photo/image.jpg",
    encoding: "jpg",
    timeout: 0 // take the picture immediately
});

camera.on("started", function( err, timestamp ){
    console.log("photo started at " + timestamp );
});

camera.on("read", function( err, timestamp, filename ){
    console.log("photo image captured with filename: " + filename );
});

camera.on("exit", function( timestamp ){
    console.log("photo child process has exited at " + timestamp );
});

camera.start();

Notice: using Raspberry Pi 2, running Raspbian, and using official Raspberry Pi camera, with latest version of this node module Raspicam.

Could not make it work so far

Some hand would be pretty. Here is my test code based on the documentation:

var RaspiCam = require("raspicam");

var camera = new RaspiCam({ mode:"photo", output:"testfile" });

//to take a snapshot, start a timelapse or video recording
camera.start( );
console.log("start")

//to stop a timelapse or video recording
camera.stop( );

//listen for the "started" event triggered when the start method has been successfully initiated
camera.on("started", function(){ 
    console.log("started")
});

//listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, filename){ 
    console.log("saved")
});

//listen for the process to exit when the timeout has been reached
camera.on("exited", function(){
    console.log("timeout")
});

I do not see any files anywhere. "saved" console.log never triggers.
I'm on raspberry pi 2 with a noircam module. Camera is enabled and raspicam command normally works (without npm)

Error: RaspiCam: must define mode and output

I got this error message using this code

Code

var RaspiCam = require("raspicam");

var camera = new RaspiCam({ mode: "photo", w: 180 });

//to take a snapshot, start a timelapse or video recording
camera.start( );

//to stop a timelapse or video recording
camera.stop( );

//listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
    //do stuff
});

//listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){ 
    //do stuff
});

//listen for the "stop" event triggered when the stop method was called
camera.on("stop", function(){
    //do stuff
});

//listen for the process to exit when the timeout has been reached
camera.on("exit", function(){
    //do stuff
});

Error message

Error: RaspiCam: must define mode and output
/Users/ZERO/node_modules/raspicam/lib/raspicam.js:182
    this.watcher.close();
                ^

TypeError: Cannot read property 'close' of undefined
    at RaspiCam.watchDirectory (/Users/ZERO/node_modules/raspicam/lib/raspicam.js:182:17)
    at RaspiCam.start (/Users/ZERO/node_modules/raspicam/lib/raspicam.js:209:8)
    at Object.<anonymous> (/Users/ZERO/Desktop/experiment/camera.js:6:8)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

Please advice. Thank you.

webcam

Hi, can I use your code for webcam usb (/dev/video0)?

Unable to disable preview - n, nopreview not working

I cant seem to get the the nopreview flag to work.

Everything else works fine except i can't stop it from throwing a preview window for every capture. Is there an example somewhere on how this can be dealt with?

Documentation states: "n, nopreview : Do not display a preview window"

Even though it also states "Note: I've kept these in for completeness, but I'm not sure how they will be useful." - I've found a use for hiding the preview. Help appreciated.

Raspberry Pi 3 B+
Node -v : 10.16.0

Cant get the standard code example to work on rpi3

I'm trying to get the standards code example to work.

var RaspiCam = require("raspicam");

var camera = new RaspiCam({ 
    mode: 'photo',
    output: './img/photo.png'
});

//listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
	console.log('Camera started!');
});

//listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){ 
	console.log('File saved: "' + filename + '"');
});

//listen for the "stop" event triggered when the stop method was called
camera.on("stop", function(){
	console.log('Camera stoped!');
});

//listen for the process to exit when the timeout has been reached
camera.on("exit", function(){
    console.log('Exiting program...');
});

camera.start();

setTimeout(() => camera.stop(), 500); // Stop after 0.5s

It creates the img folder, however it doesn't output a file, nor does it fire the event for reading data.

Output:

calling....
/opt/vc/bin/raspistill --output ./img/photo.png --width 640 --height 480
Camera started!
Camera stoped!
Exiting program...

However i can get the util to work the "normal" way: raspistill -o cam.jpg

How to apply vflip parameter?

Hello,

How can I apply vf and vflip parameter to option to flip the picture? I tried this, but it does not work.
var camera = new RaspiCam({ mode : "photo", output : "lol1.jpg", flags : "vf"});

Not getting photo

Here is my code. I've tried being specific about path to file, pausing between start/stop. Not seeing all the events get fired. It does give me the command it is going to run, which when I run in CLI I get the image I want:

var Raspicam = require('raspicam');


var camera = new Raspicam({
	mode: "photo", 
	verbose: true,
	timeout: 0,
	output: "/home/pi/Camera/picture.jpg"
});


camera.start();
console.log("about to wait...");
setTimeout(function() {
	console.log("done waiting");
	camera.stop();
}, 10000);


camera.on("read", function(err,timestamp,filename) {
	console.dir(err);
	console.dir(timestamp);
	console.log(filename);
});


camera.on("start", function() {
	console.log("Started!");
});


camera.on("exit", function() {
	console.log("Done!");
});

Question on getting stills after camera.start();

i'm very new to node.js, so please bear with me in my uneducated queries.

After require, declaring, and starting a camera object, how do i actually get access to a photo to pass to a web page. Do i access it via the camera object? any help on this is greatly appreciated.

Thanks,
Shane

Not able to take a picture

Here it's the code:

var RaspiCam = require("raspicam");

var camera = new RaspiCam({mode: "photo", delay: 0, quality: 100, filepath: "/home/pi/"});

camera.start()

USB Camera

Can this node module works with usb camera or it should have raspberry pi camera

this code not work. lost my time. :-(

raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event rename
photo image captured with filename: image.jpg
raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event rename
photo image captured with filename: image.jpg
raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event change
raspicam::watcher::event rename
photo image captured with filename: image.jpg~
raspicam::watcher::event rename
photo image captured with filename: image.jpg
raspicam::watcher::event rename

read event fires twice.

Somewhy the 'read' function is fired multiple times. It will fire for eevery photo that has been taken, so every new photo increases the number of times 'read will be called'

Can't get The Camera to Capture Images (Timelapse)

My camera class:

RaspiCam = require('raspicam')

logger = require('../logger/logger.js').getLogger()

class Camera
  constructor: (options) ->
    outputFolder = process.env.CAMERA_FOLDER || '/home/pi/app/%d'
    cameraOptions = {
      mode: 'timelapse'
      output: outputFolder
      encoding: "jpg"
      timeout: 0
    }
    console.log "outputFolder", outputFolder
    camera = new RaspiCam(cameraOptions)
    console.log camera
  #--------------------------- Events ----------------------------------
    camera.on 'start', ->
      console.log "camera started"
      return

    camera.on 'read', (err, timestamp, filename) ->
      console.log err, timestamp, filename
      return

    camera.on 'stop', ->
      console.log "camera stopped"
      return

    camera.on 'exit', ->
      console.log "camera exited"
      return
    return

#--------------------------- Start Camera ----------------------------------
    camera.start()

    setTimeout ()->
      camera.stop()
    , 10000

#/////////////////////////////////////////////////////////////////////////////////////////////////////
module.exports = Camera

I create a new camera instance after booting and it shows me from the above console.log:

RaspiCam {
  opts: 
   { mode: 'timelapse',
     output: '/home/pi/app/%d',
     encoding: 'jpg',
     timeout: 0,
     width: 640,
     height: 480 },
  filename: '%d',
  filepath: '/home/pi/app/',
  child_process: null,
  watcher: null }

I enabled the camera in raspi-config and I'm able to take an image from the cli with raspistill -o image.jpg. There are no uncaught exceptions or errors displayed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.