这是indexloc提供的服务,不要输入任何密码
Skip to content

getting undefined in detection so facelandmark is not working continously #961

@VijayalakshmiNagaraj97

Description

@VijayalakshmiNagaraj97

useEffect(() => {
if (isModelLoaded) {
const interval = setInterval(() => detectFaceChange(), 1000); // Detect every second
return () => clearInterval(interval); // Cleanup
}
}, [isModelLoaded]);
const loadModels = async () => {

try {
  const MODEL_URL = '/models';
  await faceapi.nets.tinyFaceDetector.loadFromUri(MODEL_URL);
  await faceapi.nets.faceLandmark68Net.loadFromUri(MODEL_URL);
  await faceapi.nets.faceRecognitionNet.loadFromUri(MODEL_URL);
  setIsModelLoaded(true);
} catch (error) {
  console.error('Error loading models:', error);
}

};

const detectFaceChange = async () => {
if (!videoRef.current) return;

const detection = await faceapi
  .detectSingleFace(videoRef.current, new faceapi.TinyFaceDetectorOptions())
  .withFaceLandmarks()
  .withFaceDescriptor();
  console.log('angledetection', detection);  
if (detection) {
  const {alignedRect, landmarks, descriptor} = detection;

  if (!alignedRect || !landmarks) return; // Ensure detection is valid

  // Check if the face is straight using landmarks
  const leftEye = landmarks.getLeftEye();
  const rightEye = landmarks.getRightEye();
  const nose = landmarks.getNose();

  if (!leftEye || !rightEye || !nose) return; // Ensure landmarks are valid

  // Calculate the angle between the eyes
  const eyeDistance = Math.abs(leftEye[0].x - rightEye[0].x);
  const eyeVerticalDiff = Math.abs(leftEye[0].y - rightEye[0].y);
  const angle = Math.atan2(eyeVerticalDiff, eyeDistance) * (180 / Math.PI);
  // Only process faces with an acceptable angle (e.g., less than 15 degrees)
  console.log('angle', angle);  
  if (angle > 10) {
    if(isCheckFaceAngleRef.current?.terminating_count > 0){
    props.faceAngleRef.current=true
    faceAngleTimeoutRef.current = setTimeout(() => {
        props.takeScreenshot(
          'interview_proxy_items_face_angle_alert',
          'Face Angle Alert',
        );
    }, 200);
  }
  } else {
    clearTimeout(faceAngleTimeoutRef.current)
    props.closeModal()
    props.faceAngleRef.current=false
    // setErrorText('');
  }

  const currentDescriptor = descriptor;

  if (previousDescriptor) {
    const distance = faceapi.euclideanDistance(
      previousDescriptor,
      currentDescriptor,
    );

    if (distance > 0.6) {
      // Adjust threshold based on testing
      console.log('Person has changed!');
    }
  }

  previousDescriptor = currentDescriptor;
}

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions