如何解决如何使用突然停止响应的handpose.js优化此代码?
我这么说并不夸张,但实际上几分钟前,这段代码就可以正常工作了。但是,现在,每当我尝试运行它时,它就完全没有响应,只会使我的PC发热。我不知道为什么会这样。我认为我没有做任何会使代码具有这种行为的更改。我很确定这与问题开始之前的代码完全相同。而且这个问题不仅限于我的PC,它也发生在我的手机上,否则本应足以处理此问题。
这是有问题的代码:
<html>
<head>
<!--Load the Handpose model script along with its dependencies -->
<script src="https://unpkg.com/@tensorflow/tfjs-core@2.1.0/dist/tf-core.js"></script>
<script src="https://unpkg.com/@tensorflow/tfjs-converter@2.1.0/dist/tf-converter.js"></script>
<script src="https://unpkg.com/@tensorflow/tfjs-backend-webgl@2.1.0/dist/tf-backend-webgl.js"></script>
<script src="https://unpkg.com/@tensorflow-models/handpose@0.0.6/dist/handpose.js"></script>
<script>
async function get_handpose() {
// Load the MediaPipe handpose model.
const model = await handpose.load();
const stream = document.getElementById("movie");
while (1) {
const predictions = await model.estimateHands(document.getElementById("movie"));
if (predictions.length > 0) {
console.log(predictions[0].landmarks[8][1]);
}
else {
console.log(`no hands detected..`);
}
}
}
</script>
</head>
<body>
<div id="container">
<video width=640 height=480 autoplay id="movie"> </video>
</video>
<script>
video = document.getElementById("movie");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) { video.srcObject = stream; });
}
main();
function main() {
if (video.readyState == 4) {
console.log("video is ready for processing..");
get_handpose();
}
else {
console.log("nope,not loaded yet..");
setTimeout(main,1000 / 30);
}
}
</script>
</body>
<style>
video {
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
position: fixed;
margin: auto;
}
</style>
</html>
某些人可能建议删除代码中的while(1)
部分,但是在事情突然发生之前,使用那段代码运行得很顺利(实际上,由于某种原因删除了该代码将使handpose.js无法加载)干草丝毫无理由。因此,可以将其排除在等式之外。如果有人想知道代码应该做什么,应该控制台记录食指尖端的y轴。正是在这样做之前,似乎根本没有任何理由出错。对于喜欢Codepen的人来说,here is a link to the code that's hosted on that site。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。