Skip to content

Commit

Permalink
Merge branch 'test-examples'
Browse files Browse the repository at this point in the history
Conflicts:
	package.json
  • Loading branch information
peterbraden committed Feb 11, 2015
2 parents 2c6ec42 + efb9bf5 commit 6725545
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $ npm install opencv
```

## Examples
Run the examples from the parent directory.

### Face Detection

Expand Down
6 changes: 4 additions & 2 deletions data/hogcascade_cars_sideview.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<opencv_storage>
<cascade>
<output type_id="opencv-haar-classifier">
<stageType>BOOST</stageType>
<featureType>HOG</featureType>
<height>24</height>
Expand Down Expand Up @@ -835,5 +835,7 @@
16 8 16 8 24</rect></_>
<_>
<rect>
16 8 16 8 28</rect></_></features></cascade>
16 8 16 8 28</rect></_></features>

</output>
</opencv_storage>
8 changes: 4 additions & 4 deletions examples/addweighted.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var cv = require('../lib/opencv');

cv.readImage("./files/mona.png", function(err, orig) {
cv.readImage("./examples/files/mona.png", function(err, orig) {
if (err) throw err;

cv.readImage("./files/over_text.png", function(err, over_text) {
cv.readImage("./examples/files/over_text.png", function(err, over_text) {
if (err) throw err;

var result = new cv.Matrix(orig.width(), orig.height());
result.addWeighted(orig, 0.7, over_text, 0.9);
result.save("./tmp/weighted.png");
console.log('Image saved to ./tmp/weighted.png');
result.save("./examples/tmp/weighted.png");
console.log('Image saved to ./examples/tmp/weighted.png');
});
});
28 changes: 18 additions & 10 deletions examples/camera.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
var cv = require('../lib/opencv');

var camera = new cv.VideoCapture(0);
var window = new cv.NamedWindow('Video', 0)

setInterval(function() {
camera.read(function(err, im) {
if (err) throw err;
window.show(im);
window.blockingWaitKey(0, 50);
});
}, 20);
try {
var camera = new cv.VideoCapture(0);
var window = new cv.NamedWindow('Video', 0)
/*
setInterval(function() {
camera.read(function(err, im) {
if (err) throw err;
console.log(im.size())
if (im.size()[0] > 0 && im.size()[1] > 0){
window.show(im);
}
window.blockingWaitKey(0, 50);
});
}, 20);
*/
} catch (e){
console.log("Couldn't start camera:", e)
}
4 changes: 4 additions & 0 deletions examples/car-detection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* For some reason the cascade file is broken on linux :(
var cv = require('../lib/opencv');
cv.readImage("./files/car1.jpg", function(err, im){
Expand All @@ -16,3 +19,4 @@ cv.readImage("./files/car1.jpg", function(err, im){
console.log('Image saved to ./tmp/car-detection.jpg');
});
});
*/
7 changes: 5 additions & 2 deletions examples/face-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var http = require('http'),
request = require('request'),
cv = require('../lib/opencv');

http.createServer(function(req, resp){
var server = http.createServer(function(req, resp){
var url = req.url.slice(1);
request({uri:url, encoding:'binary'}, function(err, r, body){
if (err) return resp.end(err.stack);
Expand All @@ -27,4 +27,7 @@ http.createServer(function(req, resp){
});
});

}).listen(3000, function(){ console.log('Listening on http://localhost:3000'); })
})


//server.listen(3000, function(){ console.log('Listening on http://localhost:3000'); })
2 changes: 1 addition & 1 deletion examples/salt.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var cv = require('../lib/opencv');

cv.readImage("./files/mona.png", function(err, im) {
salt(im, 1000);
salt(im, 100);
im.save("./tmp/salt.png");
console.log('Image saved to ./tmp/salt.png');
});
Expand Down
48 changes: 28 additions & 20 deletions examples/take-face-pics.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
var cv = require('../lib/opencv');
var vid = new cv.VideoCapture(0);
try {
var vid = new cv.VideoCapture(0);

vid.read(function(err, im){
if (err) throw err;

im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
vid.read(function(err, im){
if (err) throw err;
if (!faces.length) return console.log("No Faces");
if (im.size()[0] > 0 && im.size()[1] > 0){

im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
if (err) throw err;
if (!faces.length) return console.log("No Faces");

var face = faces[0];
var ims = im.size();
var im2 = im.roi(face.x, face.y, face.width, face.height)
/*
im.adjustROI(
-face.y
, (face.y + face.height) - ims[0]
, -face.x
, (face.x + face.width) - ims[1])
*/
im2.save('./tmp/take-face-pics.jpg')
console.log('Image saved to ./tmp/take-face-pics.jpg');
})
});
var face = faces[0];
var ims = im.size();
var im2 = im.roi(face.x, face.y, face.width, face.height)
/*
im.adjustROI(
-face.y
, (face.y + face.height) - ims[0]
, -face.x
, (face.x + face.width) - ims[1])
*/
im2.save('./examples/tmp/take-face-pics.jpg')
console.log('Image saved to ./tmp/take-face-pics.jpg');
})
} else {
console.log("Camera didn't return image")
}
});
} catch (e){
console.log("Couldn't start camera", e)
}
4 changes: 2 additions & 2 deletions examples/warp-image.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var cv = require('../lib/opencv');

cv.readImage("./mona.png", function(err, im) {
cv.readImage("./files/mona.png", function(err, im) {
if (err) throw err;

var width = im.width();
Expand All @@ -11,6 +11,6 @@ cv.readImage("./mona.png", function(err, im) {
var dstArray = [0, 0, width * 0.9, height * 0.1, width, height, width * 0.2, height * 0.8];
var xfrmMat = im.getPerspectiveTransform(srcArray, dstArray);
im.warpPerspective(xfrmMat, width, height, [255, 255, 255]);
im.save("./warp-image.png");
im.save("./tmp/warp-image.png");
console.log('Image saved to ./tmp/warp-image.png');
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"devDependencies": {
"tape": "^3.0.0",
"aws-sdk": "~2.0.21"
"glob": "^4.0.6",
"request": "^2.45.0"
},
"bundledDependencies":["node-pre-gyp"],
"license": "MIT",
Expand Down
8 changes: 7 additions & 1 deletion src/HighGUI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ NamedWindow::NamedWindow(const std::string& name, int f){
NAN_METHOD(NamedWindow::Show){
SETUP_FUNCTION(NamedWindow)
Matrix *im = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
cv::imshow(self->winname, im->mat);

try{
cv::imshow(self->winname, im->mat);
} catch(cv::Exception& e ){
const char* err_msg = e.what();
NanThrowError(err_msg);
}

NanReturnValue(args.Holder());
}
Expand Down
8 changes: 7 additions & 1 deletion src/Matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Matrix::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(ctor, "drawAllContours", DrawAllContours);
NODE_SET_PROTOTYPE_METHOD(ctor, "goodFeaturesToTrack", GoodFeaturesToTrack);
NODE_SET_PROTOTYPE_METHOD(ctor, "houghLinesP", HoughLinesP);
NODE_SET_PROTOTYPE_METHOD(ctor, "crop", Crop);
NODE_SET_PROTOTYPE_METHOD(ctor, "houghCircles", HoughCircles);
NODE_SET_PROTOTYPE_METHOD(ctor, "inRange", inRange);
NODE_SET_PROTOTYPE_METHOD(ctor, "adjustROI", AdjustROI);
Expand Down Expand Up @@ -1016,7 +1017,12 @@ NAN_METHOD(Matrix::AddWeighted) {
float beta = args[3]->NumberValue();
int gamma = 0;

cv::addWeighted(src1->mat, alpha, src2->mat, beta, gamma, self->mat);
try{
cv::addWeighted(src1->mat, alpha, src2->mat, beta, gamma, self->mat);
} catch(cv::Exception& e ){
const char* err_msg = e.what();
NanThrowError(err_msg);
}


NanReturnNull();
Expand Down
21 changes: 21 additions & 0 deletions test/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var test = require('tape')
, glob = require('glob')
, exec = require('child_process').exec
, path = require('path')

module.exports = function(){

glob.sync('./examples/*.js').forEach(function(example){
test("Example: " + example, function(assert){

var fullName = path.resolve(example)
, examples = path.resolve('./examples')

exec('node ' + fullName, {cwd: examples}, function(error, stdout, stderr){
assert.error(error)
assert.end()
})
})
})

}
4 changes: 4 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,7 @@ test("fonts", function(t) {
});
})

// Test the examples folder.
require('./examples')()


0 comments on commit 6725545

Please sign in to comment.