Search code examples
javascriptmachine-learningp5.jsvs-web-site-projectmobilenet

Can anyone tell me how to trigger a function when the mouse is pressed and continues untill it is released in p5.js


I was trying to add an image using p5 and ml5 In my website where user can train there own image and get the predicted output over webcam I tried implementing it by using

var addImage;
var mobilenet;
mobilenet = ml5.featureExtractor('MobileNet', modelReady);
classifier = mobilenet.classification(video,videoReady);
addImage = createButton('Insert');
addImage.mousePressed(function (){
classifier.addImage('Insert');
});

but for every image, I need to press the mouse button to insert I just want to make it something like this

**On mousePress()
  function to add multiple image;
  On mouseRelease()
  stop;**

Solution

  • From this reference, this should work;

    var addImage;
    var mobilenet;
    var drawImageInterval = null;
    mobilenet = ml5.featureExtractor('MobileNet', modelReady);
    classifier = mobilenet.classification(video,videoReady);
    addImage = createButton('Insert');
    addImage.mousePressed(function (){
       if(mouseIsPressed && !drawImageInterval){
         drawImageInterval = setInterval(function(){
             classifier.addImage('Insert');
          }, 1000);
      } else {
         clearInterval(drawImageInterval);
         drawImageInterval = null;
      }
    
    });