eduardolundgren / tracking.js

A modern approach for Computer Vision on the web
http://trackingjs.com
Other
9.44k stars 1.45k forks source link

turn off camera #126

Open icaromh opened 9 years ago

icaromh commented 9 years ago

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.

sushant12 commented 9 years ago

found the solutions yet?

icaromh commented 9 years ago

@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

M-Husein commented 7 years ago

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

icaromh commented 7 years ago

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

M-Husein commented 7 years ago

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

Please help me.

icaromh commented 7 years ago

@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

M-Husein commented 7 years ago

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 commented 7 years ago

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

alisomay commented 6 years ago
  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();   
   }
  }; 
nicksav commented 6 years ago

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

tugend commented 6 years ago

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();

lvntruong commented 6 years ago

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.

shady-xia commented 6 years ago

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()