Advertisement
Guest User

script.js

a guest
Apr 14th, 2021
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////////////////////
  2. (function() {
  3.     var canvas = document.getElementById('canvas');
  4.     var context = canvas.getContext('2d');
  5.     var video = document.getElementById('videoElement');
  6.     /////////////////////////////////////////////////////////////////
  7.     if (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia) {
  8.               navigator.mediaDevices.getUserMedia({ video: true,
  9.                     audio:false })
  10.                 .then(function (stream) {
  11.                   video.srcObject = stream;
  12.                 })
  13.                 .catch(function (err) {
  14.                   console.log("Something went wrong!");
  15.                 });
  16.             }
  17.     ///////////////////////////////////////////////////////////////
  18.     video.addEventListener('play',function()
  19.                           {
  20.         draw(this, context,640,480);
  21.     },false);
  22.     ///////////////////////////////////////////////////////////////
  23.     async function draw(video,context, width, height){
  24.         context.drawImage(video,0,0,width,height);
  25.         const model = await blazeface.load();
  26.         const predictions = await model.estimateFaces(video, false);
  27.         ///////////////////////////////////////////////////////////
  28.           if (predictions.length > 0){
  29.            console.log(predictions);
  30.            for (let i = 0; i < predictions.length; i++) {
  31.             const start = predictions[i].topLeft;
  32.             const end = predictions[i].bottomRight;
  33.             var probability = predictions[i].probability;
  34.             const size = [end[0] - start[0], end[1] - start[1]];
  35.             /////////////////////////////////////////////////////
  36.             context.beginPath();
  37.             context.lineWidth = "5";
  38.             context.strokeStyle="blue";
  39.             context.rect(start[0], start[1],size[0], size[1]);
  40.             /////////////////////////////////////////////////////
  41.             const landmarks = predictions[i].landmarks;
  42.             const right_eye = landmarks[0];
  43.             context.fillRect(right_eye[0],right_eye[1],8,8);
  44.             const left_eye = landmarks[1];
  45.             context.fillRect(left_eye[0],left_eye[1],8,8);
  46.             const nose = landmarks[2];
  47.             context.fillRect(nose[0],nose[1],8,8);
  48.             const mouth = landmarks[3];
  49.             context.fillRect(mouth[0],mouth[1],8,8);
  50.             const right_ear = landmarks[4];
  51.             context.fillRect(right_ear[0],right_ear[1],8,8);
  52.             const left_ear = landmarks[5];
  53.             context.fillRect(left_ear[0],left_ear[1],8,8);
  54.             /////////////////////////////////////////////////////
  55.             context.stroke();
  56.             var prob = (probability[0]*100).toPrecision(5).toString();
  57.             var text = "Confidence:"+prob+"%";
  58.             context.font = "16pt Comic Sans MS";
  59.             context.fillStyle = "#FF0000";
  60.             context.fillText(text,start[0]+5,start[1]+20);}
  61.             //////////////////////////////////////////////////////
  62.            }
  63.         setTimeout(draw,250,video,context,width,height);
  64.         /////////////////////////////////////////////////////////
  65.     }
  66. })();
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement