As my tinkering with node.js continues, this time I was looking for access to webcam, and process image frames.
There are few libraries such as ffmpeg, vlc and opencv are available using which webcam access can be written as node’s native addon. I have used opencv for this task.
Here’s what happens
- Client websocket sends request for frame callback
- Node’s js invokes addon’s method for frame callback
- Native addon connects to camera and starts background thread of frame read loop
- Background thread invokes callback with frames on main thread
- Node transports frames to client websocket
Note :
When loop for reading webcam’s frames is started, it is still in the same thread of frame callback’s request. So it will stuck there in infinite loop. So I’m using UV thread to start frame read loop in background thread and invoking frame callback on current thread.
Usage
- Get the source
git clone https://github.com/kiriapurv/node-camera
- Edit `binding.gyp` according to your opencv installation
Installing opencv
- Macbrew tap homebrew/science && brew install opencv
- Linux Guide
Change values under `binding.gyp` if paths deffer.
- NPM Install
npm install
Running
npm start -- [-open] [-wsport websocketPort] [-webport webserverport] [-res widthxheight]
Option | Description |
---|---|
-open | Open streaming url in browser on startup |
-wsport | Web socket port for streaming media |
-webport | Web server port |
-res | Resolution for preview image |
Video
Source
https://github.com/kiriapurv/node-camera
References
Name | Link |
---|---|
UV Threads | http://nikhilm.github.io/uvbook/threads.html |
V8 Datatype Reference | http://izs.me/v8-docs/main.html |
OpenCV | http://docs.opencv.org/ |