tracking.js icon indicating copy to clipboard operation
tracking.js copied to clipboard

turn off camera

Open icaromh opened this issue 9 years ago • 13 comments

Hi :dancer:

There is no way to turn off the camera? I tried with de trackerTask.stop(), but this just stopped the tracker and not the streaming.

Looking at the code in initUserMedia_() the stream var isn't referenced.

Maybe if insert a stopUserMedia() or something like that.

icaromh avatar Jul 04 '15 17:07 icaromh

found the solutions yet?

sushant12 avatar Aug 18 '15 04:08 sushant12

@sushant12, I did tracking.stopUserMedia() #128 but it isn't merged yet

Here: https://github.com/icaromh/tracking.js/blob/turn-off-camera/build/tracking.js#L84

icaromh avatar Aug 18 '15 13:08 icaromh

@icaromh Your solution is working? how to call you function.? Thanks

M-Husein avatar May 17 '17 15:05 M-Husein

@M-Husein I've abandoned the project that has used this lib. But you can look here

icaromh avatar May 17 '17 21:05 icaromh

@icaromh Thanks for reply, I see that. But I don't know how to use or call your function? :)

Please help me.

M-Husein avatar May 18 '17 01:05 M-Husein

@M-Husein, I think that is something like:

    window.onload = function() {

      // as tracking is a global variable, you can just call it anytime after initialize 
      window.setTimeout(function(){ 
             tracking.stopUserMedia();
      }, 1000 * 10)

      var video = document.getElementById('video');
      var canvas = document.getElementById('canvas');
      var context = canvas.getContext('2d');

      var tracker = new tracking.ObjectTracker('face');
      tracker.setInitialScale(4);
      tracker.setStepSize(2);
      tracker.setEdgesDensity(0.1);

      // You initialize UserMedia here
      tracking.track('#video', tracker, { camera: true });

      tracker.on('track', function(event) {
        context.clearRect(0, 0, canvas.width, canvas.height);

        event.data.forEach(function(rect) {
          context.strokeStyle = '#a64ceb';
          context.strokeRect(rect.x, rect.y, rect.width, rect.height);
          context.font = '11px Helvetica';
          context.fillStyle = "#fff";
          context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
          context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
        });
      });

      var gui = new dat.GUI();
      gui.add(tracker, 'edgesDensity', 0.1, 0.5).step(0.01);
      gui.add(tracker, 'initialScale', 1.0, 10.0).step(0.1);
      gui.add(tracker, 'stepSize', 1, 5).step(0.1);
    };

I've not tested this

icaromh avatar May 18 '17 14:05 icaromh

Thanks for reply again :) I have try your guide, but not success & I get error in console like this:

tracking_error

I almost gave up, please help me :(

M-Husein avatar May 19 '17 14:05 M-Husein

Now, I can stop video & stream, working good. ;) @icaromh, Thanks Dude

M-Husein avatar Jul 10 '17 04:07 M-Husein

  tracking.stopUserMedia = function(){
    if(tracking.localStream){
      tracking.localStream.stop(); 
    }
  }; 

is deprecated and should be changed as

tracking.stopUserMedia = function(){
    if(tracking.localStream){
      tracking.localStream.getVideoTracks()[0].stop();   
   }
  }; 

alisomay avatar Nov 03 '17 01:11 alisomay

With the latest version available, only this helped me: setTimeout(function () { trackingTask.stop(); video.pause(); video.srcObject.getVideoTracks()[0].stop(); }, 100);

nicksav avatar Nov 23 '17 02:11 nicksav

The following works for me in newest Chrome and the master branch. Find the video element that was referenced in tracking.track and call elm.srcObject.getTracks()[0].stop();

tugend avatar Feb 27 '18 15:02 tugend

Hello, Is there any solution for this issue?

I also tried all solutions above but seem they didn't work. I cannot stop tracking.js on accessing my webcam.

lvntruong avatar Jun 10 '18 04:06 lvntruong

I have tried videoElement.srcObject.getTracks()[0].stop() but it doesn't work, the video element doesn't have 'srcObject' attribute in latest version. So i modified the source code of tracking.js like this: image

then i can call tracking.stream.getTracks()[0].stop(); to turn off camera, it works well.

you can also add a srcObject attribute for video element, then call videoElement.srcObject.getTracks()[0].stop()

shady-xia avatar Jul 20 '18 02:07 shady-xia