如何解决承诺递归|异步画布绘画
我有一个画布,我有一个tensorflowjs模型。画布以相机的最大fps进行绘制。但是由于计算时间,我的TensorflowJS模型只能达到5 FPS。我想要实现的是,画布从相机上以稳定的60 FPS进行绘制,并且使用了AI的临时结果。因此,AI必须单独运行,并将值保存到全局变量中。我有这样的东西:
let currFace;
let net;
let currVideo;
facemesh.load().then(model => {
document.dispatchEvent(new CustomEvent("model.loaded"))
net = model;
});
async function inference() {
currFace = await net.estimateFaces(currVideo)
inference();
}
document.addEventListener("canvas.repaint",async e => {
const {ctx,video} = e.detail.payload;
ctx.fillStyle = "#c82124";
currVideo = video;
ctx.drawImage(video,0);
if (currFace && currFace.length > 0) {
for (let i = 0; i < currFace.length; i++) {
if (currFace[i].faceInViewConfidence > 0.5) {
const [bx,by] = currFace[i].boundingBox.topLeft;
const w = currFace[i].boundingBox.bottomRight[0] - bx;
const h = currFace[i].boundingBox.bottomRight[1] - by;
ctx.beginPath();
ctx.rect(x,y,w,h);
ctx.stroke();
const keypoints = currFace[i].scaledMesh;
// Log facial keypoints.
for (let i = 0; i < keypoints.length; i++) {
const [x,z] = keypoints[i];
ctx.beginPath();
ctx.arc(x,2,2 * Math.PI);
ctx.stroke();
ctx.fill();
}
} else {
ctx.fillText("No face on screen",10,50);
}
}
} else if (net) {
currFace = await net.estimateFaces(currVideo);
inference();
}
}
);
重要的部分是inference()调用和函数本身。我获得100%的CPU使用率,并且该网站无法使用(主线程被阻止...)有任何想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。