gpt4 book ai didi

javascript - cv.Mat 不是构造函数 opencv

转载 作者:太空宇宙 更新时间:2023-11-03 21:49:22 24 4
gpt4 key购买 nike

我遇到了这个错误 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]);
}
cv.imshow('canvasOutput', dst);
// schedule the next one.
let delay = 1000/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
} catch (err) {
utils.printError(err);
}
};

// schedule the first one.
setTimeout(processVideo, 0);
}

我还导入了 opencv.js,因为我有它的下载版本。猜想有一些初始化问题,请帮我解决...

最佳答案

opencv.js 在真正初始化之前加载并触发 onload 事件。为了等到 opencv.js 真正准备好,opencv.js 提供了一个 Hook “onRuntimeInitialized”。像这样使用它:

function openCvReady() {
cv['onRuntimeInitialized']=()=>{
// do all your work here
};
}

关于javascript - cv.Mat 不是构造函数 opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56671436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com