face-api.js icon indicating copy to clipboard operation
face-api.js copied to clipboard

faceapi.detectSingleFace().withLandmarks() does not always resolve

Open gurupras opened this issue 3 years ago • 1 comments

Sometimes, when changing video source, I have noticed that faceAPI gets stuck forever without resolving/rejecting the promise.

Here's the relevant code:

try {
  result = await new Promise((resolve, reject) => {
    let failed = false
    let faceAPIPromise
    const now = Date.now() // Time now
    // We time out faceAPI in case it doesn't return within 250ms
    const timeout = setTimeout(() => {
      failed = true
      // Store the faceAPI promise to see if it ever resolves
      window.failedPromises = window.failedPromises || []
      window.failedPromises.push(faceAPIPromise)
      reject(new Error('Timed out'))
    }, 250)

    // Make the faceAPI call
    // The 'source' variable is a <video> element
    faceAPIPromise = faceAPI.detectSingleFace(source, faceAPIOptions).withFaceLandmarks().then(results => {
      if (failed) {
        // If failed is set, then we've timed out. Log this anyway
        console.warn(`Timed out call completed in: ${Date.now() - now}ms`)
      } else {
        // Success. Resolved within 250ms
        clearTimeout(timeout)
        resolve(results)
      }
    }).catch(err => {
      // Catch any exception that's thrown and log this
      console.warn(`faceAPI promise errored out in ${Date.now() - now}ms: ${err}`)
    }).then(() => {
      // If this promise is in the list of failed promises, then remove it since it resolved/rejected
      const idx = window.failedPromises.indexOf(faceAPIPromise)
      if (idx > -1) {
        window.failedPromises.splice(idx, 1)
      }
    })
  })
} catch (e) {
  console.warn(`Failed to get face landmarks within 250ms`)
}

window.failedPromises grows indefinitely over time, and very rarely will one of the timed out promises resolve. I'm not sure if this helps, but this tends to occur when the underlying video stream (source) is changing.

gurupras avatar Sep 21 '20 23:09 gurupras

so...any update on that? I'm facing the same thing...it just does nothing forever, never resolving or triggering any error!

felipenmoura avatar Aug 27 '21 15:08 felipenmoura