Advertisement
Guest User

CompreFace jQuery demo

a guest
May 8th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.11 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
  5.     <script type="text/javascript">
  6.         function video() {
  7.             var video = $("#live").get()[0];
  8.             var canvas = $("#canvas");
  9.             var canvas2 = $("#canvas2");
  10.             var ctx = canvas.get()[0].getContext('2d');
  11.             var ctx2 = canvas2.get()[0].getContext('2d');
  12.  
  13.             navigator.mediaDevices.getUserMedia({
  14.                 video: { width: 640, height: 480 }
  15.             }).then(function(stream) {
  16.                 video.srcObject = stream;
  17.               });
  18.  
  19.             function draw() {
  20.                 ctx.drawImage(video, 0, 0, 640, 480);
  21.                 canvas[0].toBlob(function(blob){
  22.                     blob.name = "blob.jpeg"
  23.                     var fd = new FormData();
  24.                     fd.append('file', blob, "blob.jpeg");
  25.                     $.ajax({
  26.                       type: "POST",
  27.                       url: "http://localhost:8000/api/v1/detection/detect?face_plugins=landmarks2d106",
  28.                       data: fd,
  29.                       dataType: 'multipart/form-data',
  30.                       headers: {
  31.                         "x-api-key": "f5e06ab3-ef59-495e-9731-82cd52f28aa1"
  32.                       },
  33.                         processData: false,
  34.                         contentType: false
  35.                     }).always(function( data ) {
  36.                         const evt = new Event("next_frame", {"bubbles":true, "cancelable":false});
  37.                         document.dispatchEvent(evt);
  38.                         var body = JSON.parse(data.responseText);
  39.                         var box = JSON.parse(data.responseText).result[0].box;
  40.                         ctx2.clearRect(0, 0, 640, 480);
  41.                         ctx2.drawImage(video, 0, 0, 640, 480);
  42.                         ctx2.strokeStyle = 'green';
  43.                         ctx2.strokeRect(box.x_min, box.y_min, box.x_max - box.x_min, box.y_max - box.y_min);
  44.                       });
  45.                 }, 'image/jpeg', 0.95);
  46.             }
  47.  
  48.             document.addEventListener("next_frame", draw);
  49.  
  50.             const evt = new Event("next_frame", {"bubbles":true, "cancelable":false});
  51.             document.dispatchEvent(evt);
  52.            
  53.         }
  54.        
  55.  
  56.     </script>
  57.     <title>test</title>
  58. </head>
  59. <body>
  60.     <button onclick="video()">video</button>
  61. <video id="live" width="640" height="480" autoplay style="display:none;"></video>
  62. <canvas width="640" id="canvas" height="480" style="display:none;"></canvas>
  63. <canvas width="640" id="canvas2" height="480"></canvas>
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement