OpenCV.js-detectMultiScale“无法捕获此异常”

时间:2019-01-12 19:04:55

标签: javascript html opencv

我正在尝试通过OpenCV.js使用面部识别,但是当我调用detectMultiScale()对象的CascadeClassifier方法时,会收到错误消息:

  

未捕获6446128-异常捕获已禁用,无法捕获此异常。使用-s DISABLE_EXCEPTION_CATCHING = 0或DISABLE_EXCEPTION_CATCHING = 2进行编译。

问题是我直接从opencv.org开始利用opencv.js的托管版本-它不是构建版本,因为我无法自己构建它,因此无法遵循错误的说明。

我遵循了他们的GitHub here上的示例,并修改了代码以满足我的需要,如下所示:

<html>
  <head>
    <script src="https://docs.opencv.org/master/opencv.js"></script>
    <script src="https://docs.opencv.org/master/utils.js"></script>
  </head>

  <body>
    <img id="test" src="image/with/face.jpg" alt=""/>

    <canvas id="output"></canvas>

    <script>
        let face_cascade = new cv.CascadeClassifier();
      face_cascade.load("https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades_cuda/haarcascade_frontalface_default.xml");

        function face_detector() {
                let imgEl = document.getElementById("test");
                let img = cv.imread(imgEl);

                cv.imshow("output", img);
                let src = cv.imread("output");

                let gray = new cv.Mat();
                let msize = new cv.Size(0,0);
                cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);

                let faces = new cv.RectVector();
                face_cascade.detectMultiScale(gray, faces, 1.1, 3, 0, msize, msize); //Error occurs here
            }

        face_detector();
    </script>
  </body>
</html>

任何有OpenCV.js经验和面部识别经验的人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

遵循此thread

  

在使用加载功能加载xml文件之前,已对其进行“预构建”。为此,使用了utils.js中的函数createFileFromUrl。之后,我们终于可以从文件中加载分类器了。

let classifier = new cv.CascadeClassifier();  // initialize classifier

let utils = new Utils('errorMessage'); //use utils class

let faceCascadeFile = 'haarcascade_frontalface_default.xml'; // path to xml

// use createFileFromUrl to "pre-build" the xml
utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
    classifier.load(faceCascadeFile); // in the callback, load the cascade from file 
});

人脸检测其他示例

TRY IT :

let src = cv.imread('canvasInput');
let gray = new cv.Mat();
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
let faces = new cv.RectVector();
let eyes = new cv.RectVector();
let faceCascade = new cv.CascadeClassifier();
// load pre-trained classifiers
faceCascade.load('haarcascade_frontalface_default.xml');
// detect faces
let msize = new cv.Size(0, 0);

// try to change scaleFactor  and minNeighbors values
faceCascade.detectMultiScale(gray, faces,1.05,0);
for (let i = 0; i < faces.size(); ++i) {
    let roiGray = gray.roi(faces.get(i));
    let roiSrc = src.roi(faces.get(i));
    let point1 = new cv.Point(faces.get(i).x, faces.get(i).y);
    let point2 = new cv.Point(faces.get(i).x + faces.get(i).width,
                              faces.get(i).y + faces.get(i).height);
    cv.rectangle(src, point1, point2, [255, 0, 0, 255]);
    roiGray.delete(); roiSrc.delete();
}
cv.imshow('canvasOutput', src);
src.delete(); gray.delete(); faceCascade.delete();
faces.delete(); eyes.delete();

尝试更改faceCascade.detectMultiScale参数,如下面的示例所示:

faceCascade.detectMultiScale(gray, faces,1.05,0);
faceCascade.detectMultiScale(gray, faces,1.05,1);

faceCascade.detectMultiScale(gray, faces,2,0);
faceCascade.detectMultiScale(gray, faces,2,1);

faceCascade.detectMultiScale(gray, faces,3,0);
faceCascade.detectMultiScale(gray, faces,3,1);

faceCascade.detectMultiScale(gray, faces,4,0);
faceCascade.detectMultiScale(gray, faces,4,1);

答案 1 :(得分:1)

解决方案是

let faceCascadeFile = 'haarcascade_frontalface_default.xml'; 
utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
    console.log('cascade ready to load.');
    let src = cv.imread('imageInit');
    let gray = new cv.Mat();
    cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
    let faces = new cv.RectVector();
    let faceCascade = new cv.CascadeClassifier();
        faceCascade.load(faceCascadeFile); 
        let msize = new cv.Size(0, 0);
        faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, msize, msize); 
    }); 
}

有关完整的代码和解释,请使用此链接Face Detection with Javascript and OpenCV

Human Eye Detection using Javascript and OpenCV