Access and stream webcam in nodejs using opencv and websockets

December 2014 ยท 2 minute read

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

  1. Client websocket sends request for frame callback
  2. Node’s js invokes addon’s method for frame callback
  3. Native addon connects to camera and starts background thread of frame read loop
  4. Background thread invokes callback with frames on main thread
  5. 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

  1. Get the source
    git clone https://github.com/kiriapurv/node-camera
  2. Edit `binding.gyp` according to your opencv installation
    Installing opencv
    - Mac
    brew tap homebrew/science && brew install opencv
    - Linux Guide
    Change values under `binding.gyp` if paths deffer.
  3. 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/

contact - social@apurv.me