简体   繁体   English

如何在跟踪js中进行人脸检测后捕获图像

[英]how to capture image after face detection in tracking js

I am using tracking js for face detection. 我正在使用跟踪js进行面部检测。 I successfully detected the face but I don't know how to capture image frame.I want to save the detected face as image. 我成功检测到脸部,但我不知道如何捕捉图像帧。我想将检测到的脸部保存为图像。 This is my HTML page: 这是我的HTML页面:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>tracking.js - face with camera</title>

  <script src="../tracking.js-master/build/tracking-min.js" type="text/javascript"></script>
  <script src="../tracking.js-master/build/data/face-min.js"></script>

  <link rel="stylesheet" type="text/css" href="face.css">

</head>
<body>

  <div class="demo-frame">
      <video id="video" class="face-video" width="740" height="560" preload autoplay loop muted></video>
      <canvas id="canvas" class="face-canvas" width="740" height="560">    </canvas>
  </div>

  <script src="faceDetection.js" type="text/javascript"></script>

</body>
</html>

And this is my faceDetection.js file: 这是我的faceDetection.js文件:

window.onload = function() {
  var video = document.getElementById('video');
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');
  var tracker = new tracking.ObjectTracker('face');
  tracker.setInitialScale(6);
  tracker.setStepSize(2);
  tracker.setEdgesDensity(0.1);
  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 = '#ff0000';
      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);
    });
  });
};

You could use getUserMedia api and use download attribute on <a> element. 您可以使用getUserMedia api并在<a>元素上使用download属性。 It's a bit tricky and it will not be available on all browsers: 它有点棘手,并不适用于所有浏览器:

First draw the video on canvas: 首先在画布上绘制视频:

tracker.on('track', function(event) {
  context.clearRect(0, 0, canvas.width, canvas.height);
  event.data.forEach(function(rect) {
    context.strokeStyle = '#ff0000';
    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);
    context.drawImage(video, rect.x, rect.y, rect.width, rect.height, rect.x, rect.y, rect.width, rect.height);
  });
});

Create a method to trigger the snapshot: 创建一个触发快照的方法:

var snapshot = function() {
  if (localMediaStream) {
    document.querySelector('a').href = canvas.toDataURL('image/webp');
  }
}

video.addEventListener('click', snapshot, false);

Keep a reference to user media: 保留对用户媒体的引用:

navigator.getUserMedia({video: true}, function(stream) {
  localMediaStream = stream;
}, function(error){console.error(error)});

See a rough demo with all the code in action here . 见有行动的所有代码粗略的演示在这里 Click on the video and a image should be saved (Note: Using latest Chrome; You may need to fine tune coordinates; Be sure to click snapshot after detection was done correctly). 单击视频,应保存图像(注意:使用最新的Chrome;您可能需要微调坐标;确保在检测正确完成后单击快照)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此Jsfiddle无法正常工作:tracking.js人脸检测示例 - Why is this Jsfiddle not working: tracking.js face detection example 在使用 face-api.js 进行人脸检测后,有什么方法可以自动裁剪人脸? - Is there any way to auto crop the face after doing face detection with face-api.js? 通过tracking.js在画布上进行面部跟踪 - Face tracking on canvas via tracking.js 如何对 javascript 中的 static 图像使用 mediapipe 人脸检测? - How can I use mediapipe face detection for static image in javascript? 使用tracking.js进行人脸识别 - face recognition using tracking.js 仅捕获检测到的人脸正方形 JS - Capture Detected Face Square Only JS Kinetic.JS图像在使用像素检测后无法拖动 - Kinetic.JS Image Unable to Drag After Using Pixel Detection 如何使用网络摄像头捕获图像并通过八位字节 stream 发送到 microsoft 的 face api - How to capture an image with the webcam and send it through the octet stream to microsoft's face api 使用面部 api js 上传面部遮罩,我找到面部地标和 append 遮罩,但如何下载整个附加图像? - Using face api js to upload mask on face, I find the face landmarks and append the mask on them but how do I download the appended image as a whole? 如何将网络摄像头选择添加到官方 mediapipe 人脸检测解决方案? - How to add webcam selection to official mediapipe face detection solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM