How to Stop/Terminate ML5 Posenet

628 Views Asked by At

Creating an app and want to stop Posenet when its job is done

    private sketch(p: any) {
        p.setup = () => {
            this.poseNet = ml5.poseNet(p.createCapture(p.VIDEO), {
                outputStride: 8
            });
            this.poseNet.on('pose', (results) => {
                //DO SOMETHING WITH results[0]
            });
        };
    }

Already tried setting diverse vars to null

    StopKI() {
        // Terminate PoseNet when done
        this.p5 = null;
        this.poseNet = null;
        console.log('KI stopped');
    }
1

There are 1 best solutions below

4
On BEST ANSWER

The posenet responds to detections in the video element. If you remove the video element the detection and callbacks will probably stop?

const video = document.getElementById("video");
video.remove();

In the source code the detection function keeps calling itself if the video element exists

async multiPose(inputOr, cb) {
  if (this.video) {
    return tf.nextFrame().then(() => this.multiPose());
  }
}

So removing the video element might hopefully stop the repeating loop. I don't see a more elegant solution in the source code

UPDATE

I found that the poseNet.on(...) event listener can be canceled by calling RemoveListener and passing the same callback function. It seems to work in the online P5 web editor:

// put the pose event callback in a variable
callback = function(results) {
    poses = results;
}

// start listening to pose detection events
poseNet.on('pose', callback);

// stop listening to pose detection events by removing the event listener
poseNet.removeListener('pose', callback);