1

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');
    }
Kokodoko
  • 26,167
  • 33
  • 120
  • 197

1 Answers1

2

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);
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • It´s embedded in an Angular(Ionic) app and I'm getting the video-input via p5-lib (p.VIDEO) because the whole Postnet is written in a Service so I cant refer to the HTML. Cause the ref. of p.VIDEO is passed setting it to null does not affect it. So where can u set it to null? – Felix Brüggemann Aug 01 '20 at 20:17
  • When you create the posenet you pass a reference to the video it should analyze: `const poseNet = ml5.poseNet(video, modelLoaded)`, that’s the video that needs to be removed. Just setting the reference to null may not help because ml5 keeps an internal reference. – Kokodoko Aug 02 '20 at 22:44
  • yes but that video is p.VIDEO so its no reference in the HTML-Code (which I could not make a workaround cause the logic sits in a service) so how can I remove the p.VIDEO or is there a way where i could store this in a variable and .remove this? – Felix Brüggemann Aug 03 '20 at 09:08
  • I tried setting the video to null in the P5 web editor, but that actually results in an error. But looking in the source code of ML5 it says that poseNet is an EventEmitter. That means we can remove the event callback, which seems to be working for me in the online P5 example. I have updated my answer above. – Kokodoko Aug 03 '20 at 11:12