简体   繁体   English

如何在模块中测试OpenCV nodejs绑定代码?

[英]How to test OpenCV nodejs binding code in a module?

I'm experimenting opencv and I've just created a 'face finder' module. 我正在实验opencv,我刚刚创建了一个“面部查找器”模块。

The code works well. 该代码运行良好。 However, my mocha test is not executing the whole code. 但是,我的摩卡测试未执行全部代码。

Here is my repository: https://github.com/scaryguy/facefinder 这是我的存储库: 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. im.detectObject()方法接受异步回调-即,它不会立即发生,而是在以后发生。

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 . 我对这个OpenCV库不熟悉,但是给定了方法的名称( detectObject ),我猜测这个方法在检测到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. 运行模块时,将检测到cv.FACE_CASCADE对象,并且代码将运行。
  • When running the test, there is no cv.FACE_CASCADE to detect, and it doesn't run (but also does not cause an error). 运行测试时,没有cv.FACE_CASCADE可以检测到,并且它不会运行(但也不会导致错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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