From 0c730cef5a9701cdc48b60d8022e760eeead5892 Mon Sep 17 00:00:00 2001 From: Peter Braden Date: Thu, 26 Jan 2012 16:37:40 -0800 Subject: [PATCH] make readfile async --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ lib/opencv.js | 7 +++++-- src/OpenCV.cc | 17 ++++++++++++++++- test/smoke.js | 49 +++++++++++++++++++++++++++++++------------------ 4 files changed, 93 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9f0eb2f..b930f41 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,47 @@ Or to build the repo: +## API Documentation + +### Matrix + +The [matrix](http://opencv.jp/opencv-2svn_org/cpp/core_basic_structures.html#mat) is the most useful +base datastructure in OpenCV. Things like images are just matrices of pixels. + +#### Creation + + new Matrix(width, height) + +Or you can use opencv to read in image files. Supported formats are in the OpenCV docs, but jpgs etc are supported. + + cv.readImage(filename, function(mat){ + ... + }) + + cv.readImage(buffer, function(mat){ + ... + }) + +If you need to pipe data into an image, you can use an imagestream: + + var s = new cv.ImageStream() + + s.on('load', function(matrix){ + ... + }) + + fs.createReadStream('./examples/test.jpg').pipe(s); + +#### Accessors + +#### Image Processing + + + + + + + ## WIP diff --git a/lib/opencv.js b/lib/opencv.js index 510f827..c25f6da 100644 --- a/lib/opencv.js +++ b/lib/opencv.js @@ -44,12 +44,15 @@ imagestream.write = function(buf){ imagestream.end = function(b){ + var self = this; + if (b) imagestream.write.call(this,b); var buf = this.data.toBuffer(); - var im = cv.readImage(buf); - this.emit('load', im); + cv.readImage(buf, function(err, im){ + self.emit('load', im); + }); } diff --git a/src/OpenCV.cc b/src/OpenCV.cc index d5f3999..9dd9ffe 100644 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -27,6 +27,7 @@ OpenCV::ReadImage(const Arguments &args) { Matrix *img = ObjectWrap::Unwrap(im_h); cv::Mat mat; + REQ_FUN_ARG(1, cb); if (args[0]->IsNumber() && args[1]->IsNumber()){ int width, height; @@ -53,7 +54,21 @@ OpenCV::ReadImage(const Arguments &args) { } img->mat = mat; - return scope.Close(im_h); + + Local argv[2]; + + argv[0] = Local::New(Null()); + argv[1] = im_h; + + TryCatch try_catch; + + cb->Call(Context::GetCurrent()->Global(), 2, argv); + + if (try_catch.HasCaught()) { + FatalException(try_catch); + } + + return Undefined(); } catch( cv::Exception& e ){ const char* err_msg = e.what(); diff --git a/test/smoke.js b/test/smoke.js index 0f257a8..12c4247 100644 --- a/test/smoke.js +++ b/test/smoke.js @@ -111,19 +111,25 @@ vows.describe('Smoke Tests OpenCV').addBatch({ , "toBuffer": function(cv){ var buf = fs.readFileSync('./examples/mona.jpg') - , im = cv.readImage(buf.slice(0)) - , buf0 = im.toBuffer() - assert.ok(buf0); + cv.readImage(buf.slice(0), function(err, mat){ + var buf0 = mat.toBuffer() + + assert.ok(buf0); //assert.equal(buf.toString('base64'), buf0.toString('base64')); + }) + } , "faceDetect": { topic : function(){ var cv = require('../lib/opencv') - , im = cv.readImage("./examples/mona.jpg") - im.faceDetect("./examples/haarcascade_frontalface_alt.xml", {}, this.callback) + , cb = this.callback + + cv.readImage("./examples/mona.jpg", function(err, im){ + im.faceDetect("./examples/haarcascade_frontalface_alt.xml", {}, cb) + }) } , "finds face": function(err, faces){ @@ -142,19 +148,22 @@ vows.describe('Smoke Tests OpenCV').addBatch({ topic : require('../lib/opencv') , ".readImage from file": function(cv){ - var im = cv.readImage("./examples/mona.jpg") - assert.ok(im) - assert.equal(im.width(), 500); - assert.equal(im.height(), 756) - assert.equal(im.empty(), false) + cv.readImage("./examples/mona.jpg", function(err, im){ + assert.ok(im); + assert.equal(im.width(), 500); + assert.equal(im.height(), 756) + assert.equal(im.empty(), false) + }) } , ".readImage from buffer" : function(cv){ - var im = cv.readImage(fs.readFileSync('./examples/mona.jpg')) - assert.ok(im); - assert.equal(im.width(), 500); - assert.equal(im.height(), 756) - assert.equal(im.empty(), false) + cv.readImage(fs.readFileSync('./examples/mona.jpg'), function(err, im){ + assert.ok(im); + assert.equal(im.width(), 500); + assert.equal(im.height(), 756) + assert.equal(im.empty(), false) + }) + } } @@ -170,9 +179,13 @@ vows.describe('Smoke Tests OpenCV').addBatch({ , "face detection": { topic : function(){ var cv = require('../lib/opencv') - , im = cv.readImage("./examples/mona.jpg") - , cascade = new cv.CascadeClassifier("./examples/haarcascade_frontalface_alt.xml"); - cascade.detectMultiScale(im, this.callback)//, 1.1, 2, [30, 30]); + , self = this + + cv.readImage("./examples/mona.jpg", function(err, im){ + cascade = new cv.CascadeClassifier("./examples/haarcascade_frontalface_alt.xml"); + cascade.detectMultiScale(im, self.callback)//, 1.1, 2, [30, 30]); + }) + } , "finds face": function(err, faces){