eKYC & MediaPipe Face Liveness - Documentation
Giới thiệu
Bộ tài liệu chi tiết về implementation eKYC với face liveness detection sử dụng MediaPipe FaceMesh trong Vue 3 + TypeScript + Vite.
Tổng quan
Hệ thống eKYC cho phép xác thực danh tính trực tuyến qua:
- ✅ OCR từ ảnh CCCD (front/back)
- ✅ Face liveness detection với MediaPipe
- ✅ Face verification với backend
- ✅ Address verification và ward lookup
Cấu trúc bài học (16 bài)
🎯 00. Case Study - Executive Summary
TL;DR cho Leaders và Stakeholders
- Vấn đề kỹ thuật & giải pháp
- Metrics & ROI
- Lessons learned
- Production deployment
Thời gian: 15-20 phút
Level: All levels
Format: Template-based case study
Phần 1: Foundations (Nền tảng)
01. Tổng quan hệ thống eKYC
- Kiến trúc tổng thể
- Stack công nghệ
- Flow hoạt động cơ bản
- Thách thức kỹ thuật
Thời gian: 15-20 phút
Level: Beginner
Mục tiêu: Hiểu big picture
02. Chi tiết từng bước trong flow
- 5 steps: Preparation → Front → Back → Face → Review
- State management
- Step navigation
- Auto-progression và validation
Thời gian: 30-40 phút
Level: Beginner → Intermediate
Mục tiêu: Hiểu flow chi tiết với code examples
Phần 2: MediaPipe & Camera (Core Implementation)
03. Giới thiệu MediaPipe FaceMesh
- MediaPipe là gì?
- Tại sao chọn MediaPipe?
- Cách hoạt động (468 landmarks)
- Configuration options
- So sánh với alternatives
Thời gian: 25-30 phút
Level: Beginner
Mục tiêu: Hiểu MediaPipe fundamentals
04. Xây dựng MediaPipe Wrapper cho Vue
- Vấn đề: Vue Proxy vs WASM
- Architecture của wrapper class
- Implementation chi tiết
- Best practices
- Full Vue component example
Thời gian: 45-60 phút
Level: Intermediate → Advanced
Mục tiêu: Master wrapper pattern
05. Camera Pipeline và Detection Loop
- Camera access và permissions
- Detection loop optimization (setInterval vs RAF)
- Adaptive frame rate
- Video processing và capture
- UI feedback loop
- Performance monitoring
Thời gian: 40-50 phút
Level: Intermediate
Mục tiêu: Master camera và detection pipeline
06a. Face Detection & Angle Calculation - Fundamentals
- Face detection basics (ML, CNN, MediaPipe)
- 468 landmarks explained
- Toán học: Vector operations, Cross product, Normalization
- Rotation matrix construction
- Euler angles (Yaw, Pitch, Roll)
- Step-by-step calculation với số cụ thể
- Trích dẫn papers & books (MediaPipe, Three.js, Computer Vision)
Thời gian: 60-90 phút
Level: Beginner → Advanced
Mục tiêu: Hiểu toán học và ML đằng sau face detection
06b. Liveness Calculation và Anti-Spoofing
- ✅ [THỰC TẾ] vs 💡 [ĐỀ XUẤT] code
- Active liveness (face actions)
- Frame stability check
- Face distance check
- Backend anti-spoofing
- Validation thresholds
- Complete UI flow
Thời gian: 35-45 phút
Level: Intermediate
Mục tiêu: Hiểu liveness workflow thực tế trong portal
07. Backend Integration: OCR & Face Verify APIs
- API overview và architecture
- OCR Front/Back APIs
- Face Verify API
- Check2FA và Ward Search
- Error handling patterns
- Timeout & retry strategies
Thời gian: 40-50 phút
Level: Intermediate
Mục tiêu: Master API integration
08a. Xử Lý Ward/District/Province
- Data structure (wards.json)
- Text normalization (remove diacritics)
- Prefix extraction (Phường/Xã/Thị trấn)
- Word boundary regex matching
- Position-based priority
- Duplicate removal
- Complete algorithm với examples
Thời gian: 45-60 phút
Level: Advanced
Mục tiêu: Hiểu chi tiết xử lý địa chỉ Việt Nam
08b. Review Step: Check2FA và Ward Logic
- Review step overview
- Check2FA logic
- Ward search flow
- Address update warnings
- UI implementation
- Edge cases handling
Thời gian: 35-45 phút
Level: Intermediate
Mục tiêu: Implement review step correctly
09. I18n và UX Patterns
- Internationalization setup
- Locale management
- UX patterns (loading, errors, empty states)
- Mobile optimization
- Accessibility (keyboard, ARIA, focus)
- Performance UX
Thời gian: 30-40 phút
Level: Beginner → Intermediate
Mục tiêu: Polish UX và i18n
Phần 3: Vue Specific
10. MediaPipe với Vue: Ưu và Nhược điểm
- ✅ Pros: Reactivity, Composition API, Template syntax
- ❌ Cons: Proxy issues, Build complexity, Bundle size
- So sánh với React
- Recommendations
- Best practices
Thời gian: 20-30 phút
Level: Intermediate
Mục tiêu: Hiểu trade-offs khi dùng Vue
Phần 4: Production Issues & Solutions ⚠️
Quan trọng: Đây là phần khó nhất và quan trọng nhất!
11. Vấn đề Build Production
- ❌ Triệu chứng: "FaceMesh is not a constructor"
- 🔍 Root cause analysis
- 📊 Debug process
- 🧪 So sánh dev vs production
- ⚠️ Tại sao chỉ xảy ra với MediaPipe?
Thời gian: 30-40 phút
Level: Advanced
Mục tiêu: Hiểu root cause của production bug
12. Giải pháp Workaround: Vite Plugin
- ✅ Strategy: 3-Layer Defense
- Layer 1: Flexible Import
- Layer 2: Vite Plugin
- Layer 3: Build Config
- 🛠️ Implementation từng layer
- ✅ Verification steps
- 🐛 Troubleshooting
- 💡 Alternative solutions
Thời gian: 60-90 phút
Level: Advanced
Mục tiêu: Implement và understand workaround
13. Tích hợp Thư viện - Flow Dữ liệu Chi tiết
- Danh sách thư viện được sử dụng (MediaPipe, Three.js, Axios, Vue)
- Flow dữ liệu chi tiết từ thư viện này sang thư viện khác
- Code examples thực tế từ dự án Portal
- Sơ đồ minh họa luồng xử lý
- Data types & interfaces
- Performance & optimization
- Error handling patterns
Thời gian: 45-60 phút
Level: Intermediate → Advanced
Mục tiêu: Hiểu rõ cách các thư viện tương tác và dữ liệu được xử lý
Quick Start
Nếu bạn là Leader/Stakeholder
- 🎯 Case Study - TL;DR (vấn đề, giải pháp, metrics, ROI)
Nếu bạn mới bắt đầu
- Tổng quan hệ thống - Hiểu bức tranh tổng thể
- Chi tiết flow - Hiểu từng bước
- Giới thiệu MediaPipe - Hiểu MediaPipe
- Face Detection Fundamentals - Hiểu toán học và ML
Nếu bạn đang triển khai
- Wrapper class - Xây dựng wrapper
- Camera pipeline - Thiết lập camera
- Backend integration - Tích hợp API
- Review step - Triển khai bước xem lại
- I18n & UX - Hoàn thiện UX
- Library Integration - Hiểu flow dữ liệu giữa các thư viện
Nếu gặp lỗi production
- Production issue - Hiểu vấn đề
- Vite plugin fix - Áp dụng giải pháp
Nếu cần tối ưu hóa
- Camera pipeline - Tối ưu vòng lặp phát hiện
- Liveness calculation - Cải thiện độ chính xác
- I18n & UX - Hiệu suất UX
- Library Integration - Tối ưu flow dữ liệu
Prerequisites
Kiến thức cần có
- ✅ Vue 3: Composition API,
<script setup>, reactivity - ✅ TypeScript: Interfaces, types, generics
- ✅ Vite: Cơ bản về công cụ build
- ⚠️ WASM: Hiểu biết cơ bản (hữu ích)
- ⚠️ Rollup: Quy trình bundle (hữu ích)
Môi trường
Node.js: >= 18.x
npm: >= 9.x
Vue: 3.x
Vite: 5.x
TypeScript: >= 5.xKey Takeaways
✅ Điểm mạnh
- MediaPipe: Độ chính xác cao, ưu tiên quyền riêng tư, miễn phí
- Vue 3: Reactivity tốt, template rõ ràng
- Wrapper pattern: Tách biệt WASM khỏi Vue Proxy
- 3-layer defense: Giải pháp mạnh mẽ cho production
⚠️ Điểm cần lưu ý
- Vấn đề Proxy: Không dùng ref/reactive với WASM
- Độ phức tạp build: Cần plugin Vite tùy chỉnh
- Kích thước bundle: ~7MB cho MediaPipe
- Kiểm thử production: Luôn kiểm thử build production!
🎯 Best Practices
// ✅ DO
let wrapper: MediaPipeFaceMeshWrapper | null = null;
wrapper = new MediaPipeFaceMeshWrapper();
// ❌ DON'T
const wrapper = ref(new MediaPipeFaceMeshWrapper());// ✅ DO
if (videoRef.value) {
wrapper.setVideoElement(videoRef.value);
}
// ❌ DON'T
wrapper.setVideoElement(toRaw(videoRef));# ✅ DO
npm run build-production
npm run preview:proxy
# Test thoroughly before deploy
# ❌ DON'T
npm run dev # Only test dev
git push # Deploy without testing prodTham khảo nhanh xử lý sự cố
| Vấn đề | Giải pháp | Liên kết |
|---|---|---|
| Cần tóm tắt điều hành | Đọc case study trước | #00 |
| FaceMesh không phải constructor | Áp dụng 3-layer defense | #12 |
| Module không tìm thấy | Kiểm tra đường dẫn locateFile | #03 |
| Lỗi Vue Proxy | Dùng wrapper class | #04 |
| Kích thước build quá lớn | Kiểm tra manualChunks | #12 |
| Camera không hoạt động | Kiểm tra quyền | #04 |
| Không tìm thấy phường/xã | Kiểm tra chuẩn hóa + regex | #08a |
| Nhiều phường/xã được trả về | Kiểm tra ranh giới từ | #08a |
| Phát hiện khuôn mặt không ổn định | Điều chỉnh ngưỡng | #06b |
| Không hiểu toán học | Đọc fundamentals trước | #06a |
| Không hiểu flow dữ liệu | Đọc library integration | #13 |
Kho lưu trữ ví dụ code
Tất cả ví dụ trong docs đều dựa trên code thực tế từ:
Frontend:
portal-client/src/pages/information/directive/EkycSettingEnhanced.vueportal-client/src/utils/ekyc/MediaPipeFaceMeshWrapper.tsportal-client/vite.config.tsportal-client/mediaPipePlugin.ts
Backend:
portal-server/src/services/client/EkycService.tsportal-server/src/services/client/WardService.tsportal-server/src/utils/MyFunction.ts(normalizeVietnameseText)portal-server/src/utils/wards.json
Phản hồi & Đóng góp
Tài liệu này được viết dựa trên kinh nghiệm thực tế khi triển khai eKYC.
Nếu bạn:
- ❓ Có câu hỏi
- 🐛 Tìm thấy lỗi
- 💡 Có ý tưởng cải thiện
- ✅ Muốn bổ sung
→ Liên hệ team hoặc tạo issue.
Giấy phép
Tài liệu nội bộ cho dự án portal iNET.
Happy coding! 🚀
Documentation này được viết dựa trên kinh nghiệm thực tế khi implement eKYC.
Nếu bạn:
- ❓ Có câu hỏi
- 🐛 Tìm thấy lỗi
- 💡 Có ý tưởng cải thiện
- ✅ Muốn bổ sung
→ Contact team hoặc tạo issue.
License
Internal documentation for iNET portal project.
Happy coding! 🚀