pi-camera-connect is a library to capture and stream Raspberry Pi camera data directly to NodeJS.
There are many NPM modules for connecting to the Raspberry Pi camera, why use this?
- Speed: JPEG images can be captured in ~33ms using a built in MJPEG parser
- Efficient: Pictures and video streams are piped directly into Node as a
Buffer, keeping all data in memory and eliminating disk I/O - Usable: Video streams are available as
stream.Readableobjects that can be piped or listened to - Tested: Contains automated tests using Jest
- Modern: Uses the latest ESNext features and up to date development practices
- Structure: Ships with TypeScript definition files
NPM
$ npm install --save pi-camera-connect Yarn
$ yarn add pi-camera-connect Image capture:
import { StillCamera } from 'pi-camera-connect'; import * as fs from 'fs'; // Take still image and save to disk const runApp = async () => { const stillCamera = new StillCamera(); const image = await stillCamera.takeImage(); fs.writeFileSync('still-image.jpg', image); }; runApp();Video capture:
import { StreamCamera, Codec } from 'pi-camera-connect'; import * as fs from 'fs'; // Capture 5 seconds of H264 video and save to disk const runApp = async () => { const streamCamera = new StreamCamera({ codec: Codec.H264, }); const videoStream = streamCamera.createStream(); const writeStream = fs.createWriteStream('video-stream.h264'); videoStream.pipe(writeStream); await streamCamera.startCapture(); await new Promise(resolve => setTimeout(() => resolve(), 5000)); await streamCamera.stopCapture(); }; runApp();Image capture:
const { StillCamera } = require('pi-camera-connect'); const stillCamera = new StillCamera(); stillCamera.takeImage().then(image => { fs.writeFileSync('still-image.jpg', image); });Video capture:
const { StreamCamera, Codec } = require('pi-camera-connect'); const streamCamera = new StreamCamera({ codec: Codec.H264, }); const writeStream = fs.createWriteStream('video-stream.h264'); const videoStream = streamCamera.createStream(); videoStream.pipe(writeStream); streamCamera.startCapture().then(() => { setTimeout(() => streamCamera.stopCapture(), 5000); });There are 2 ways to capture an image with pi-camera-connect:
-
StillCamera.takeImage()- Slow, but higher qualityThis is equivalent to running the
raspistillcommand. Under the hood, the GPU will run a strong noise reduction algorithm to make the image appear higher quality.import { StillCamera } from 'pi-camera-connect'; const runApp = async () => { const stillCamera = new StillCamera(); const image = await stillCamera.takeImage(); // Process image... }; runApp();
-
StreamCamera.takeImage()- Fast, but lower qualityThis works by grabbing a single JPEG frame from a Motion JPEG (MJPEG) video stream . Images captured from the video port tend to have a grainier appearance due to the lack of a strong noise reduction algorithm.
Using this method, you can capture a JPEG image at more or less the frame rate of the stream, eg. 30 fps = ~33ms capture times.
import { StreamCamera, Codec } from 'pi-camera-connect'; const runApp = async () => { const streamCamera = new StreamCamera({ codec: Codec.MJPEG, }); await streamCamera.startCapture(); const image = await streamCamera.takeImage(); // Process image... await streamCamera.stopCapture(); }; runApp();
Capturing a video stream is easy. There are currently 2 codecs supported: H264 and MJPEG.
The GPU on the Raspberry Pi comes with a hardware-accelerated H264 encoder and JPEG encoder. To capture videos in real time, using these hardware encoders are required.
A standard NodeJS readable stream is available after calling createStream(). As with any readable stream, it can be piped or listened to.
import { StreamCamera, Codec } from 'pi-camera-connect'; import * as fs from 'fs'; const runApp = async () => { const streamCamera = new StreamCamera({ codec: Codec.H264, }); const videoStream = streamCamera.createStream(); const writeStream = fs.createWriteStream('video-stream.h264'); // Pipe the video stream to our video file videoStream.pipe(writeStream); await streamCamera.startCapture(); // We can also listen to data events as they arrive videoStream.on('data', data => console.log('New data', data)); videoStream.on('end', data => console.log('Video stream has ended')); // Wait for 5 seconds await new Promise(resolve => setTimeout(() => resolve(), 5000)); await streamCamera.stopCapture(); }; runApp();You can test the video by viewing it in omxplayer (ships with Raspbian):
$ omxplayer video-stream.h264 Note that this example produces a raw H264 video. Wrapping it in a video container (eg. MP4, MKV, etc) is out of the scope of this module.
StillCameraconstructor(options?: StillOptions): StillCameratakeImage(): Promise<Buffer>
StreamCameraconstructor(options?: StreamOptions): StreamCamerastartCapture(): Promise<void>stopCapture(): Promise<void>createStream(): stream.ReadabletakeImage(): Promise<Buffer>
RotationFlipCodecSensorModeExposureModeAwbModeDynamicRangeImageEffectMode
A class for taking still images. Equivalent to running the raspistill command.
Instantiates a new StillCamera class.
const stillCamera = new StillCamera({ ... });StillOptions are:
width: number- Default: Max sensor widthheight: number- Default: Max sensor heightrotation: Rotation- Default:Rotation.Rotate0flip: Flip- Default:Flip.Nonedelay: number- Default:1msshutter: number- Default: Auto calculated based on framerate (1000000µs/fps). Number is in microsecondssharpness: number- Range:-100-100; Default:0contrast: number- Range:-100-100; Default:0brightness: number- Range:0-100; Default:50saturation: number- Range:-100-100; Default:0iso: number- Range:100-800; Default: AutoexposureCompensation: number- Range:-10-10; Default:0exposureMode: ExposureMode- Default:ExposureMode.AutoawbMode: AwbMode- Default:AwbMode.AutoawbGains: [number, number]- Range:0.0-8.0; Default:undefinedanalogGain: number- Range:1.0-12.0(OV5647:1.0-8.0); Default:0digitalGain: number- Range:1.0-64.0; Default:0quality: number- Range:0-100; Default:100colorEffect: [number, number](U, V) - Range:0-255; Default:undefinedimageEffectMode: ImageEffectMode- Default:ImageEffectMode.NonedynamicRange: DynamicRange- Default:DynamicRange.OffvideoStabilization: boolean- Default:falseraw: boolean- Default:falsemeteringMode- Default:MeteringMode.Offthumbnail: [number, number, number] | false(X, Y, Q) - Default:[64, 48, 35];falseto dismiss thumbnailflickerMode- Default:FlickerMode.Offburst: boolean- Default:falseroi: [number, number, number, number](X, Y, W, H) - Range:0.0-1.0; Default:[0, 0, 1, 1](Full sensor)statistics: boolean- Default:falseexif: { [key:string]: string | number } | false- Default: Default camera values;falseto dissmis default exifgpsExif: boolean- Default:falseannotate: (number | string)[]- Default: No annotationsannotateExtra: [number, string, string](fontSize, fontColor, backgroundColor) - Default:[32, '0xff', '0x808000']
Takes a JPEG image from the camera. Returns a Promise with a Buffer containing the image bytes.
const stillCamera = new StillCamera(); const image = await stillCamera.takeImage();A class for capturing a stream of camera data, either as H264 or MJPEG.
Instantiates a new StreamCamera class.
const streamCamera = new StreamCamera({ ... });StreamOptions are:
width: number- Default: Max sensor widthheight: number- Default: Max sensor heightrotation: Rotation- Default:Rotation.Rotate0flip: Flip- Default:Flip.NonebitRate: number- Default:17000000(17 Mbps)fps: number- Default:30fpscodec: Codec- Default:Codec.H264sensorMode: SensorMode- Default:SensorMode.AutoSelectshutter: number- Default: Auto calculated based on framerate (1000000µs/fps). Number is in microsecondssharpness: number- Range:-100-100; Default:0contrast: number- Range:-100-100; Default:0brightness: number- Range:0-100; Default:50saturation: number- Range:-100-100; Default:0iso: number- Range:100-800; Default: AutoexposureCompensation: number- Range:-10-10; Default:0exposureMode: ExposureMode- Default:ExposureMode.AutoawbMode: AwbMode- Default:AwbMode.AutoawbGains: [number, number]- Range:0.0-8.0; Default:undefinedanalogGain: number- Range:1.0-12.0(OV5647:1.0-8.0); Default:0digitalGain: number- Range:1.0-64.0; Default:0colorEffect: [number, number](U, V) - Range:0-255; Default:undefinedimageEffectMode: ImageEffectMode- Default:ImageEffectMode.NonedynamicRange: DynamicRange- Default:DynamicRange.OffvideoStabilization: boolean- Default:falsemeteringMode- Default:MeteringMode.OffflickerMode- Default:FlickerMode.Offroi: [number, number, number, number](X, Y, W, H) - Range:0.0-1.0; Default:[0, 0, 1, 1](Full sensor)statistics: boolean- Default:falseannotate: (number | string)[]- Default: No annotationsannotateExtra: [number, string, string](fontSize, fontColor, backgroundColor) - Default:[32, '0xff', '0x808000']
Begins the camera stream. Returns a Promise that is resolved when the capture has started.
Ends the camera stream. Returns a Promise that is resolved when the capture has stopped.
Creates a readable stream of video data. There is no limit to the number of streams you can create.
Be aware that, as with any readable stream, data will buffer in memory until it is read. If you create a video stream but do not read its data, your program will quickly run out of memory.
Ways to read data so that it does not remain buffered in memory include:
- Switching the stream to 'flowing' mode by calling either
resume(),pipe(), or attaching a listener to the'data'event - Calling
read()when the stream is in 'paused' mode
See the readable stream documentation for more information on flowing/paused modes.
const streamCamera = new StreamCamera({ codec: Codec.H264, }); const videoStream = streamCamera.createStream(); await streamCamera.startCapture(); videoStream.on('data', data => console.log('New video data', data)); // Wait 5 seconds await new Promise(resolve => setTimeout(() => resolve(), 5000)); await streamCamera.stopCapture();Takes a JPEG image frame from an MJPEG camera stream, resulting in very fast image captures. Returns a Promise with a Buffer containing the image bytes.
Note: StreamOptions.codec must be set to Codec.MJPEG, otherwise takeImage() with throw an error.
const streamCamera = new StreamCamera({ codec: Codec.MJPEG, }); await streamCamera.startCapture(); const image = await streamCamera.takeImage(); await streamCamera.stopCapture();Image rotation options.
Rotation.Rotate0Rotation.Rotate90Rotation.Rotate180Rotation.Rotate270
import { Rotation } from 'pi-camera-connect';Image flip options.
Flip.NoneFlip.HorizontalFlip.VerticalFlip.Both
import { Flip } from 'pi-camera-connect';Stream codec options.
Codec.H264Codec.MJPEG
import { Codec } from 'pi-camera-connect';Stream sensor mode options.
SensorMode.AutoSelectSensorMode.Mode1SensorMode.Mode2SensorMode.Mode3SensorMode.Mode4SensorMode.Mode5SensorMode.Mode6SensorMode.Mode7
import { SensorMode } from 'pi-camera-connect';These are slightly different depending on the version of Raspberry Pi camera you are using.
| Mode | Size | Aspect Ratio | Frame rates | FOV | Binning |
|---|---|---|---|---|---|
| 0 | automatic selection | ||||
| 1 | 1920x1080 | 16:9 | 1-30fps | Partial | None |
| 2 | 2592x1944 | 4:3 | 1-15fps | Full | None |
| 3 | 2592x1944 | 4:3 | 0.1666-1fps | Full | None |
| 4 | 1296x972 | 4:3 | 1-42fps | Full | 2x2 |
| 5 | 1296x730 | 16:9 | 1-49fps | Full | 2x2 |
| 6 | 640x480 | 4:3 | 42.1-60fps | Full | 2x2 plus skip |
| 7 | 640x480 | 4:3 | 60.1-90fps | Full | 2x2 plus skip |
| Mode | Size | Aspect Ratio | Frame rates | FOV | Binning |
|---|---|---|---|---|---|
| 0 | automatic selection | ||||
| 1 | 1920x1080 | 16:9 | 0.1-30fps | Partial | None |
| 2 | 3280x2464 | 4:3 | 0.1-15fps | Full | None |
| 3 | 3280x2464 | 4:3 | 0.1-15fps | Full | None |
| 4 | 1640x1232 | 4:3 | 0.1-40fps | Full | 2x2 |
| 5 | 1640x922 | 16:9 | 0.1-40fps | Full | 2x2 |
| 6 | 1280x720 | 16:9 | 40-90fps | Partial | 2x2 |
| 7 | 640x480 | 4:3 | 40-200fps* | Partial | 2x2 |
*For frame rates over 120fps, it is necessary to turn off automatic exposure and gain control using -ex off. Doing so should achieve the higher frame rates, but exposure time and gains will need to be set to fixed values supplied by the user.
| Mode | Size | Aspect Ratio | Frame rates | FOV | Binning |
|---|---|---|---|---|---|
| 0 | automatic selection | ||||
| 1 | 2028x1080 | 169:90 | 0.1-50fps | Partial | 2x2 binned |
| 2 | 2028x1520 | 4:3 | 0.1-50fps | Full | 2x2 binned |
| 3 | 4056x3040 | 4:3 | 0.005-10fps | Full | None |
| 4 | 1332x990 | 74:55 | 50.1-120fps | Partial | 2x2 binned |
Exposure mode options.
ExposureMode.OffExposureMode.AutoExposureMode.NightExposureMode.NightPreviewExposureMode.BacklightExposureMode.SpotlightExposureMode.SportsExposureMode.SnowExposureMode.BeachExposureMode.VeryLongExposureMode.FixedFpsExposureMode.AntiShakeExposureMode.Fireworks
import { ExposureMode } from 'pi-camera-connect';White balance mode options.
AwbMode.OffAwbMode.AutoAwbMode.SunAwbMode.CloudAwbMode.ShadeAwbMode.TungstenAwbMode.FluorescentAwbMode.IncandescentAwbMode.FlashAwbMode.HorizonAwbMode.GreyWorld
import { AwbMode } from 'pi-camera-connect';Image Effect options.
ImageEffectMode.NoneImageEffectMode.NegativeImageEffectMode.SolariseImageEffectMode.SketchImageEffectMode.DenoiseImageEffectMode.EmbossImageEffectMode.OilPaintImageEffectMode.HatchImageEffectMode.GPenImageEffectMode.PastelImageEffectMode.WatercolourImageEffectMode.FilmImageEffectMode.BlurImageEffectMode.SaturationImageEffectMode.ColourSwapImageEffectMode.WashedOutImageEffectMode.PosteriseImageEffectMode.ColourPointImageEffectMode.ColourBalanceImageEffectMode.Cartoon
import { ImageEffectMode } from 'pi-camera-connect';Dynamic Range options.
DynamicRange.OffDynamicRange.LowDynamicRange.MediumDynamicRange.High
import { DynamicRange } from 'pi-camera-connect';Dynamic Range options.
MeteringMode.AverageMeteringMode.SpotMeteringMode.BacklitMeteringMode.Matrix
import { MeteringMode } from 'pi-camera-connect';Dynamic Range options.
FlickerMode.OffFlickerMode.AutoFlickerMode.Frq50hzFlickerMode.Frq60hz
import { FlickerMode } from 'pi-camera-connect';