猿问

cv.Mat 不是构造函数 opencv

我收到这个错误 TypeError: cv.Mat is not a constructor


我尝试做几乎所有事情都无法在互联网上找到任何解决方案


索引.html


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Hello OpenCV.js</title>

</head>

<body>

<h2>Hello OpenCV.js</h2>

<p id="status">OpenCV.js is loading...</p>

<div>

<img src="dd.jpg" style="display:none;" id ="img">

<canvas id = "videoInput" height="500" width="500"></canvas>

<canvas id = "canvasOutput" height="500" width="500"></canvas>


<script async type="text/javascript" src="index.js"></script>

<script async src="opencv.js" onload="onOpenCvReady();" type="text/javascript"></script>

</body>

</html>


索引.js


  document.getElementById('status').innerHTML = 'OpenCV.js is ready.';

  let video = document.getElementById('videoInput');

  let src = new cv.Mat(video.height, video.width, cv.CV_8UC4);

  let dst = new cv.Mat(video.height, video.width, cv.CV_8UC4);

  let gray = new cv.Mat();

  let cap = new cv.VideoCapture(video);

  let faces = new cv.RectVector();

  let classifier = new cv.CascadeClassifier();


  // load pre-trained classifiers

  classifier.load('haarcascade_frontalface_default.xml');


  const FPS = 30;

  function processVideo() {

      try {

          if (!streaming) {

              // clean and stop.

              src.delete();

              dst.delete();

              gray.delete();

              faces.delete();

              classifier.delete();

              return;

          }

          let begin = Date.now();

          // start processing.

          cap.read(src);

          src.copyTo(dst);

          cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0);

          // detect faces.

          classifier.detectMultiScale(gray, faces, 1.1, 3, 0);

          // draw faces.

          for (let i = 0; i < faces.size(); ++i) {

              let face = faces.get(i);

              let point1 = new cv.Point(face.x, face.y);

              let point2 = new cv.Point(face.x + face.width, face.y + face.height);

              cv.rectangle(dst, point1, point2, [255, 0, 0, 255]);

          }

    


我也在导入 opencv.js,因为我有它的下载版本。估计是初始化有问题,请帮我解决...


撒科打诨
浏览 931回答 3
3回答

蓝山帝景

opencv.js 在真正初始化之前加载并触发 onload 事件。为了等到 opencv.js 真正准备好,opencv.js 提供了一个挂机“onRuntimeInitialized”。像这样使用它:function openCvReady() {&nbsp; cv['onRuntimeInitialized']=()=>{&nbsp; &nbsp; // do all your work here&nbsp; };}

DIEA

确保脚本真正加载。如果错误是“cv is not defined”,那么要么删除async脚本标签中的 要么添加这个(或者只是标签中的一个onload属性<script>)&nbsp;script.addEventListener('load', () => {在 WASM 构建(并且只有 WASM 构建)中,cv 不会立即可用,因为 WASM 是在运行时编译的。将启动功能分配给cv.onRuntimeInitialized。请注意,WASM 版本应该更快;但是,它会产生一些启动开销(几 CPU 秒)。非 WASM 根本不调用onRuntimeInitialized。要检查这两种情况,可以这样做if (cv.getBuildInformation){&nbsp; &nbsp; console.log(cv.getBuildInformation());&nbsp; &nbsp; onloadCallback();}else{&nbsp; &nbsp; // WASM&nbsp; &nbsp; cv['onRuntimeInitialized']=()=>{&nbsp; &nbsp; &nbsp; &nbsp; console.log(cv.getBuildInformation());&nbsp; &nbsp; &nbsp; &nbsp; onloadCallback();&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答