简体   繁体   English

requestAnimFrame被执行多次

[英]requestAnimFrame is executed multiple times

I have an emotion detection running with openCV.js for the face detection and tensorflow.js for the emotion classification. 我有一个与openCV.js一起运行的用于面部检测的情绪检测程序,以及用于情感分类的tensorflow.js。 When I start the emotion detection I call the requestAnimFrame(myProcessingLogic) function and pass my detection logic to the callback parameter. 当我开始情感检测时,我会调用requestAnimFrame(myProcessingLogic)函数并将检测逻辑传递给回调参数。 My processing logic calls the requestAnimFrame(myProcessingLogic) again. 我的处理逻辑再次调用requestAnimFrame(myProcessingLogic)。

When disabling the emotion detection a global variable is set which then disables the recurisve call to requestAnimFrame. 禁用情感检测时,将设置一个全局变量,该全局变量随后将禁用对requestAnimFrame的重新调用。 This works fine. 这很好。

...but on each reactivation of the emotion detection the requestAnimFrame call is called once more additionally. ...但是在每次重新激活情感检测时,都会再次再次调用requestAnimFrame调用。 This causes performance issues. 这会导致性能问题。

I tried to save the returned id of requestAnimFrame() globally to call cancelAnimFrame() when the detection is stopped but this did not seem to have an effect. 我试图全局保存返回的requestAnimFrame()的ID,以在检测停止时调用cancelAnimFrame(),但这似乎没有效果。

First call: 首次致电:

function startVideoProcessing() {
    if (!streaming) {
        console.warn("Please startup your webcam");
        return;
    }

    canvasInput = document.createElement('canvas');
    canvasInput.width = videoWidth;
    canvasInput.height = videoHeight;
    canvasInputCtx = canvasInput.getContext('2d');

    canvasBuffer = document.createElement('canvas');
    canvasBuffer.width = videoWidth;
    canvasBuffer.height = videoHeight;
    canvasBufferCtx = canvasBuffer.getContext('2d');

    srcMat = new cv.Mat(videoHeight, videoWidth, cv.CV_8UC4);
    grayMat = new cv.Mat(videoHeight, videoWidth, cv.CV_8UC1);

    requestAnimId = requestAnimationFrame(processVideo);
}

ProcessingLogic 处理逻辑

function processVideo() {
    if(!streaming) {
        return;
    }

    /*
    logic removed to simplify
    */

    requestAnimId = requestAnimationFrame(processVideo);
}
function stopEmotionTracking() {
    stopCamera();
    cancelAnimationFrame(requestAnimId);
    requestAnimId = null;

}

I took a look in the firefox runtime analysis and saw that on each reactivation an additional function call is performed. 我看了一下firefox运行时分析,发现每次重新激活时都会执行一个附加的函数调用。

运行时分析的图像

Found the bug myself. 自己发现了该错误。 It had nothing to do with the code posted above. 它与上面发布的代码无关。 On each start of the emotion tracking I was adding an EventListener to the video element. 在情感跟踪的每次开始时,我都向视频元素添加了一个EventListener。 The EventListener on the other hand executed startVideoProcessing. 另一方面,EventListener执行startVideoProcessing。 Since those eventlistener stack on each other they were executed multiple times. 由于这些事件侦听器彼此堆叠,因此它们被多次执行。

For anyone facing the same problem, take care of your event listeners ;) 对于面临相同问题的任何人,请注意事件监听器;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM