我正在使用face-api.js为一个人而不是一张照片进行人脸识别。然而,如果用户放入一张照片,它就能够识别它。我该怎么做呢?
发布于 2020-07-14 18:24:37
我的脚本是什么
<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 = `surati/${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 })
drawBox.draw(document.getElementById('overlay'))        console.log(text)   }) //  results  
 
     onPlay($('#inputVideo').get(0)) }
    function updateResults() {}
    $(document).ready(function() {
      renderNavBar('#navbar', 'video_face_tracking')
      initFaceDetectionControls()
      run()
    })
            </script>它可以检测人脸,如果你打开浏览器控制台,你可以看到它的can识别,但是错误的..也许它可以帮助你.... ,如果你有好的代码,也许剪切到我的?
https://stackoverflow.com/questions/61596260
复制相似问题