我正在开展一个需要人脸识别和预测的项目。当我上传图像时,它应该从数据库中识别出这个人。我正在尝试使用Nodejs和OpenCV来实现。 我的代码如下所示:
var FaceRecognizer = new cv.FaceRecognizer();
var uploadDir = joinPath(__dirname, "/images");
var fileDir = joinPath(__dirname, "/uploads");
var cvImages = [];
fs.readdir(uploadDir, function(err, files){
if(err){
throw new Error(err);
}
if(files.length > 0){ //There are some user related image folders
files.forEach(function(subfolder, index, array){
if(subfolder != ".DS_Store" ){ //Issue with Mac, test on Linux-VM
//We are now iterating over each subfolder
var subFolderDir = joinPath(uploadDir, "/"+"subfolder");
var images = fs.readdirSync(subFolderDir);
images.forEach(function(image, index, array){//Get Matrix Objekt for each image to train OpenCV
if(image != ".DS_Store"){
var imageDir = joinPath(subFolderDir, "/"+image);
cv.readImage(imageDir, function(err, im){
var channels = im.channels();
if(channels >=3){
var labelNumber = parseInt(Math.floor((Math.random()* 10)+1)); //Create labelnumber; Account-Id starts by 1, labels for openCV start with 0
cvImages.push(new Array(labelNumber,im)); //Add image to Array
}
});
}
});
}
});
if(cvImages.length > 3){
console.log("Training images (we have at least 3 images)", cvImages);
FaceRecognizer.trainSync(cvImages);
cv.readImage(fileDir, function(err, im){
if(err) res.send(err);
var whoisit = FaceRecognizer.predictSync(im);
console.log("Identified image", whoisit);
})
}else{
console.log("Not enough images uploaded yet", cvImages);
}
}else{
console.log("There are no images uploaded yet!");
}
});
我收到此错误:
OpenCV Error: Assertion failed (s >= 0) in setSize, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp, line 116
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp:116: error: (-215) s >= 0 in function setSize
Aborted (core dumped)
有人可以建议任何可能的解决方案吗?