无法使用opencv.js显示人脸检测图像

时间:2019-07-20 08:40:52

标签: javascript opencv face-detection

在这里,我正在使用opencv.js进行面部检测。它是javascript中opencv中的示例之一。我可以从文件中获取图像,但是使用multidetect函数加载图像后,出现以下错误:

  

opencv.js:26未捕获6445208-异常捕获已禁用,无法捕获此异常。使用-s DISABLE_EXCEPTION_CATCHING = 0或DISABLE_EXCEPTION_CATCHING = 2进行编译。

我的代码:

<h2>Hello OpenCV.js</h2>
<p id="status">OpenCV.js is loading...</p>
<div>
  <div class="inputoutput">
    <img id="canvasInput" alt="No Image" />
    <div class="caption">imageSrc <input type="file" id="fileInput" name="file" /></div>
  </div>
  <div class="inputoutput">
    <canvas id="canvasOutput" ></canvas>
    <div class="caption">canvasOutput</div>
  </div>
</div>
<script type="text/javascript">
let imgElement = document.getElementById('canvasInput');
let inputElement = document.getElementById('fileInput');
inputElement.addEventListener('change', (e) => {
  imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
imgElement.onload = function() {
  //let mat = cv.imread(imgElement);
  //cv.imshow('canvasOutput', mat);
 // mat.delete();
let src = cv.imread('canvasInput');
let dst = new cv.Mat();
// To distinguish the input and output, we graying the image.
// You can try different conversions.
cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);

let faces = new cv.RectVector();
let eyes = new cv.RectVector();

let faceCascade = new cv.CascadeClassifier();
let eyeCascade = new cv.CascadeClassifier();

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


faceCascade.load('haarcascade_frontalface_default.xml');


eyeCascade.load('haarcascade_eye.xml');

let msize = new cv.Size(0, 0);

faceCascade.detectMultiScale(dst, faces, 1.1, 3, 0, msize, msize);

for (let i = 0; i < faces.size(); ++i) {
    let roiGray = dst.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]);
    // detect eyes in face ROI
    eyeCascade.detectMultiScale(roiGray, eyes);
    for (let j = 0; j < eyes.size(); ++j) {
        let point1 = new cv.Point(eyes.get(j).x, eyes.get(j).y);
        let point2 = new cv.Point(eyes.get(j).x + eyes.get(j).width,
                                  eyes.get(j).y + eyes.get(j).height);
        cv.rectangle(roiSrc, point1, point2, [0, 0, 255, 255]);
    }
    roiGray.delete(); roiSrc.delete();
}
cv.imshow('canvasOutput', src);
src.delete(); 
dst.delete(); 
faceCascade.delete();
eyeCascade.delete(); 
faces.delete(); 
eyes.delete();

};
function onOpenCvReady() {
  document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
}
</script>
<script async src="opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>
</body>
</html>

0 个答案:

没有答案