Chọn một theme bên dưới để mở popup xác thực khuôn mặt. Camera sẽ bật lên, hướng dẫn người dùng đưa khuôn mặt vào đúng vị trí, đủ sáng và tiến hành xác thực.
Tùy chỉnh toàn bộ màu sắc của popup FaceAuth bằng options trong JavaScript. Bên trái chọn màu, bên phải là code mẫu.
const faceAuth = new FaceAuth({
primaryColor: '#3a86ff',
successColor: '#2ed573',
warningColor: '#ffa502',
errorColor: '#ff4757',
textColor: '#ffffff',
subtitleColor: '#b0b0b0',
backgroundColor: '#1e1e1e',
overlayColor: 'rgba(0, 0, 0, 0.8)'
});
await faceAuth.open();
function FaceAuthButton() {
const handleAuth = async () => {
const faceAuth = new FaceAuth({
primaryColor: '#3a86ff',
successColor: '#2ed573',
warningColor: '#ffa502',
errorColor: '#ff4757',
textColor: '#ffffff',
subtitleColor: '#b0b0b0'
});
faceAuth.config.onCapture = (blob) => {
console.log('Captured:', blob);
};
await faceAuth.open();
};
return (
<button onClick={handleAuth}>
Xác Thực Khuôn Mặt
</button>
);
}
Tích hợp FaceAuth vào dự án hiện có bằng CDN hoặc NPM. Dưới đây là các cách đơn giản nhất để dùng.
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.18.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/face-landmarks-detection@0.0.1/dist/face-landmarks-detection.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/face-auth-library@latest/dist/face-auth.min.js"></script>
npm install face-auth-library
npm install @tensorflow/tfjs
npm install @tensorflow-models/face-landmarks-detection
<button onclick="authenticateUser()">
Xác Thực Khuôn Mặt
</button>
<script>
async function authenticateUser() {
try {
const faceAuth = new FaceAuth();
faceAuth.config.onCapture = (imageBlob) => {
console.log('OK:', imageBlob);
// uploadToServer(imageBlob);
};
faceAuth.config.onError = (error) => {
console.error('Lỗi:', error);
};
await faceAuth.open();
} catch (error) {
console.error('Init error:', error);
}
}
</script>
import React, { useCallback } from 'react';
function FaceAuthButton() {
const handleFaceAuth = useCallback(async () => {
const faceAuth = new FaceAuth({
detectionInterval: 150,
voiceEnabled: true
});
faceAuth.config.onCapture = (blob) => {
console.log('Authenticated:', blob);
};
faceAuth.config.onError = (error) => {
console.error('Error:', error);
};
await faceAuth.open();
}, []);
return (
<button onClick={handleFaceAuth}>
<i className="fas fa-camera"></i>
Xác Thực Khuôn Mặt
</button>
);
}
export default FaceAuthButton;
<template>
<button @click="handleFaceAuth">
<i class="fas fa-camera"></i>
Xác Thực Khuôn Mặt
</button>
</template>
<script>
export default {
methods: {
async handleFaceAuth() {
const faceAuth = new FaceAuth({
voiceEnabled: true
});
faceAuth.config.onCapture = (blob) => {
console.log('Authenticated:', blob);
};
await faceAuth.open();
}
}
};
</script>
Các options, method và callback quan trọng của FaceAuth.
const faceAuth = new FaceAuth({
// Detection
detectionInterval: 200,
progressStep: 1,
progressIntervalTime: 100,
// Face size (tỉ lệ theo video)
minFaceSize: 0.3,
maxFaceSize: 0.6,
// Center threshold
centerThreshold: 0.15,
warningThreshold: 0.25,
// Colors
primaryColor: '#3a86ff',
successColor: '#2ed573',
warningColor: '#ffa502',
errorColor: '#ff4757',
textColor: '#ffffff',
subtitleColor: '#b0b0b0',
backgroundColor: '#1e1e1e',
overlayColor: 'rgba(0, 0, 0, 0.8)',
// Callbacks
onCapture: null,
onClose: null,
onError: null,
voiceEnabled: true
});
// Mở popup & khởi tạo camera + model
await faceAuth.open();
// Đóng popup & tắt camera
faceAuth.close();
// Cập nhật config trong runtime
faceAuth.updateConfig({
progressStep: 2,
voiceEnabled: false
});
// Kiểm tra popup đang mở hay đã đóng
const opened = faceAuth.isOpen();
faceAuth.config.onCapture = (imageBlob) => {
// imageBlob là Blob ảnh JPEG
const formData = new FormData();
formData.append('faceImage', imageBlob, 'face.jpg');
fetch('/api/verify-face', {
method: 'POST',
body: formData,
});
};
faceAuth.config.onError = (error) => {
console.error('FaceAuth error:', error.message);
// Hiển thị thông báo lỗi cho người dùng
alert('Lỗi xác thực: ' + error.message);
};
faceAuth.config.onClose = () => {
console.log('Popup đã đóng, camera & model đã cleanup');
// Thực hiện hành động sau khi đóng
};
const faceAuth = new FaceAuth();
await faceAuth.open();
// Sau 3s tăng tốc độ progress & tắt voice
setTimeout(() => {
faceAuth.updateConfig({
progressStep: 2,
voiceEnabled: false
});
}, 3000);