简体   繁体   中英

How to test OpenCV nodejs binding code in a module?

I'm experimenting opencv and I've just created a 'face finder' module.

The code works well. However, my mocha test is not executing the whole code.

Here is my repository: https://github.com/scaryguy/facefinder

When I run this test:

describe('FaceFinder', function() {
    it('should work', function() {
        return FaceFinder('/Users/scaryguy/arge/opencv/facefinder/test/fixtures/childFaces.jpg', '/Users/scaryguy/arge/opencv/facefinder/')
    });
});

My code is executed partially.

See where I commented the line where the code is run lastly.

// https://github.com/scaryguy/facefinder/blob/master/lib/find.js
Find.prototype.image = function(cb) {
    var img = this;
    cv.readImage(img.image_path, function(err, im) {
        if (err) return cb(err, false);
// WHEN I console.log something here it's shown
        im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) {
// BUT here is never run WHILE executing the test. 
            if (err) return cb(err, false);
            for (var i = 0; i < faces.length; i++) {
                var x = faces[i]
                im.ellipse(x.x + x.width / 2, x.y + x.height / 2, x.width / 2, x.height / 2);
            }
            im.save(img.output_name + ".jpg");
            console.log("Number of found faces: " + faces.length + "\n");
            cb(null, true);
        });
    })
}

Weird thing is when I require this module and run the code, it works perfectly. But it doesn't work while testing. And there is NO error.

Any ideas?

The im.detectObject() method accepts an asynchronous callback - ie, it will not happen immediately but at a later date.

I am not familiar with this OpenCV library but given the name of the method ( detectObject ) I am going to guess that this method will invoke the callback only when it detects a cv.FACE_CASCADE Object .

Your situation is almost certainly thus:

  • When running the module, a cv.FACE_CASCADE object is detected, and the code runs.
  • When running the test, there is no cv.FACE_CASCADE to detect, and it doesn't run (but also does not cause an error).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM