在检测中出现未定义的情况,因此 facelandmark 无法持续工作
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(); 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) 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; } };
内容来源: justadudewhohacks/face-api.js