Skip to content
Advertisement

expo FaceDetector keep triggering “onFacesDetected” event in “accurate” mode even without face

I’m new to react native. I’m using expo FaceDetector to detect faces. when I’m using it in “fast” mode it trigger “onFacesDetected” event correctly. But when I’m using “accuratemodeonFacesDetected” event keep triggering (on “minDetectionInterval“) (it suppose to trigger after detecting a face).

Is this a expo issue or my code is wrong ? Any help would be greatly appreciated. 1.below is fast mode code

    <Camera style={styles.camara} type={type}
            ref={ref}
            onFacesDetected={faceDetected}
            faceDetectorSettings={{
                mode: FaceDetector.Constants.Mode.fast,
                detectLandmarks: FaceDetector.Constants.Landmarks.all,
                 runClassifications: FaceDetector.Constants.Classifications.all,
                minDetectionInterval: 100,
                tracking: false,
              }}>
        </Camera>

2.below is accurate mode code

       <Camera style={styles.camara} type={type}
            ref={ref}
            onFacesDetected={faceDetected}
            faceDetectorSettings={{
                mode: FaceDetector.Constants.Mode.accurate,
                detectLandmarks: FaceDetector.Constants.Landmarks.all,
                 runClassifications: FaceDetector.Constants.Classifications.all,
                minDetectionInterval: 100,
                tracking: false,
              }}>
        </Camera>

expo documentation expo documentation

Advertisement

Answer

I think this may help. The problem is that onFacesDetected returns an object, not a boolean value.

const [faceDetected, setFaceDetected] = useState(false)
const checkForFace = (obj) => {
  try {
    setFaceDetected(obj.faces.length==0?false:true);
    //or
    setFaceDetected(obj.faces.length);
    //0 is false and any natural number(1,2,3...) is true
  } catch (error) {
    console.error(error);
  }
}

return (       
  <Camera style={styles.camara} type={type}
      ref={ref}
      onFacesDetected={(e)=>checkForFace(e)}
      faceDetectorSettings={{
          mode: FaceDetector.Constants.Mode.accurate,
          detectLandmarks: FaceDetector.Constants.Landmarks.all,
          runClassifications: FaceDetector.Constants.Classifications.all,
          minDetectionInterval: 500,
          tracking: true,
      }}>
  </Camera>
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement