Javascript Face-api. js Невозможно распознать лицо - PullRequest
0 голосов
/ 13 июля 2020

Кодовый удар может обнаруживать лица, записывать оценки без ошибок и предупреждений, но он работает без распознавания> (имя метки), и я хочу обнаруживать и распознавать лицо, если оно есть в метке.

Как мне это сделать что?

<script>
        let forwardTimes = []
        let withFaceLandmarks = false
        let withBoxes = true

        function onChangeWithFaceLandmarks(e) {
          withFaceLandmarks = $(e.target).prop('checked')
        }
        function onChangeHideBoundingBoxes(e) {
          withBoxes = !$(e.target).prop('checked')
        }

        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.utils.round(1000 / avgTimeInMs)}`)
        }

        async function onPlay(videoEl) {
          if(!videoEl.currentTime || videoEl.paused || videoEl.ended || !isFaceDetectionModelLoaded())
            return setTimeout(() => onPlay(videoEl))
          const options = getFaceDetectorOptions()
          const ts = Date.now()
          const drawBoxes = withBoxes
          const drawLandmarks = withFaceLandmarks
          let task = faceapi.detectAllFaces(videoEl, options)
          task = withFaceLandmarks ? task.withFaceLandmarks() : task
          const results = await task
          updateTimeStats(Date.now() - ts)
          const canvas = $('#overlay').get(0)
          const dims = faceapi.matchDimensions(canvas, videoEl, true)
          const resizedResults = faceapi.resizeResults(results, dims)
          if (drawBoxes) {
            faceapi.draw.drawDetections(canvas, resizedResults)
          }
          if (drawLandmarks) {
            faceapi.draw.drawFaceLandmarks(canvas, resizedResults)
          }
          setTimeout(() => onPlay(videoEl))
        }

        async function run() {
          // load face detection and face landmark models
          await changeFaceDetector(TINY_FACE_DETECTOR)
              await faceapi.loadSsdMobilenetv1Model('/')
                  await faceapi.loadFaceRecognitionModel('/')
          await faceapi.loadFaceLandmarkModel('/')
          changeInputSize(416)

          // start processing frames
    const labels = ['1','2']
    const labeledFaceDescriptors = await Promise.all(
      labels.map(async label => {
        // fetch image data from urls and convert blob to HTMLImage element
        const imgUrl = `picture/${label}.JPG`
        const img = await faceapi.fetchImage(imgUrl)
        // detect the face with the highest score in the image and compute it's landmarks and face descriptor
        const fullFaceDescription = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
        if (!fullFaceDescription) {
          throw new Error(`no faces detected for ${label}`)
        }
        const faceDescriptors = [fullFaceDescription.descriptor]
        console.log(label)
        return new faceapi.LabeledFaceDescriptors(label, faceDescriptors)
      })
    )
    const input = document.getElementById('inputVideo')
    const fullFaceDescriptions = await faceapi.detectAllFaces(input).withFaceLandmarks().withFaceDescriptors()
    // 0.6 is a good distance threshold value to judge
    // whether the descriptors match or not
    const maxDescriptorDistance = 0.6
    const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, maxDescriptorDistance)
     console.log("face matcher"+faceMatcher)
    const results = fullFaceDescriptions.map(fd => faceMatcher.findBestMatch(fd.descriptor))
    
        results.forEach((bestMatch, i) => {
        const box = fullFaceDescriptions[i].detection.box
        const text = bestMatch.toString()
        const drawBox = new faceapi.draw.DrawBox(box, { label: text })
        console.log("last")
        })
    //  results   
         onPlay($('#inputVideo').get(0))
    }
        function updateResults() {}
        $(document).ready(function() {
          renderNavBar('#navbar', 'video_face_tracking')
          initFaceDetectionControls()
          run()
        })
</script>
...