<!DOCTYPE html>
<html>
<head>
  <script src="face-api.js"></script>
  <script src="commons.js"></script>
  <link rel="stylesheet" href="styles.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.css">
  <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
</head>
<body>
  <div id="navbar"></div>
  <div class="center-content page-container">
    <div class="progress" id="loader">
      <div class="indeterminate"></div>
    </div>
    <div style="position: relative" class="margin">
      <video src="media/bbt.mp4" onplay="onPlay(this)" id="inputVideo" autoplay muted></video>
      <canvas id="overlay" />
    </div>
    <div class="row side-by-side">
      <div class="row">
        <label for="minConfidence">Min Confidence:</label>
        <input disabled value="0.7" id="minConfidence" type="text" class="bold">
      </div>
      <button
        class="waves-effect waves-light btn"
        onclick="onDecreaseThreshold()"
      >
        <i class="material-icons left">-</i>
      </button>
      <button
        class="waves-effect waves-light btn"
        onclick="onIncreaseThreshold()"
      >
        <i class="material-icons left">+</i>
      </button>
    </div>
    <div class="row side-by-side">
      <div class="row">
        <label for="time">Time:</label>
        <input disabled value="-" id="time" type="text" class="bold">
      </div>
      <div class="row">
        <label for="fps">Estimated Fps:</label>
        <input disabled value="-" id="fps" type="text" class="bold">
      </div>
    </div>
  </div>

  <script>
    let minConfidence = 0.7
    let modelLoaded = false
    let result

    let forwardTimes = []

    function updateTimeStats(timeInMs) {
      forwardTimes = [timeInMs].concat(forwardTimes).slice(0, 30)
      const avgTimeInMs = forwardTimes.reduce((total, t) => total + t) / forwardTimes.length
      $('#time').val(`${Math.round(avgTimeInMs)} ms`)
      $('#fps').val(`${faceapi.round(1000 / avgTimeInMs)}`)
    }

    function onIncreaseThreshold() {
      minConfidence = Math.min(faceapi.round(minConfidence + 0.1), 1.0)
      $('#minConfidence').val(minConfidence)
    }

    function onDecreaseThreshold() {
      minConfidence = Math.max(faceapi.round(minConfidence - 0.1), 0.1)
      $('#minConfidence').val(minConfidence)
    }

    async function onPlay(videoEl) {
      if(videoEl.paused || videoEl.ended || !modelLoaded)
        return false

      const { width, height } = faceapi.getMediaDimensions(videoEl)
      const canvas = $('#overlay').get(0)
      canvas.width = width
      canvas.height = height

      const ts = Date.now()
      result = await faceapi.locateFaces(videoEl, minConfidence)
      updateTimeStats(Date.now() - ts)

      faceapi.drawDetection('overlay', result.map(det => det.forSize(width, height)))
      setTimeout(() => onPlay(videoEl))
    }

    async function run() {
      await faceapi.loadFaceDetectionModel('/')
      modelLoaded = true
      onPlay($('#inputVideo').get(0))
      $('#loader').hide()
    }

    $(document).ready(function() {
      renderNavBar('#navbar', 'face_detection_video')
      run()
    })
  </script>
</body>
</html>