如何testing模块中的OpenCV nodejs绑定代码?
我正在尝试opencv,我刚刚创build了一个“face finder”模块。
代码运行良好。 不过,我的摩卡testing并没有执行整个代码。
这是我的存储库: https : //github.com/scaryguy/facefinder
当我运行这个testing时:
describe('FaceFinder', function() { it('should work', function() { return FaceFinder('/Users/scaryguy/arge/opencv/facefinder/test/fixtures/childFaces.jpg', '/Users/scaryguy/arge/opencv/facefinder/') }); });
我的代码部分执行。
看看我在哪里注释了最后运行代码的行。
// 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(xx + x.width / 2, xy + 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); }); }) }
奇怪的是,当我需要这个模块并运行代码时,它完美的工作。 但是在testing时它不起作用。 没有错误。
有任何想法吗?
im.detectObject()
方法接受一个asynchronouscallback – 即不会立即发生,而是在以后的日子。
我不熟悉这个OpenCV库,但给出了方法的名称( detectObject ),我会猜测, 只有当它检测到一个cv.FACE_CASCADE Object
时,这个方法才会调用callback。
你的情况几乎肯定是这样的:
- 运行模块时,会检测到
cv.FACE_CASCADE
对象,并运行代码。 - 运行testing时,没有
cv.FACE_CASCADE
可以检测,并且不运行(但也不会导致错误)。