Node-OpenCV 0.0.11

04 Mar 2013

A couple of days ago I released 0.0.11 of node-opencv. I try to keep releases fairly small and incremental, so normally there wouldn't be much to talk about, but in this version I've started adding what I hope will be the first of several Stream wrappers to the opencv primitives.

This is a breaking API change - if you were using ImageStream before, you'll need to change it to ImageDataStream as ImageStream now refers to a stream of images.

The inspiration for the new API came from a module called camera by wearefractal which adds a clean stream API to a VideoCapture from a webcam.

I realised that working with video streams was a common occurrence and thus making them available as streams was a great idea - this would simplify a lot of the code I'd been writing.

Before, you could do image recognition with a series of nested callbacks:

var s = new cv.VideoCapture(0)

var detect = function(){
  s.read(function(im){
    im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
      console.log("I spy", faces.length, "faces...")
      detect();
    })
  })
}
detect()

While this works fine, it isn't exactly elegant - if we instead think of the webcam as a stream of images (or matrices in OpenCV parlance) then with the new API we could write the above as follows:

var matrixStream = new cv.VideoCapture(0).toStream()
  , faceRecognitionStream = new cv.ObjectDetectionStream(cv.FACE_CASCADE)

faceRecognitionStream.on('data', function(faces){
  console.log("I spy", faces.length, "faces...")
})

matrixStream.pipe(faceRecognitionStream)
matrixStream.read()

I'm excited to see what other OpenCV operations lend themselves to streams.

My aim with node-opencv is eventually to have a comprehensive API to all of the OpenCV functionality. But I also want it to be usable in an idiomatic javascript way. Adding streams to node-opencv is a step in that path.