Face Liveness Detection Application using DeepFace and FastAPI
- ✅ Real-time face liveness detection using DeepFace
- ✅ Camera view with responsive circular frame
- ✅ Color border indicators (green = success, red = failed, orange = scanning)
- ✅ Face quality analysis with improvement recommendations
- ✅ Automatic photo saving to
storage/liveness/ - ✅ Real-time notifications without popups
- ✅ "Next Process" button after successful detection
- ✅ Responsive design with Bootstrap 5
- Backend: FastAPI with Python 3.12+
- Frontend: HTML5, CSS3, Bootstrap 5, jQuery
- Liveness Detection: DeepFace with TensorFlow/Keras
- Computer Vision: OpenCV
- Package Manager: UV
- Python 3.12 or higher
- UV package manager (https://docs.astral.sh/uv/)
- Web/USB camera
- Clone this repository:
git clone <repository-url>
cd poc-liveness- Install dependencies with UV:
uv sync --python 3.12- Activate virtual environment:
source .venv/bin/activate # For Linux/Mac
# or
.venv\Scripts\activate # For Windowsuv run python -m app.mainThe application will run at http://localhost:8002
uvicorn app.main:app --host 0.0.0.0 --port 8002 --workers 4You can easily run this application using Docker.
In the root directory of the project, run:
docker build -t web-liveness-app:1.0.0 -f Dockerfile .To start the application (and optional dependencies such as Redis), use:
docker compose up -dThis will build and start all services defined in docker-compose.yml.
The application will be available at http://localhost:8002.
Note:
- Ensure ports and volumes specified in
docker-compose.ymlare available on your system. - The app’s persistent data (such as liveness images) is stored in the
storagedirectory, mapped as a Docker volume.
GET /- Main application page (http://localhost:8002)GET /api/health- Health checkPOST /api/liveness/check- Check liveness from base64 imagePOST /api/liveness/capture- Save successful liveness photoPOST /api/liveness/quality- Analyze face quality
poc-liveness/
├── app/
│ ├── __init__.py
│ ├── main.py # Main FastAPI application
│ ├── api/
│ │ ├── __init__.py
│ │ └── routes.py # API endpoints
│ ├── core/
│ │ ├── __init__.py
│ │ └── config.py # Configuration settings
│ ├── services/
│ │ ├── __init__.py
│ │ └── liveness_service.py # Liveness detection logic
│ └── utils/
│ └── __init__.py
├── static/
│ ├── css/
│ │ └── style.css # Custom styles
│ └── js/
│ └── main.js # JavaScript functionality
├── templates/
│ └── index.html # Main HTML page
├── storage/
│ └── liveness/ # Saved liveness images
├── pyproject.toml # Project configuration
└── README.md # This file
- Open browser and access
http://localhost:8002 - Grant camera permission when prompted
- Position your face within the available circle
- System will automatically detect liveness every 2 seconds
- Follow recommendations if face quality is poor
- If liveness is successfully detected:
- Circle border will turn green
- Success notification will appear
- Photo will be saved automatically
- "Next Process" button will appear
Configuration can be changed in app/core/config.py:
LIVENESS_CONFIDENCE_THRESHOLD: Confidence threshold for liveness detection (default: 0.90)LIVENESS_MODEL: Model used for detection (default: "VGG-Face")LIVENESS_DETECTOR_BACKEND: Backend for face detection (default: "opencv")STORAGE_PATH: Path for file storageLIVENESS_STORAGE_PATH: Specific path for liveness photos
-
Cannot access camera
- Ensure browser has camera permission
- Check if other applications are using the camera
- Use HTTPS in production environment
-
Import error with tf-keras
uv sync --python 3.12
Ensure
tf-kerasis properly installed -
Port already in use
- Change port in
app/main.pyor kill process using port 8002
- Change port in
-
DeepFace model download slow
- Ensure stable internet connection
- Models will be downloaded on first run and saved in
~/.deepface/weights/
uv tree # To see dependency tree
uv sync # To reinstall dependenciesuv add <package-name># Test health endpoint
curl http://localhost:8002/api/health
# Test main page
curl http://localhost:8002/- Ensure HTTPS is used in production environment
- Validate and sanitize all received input
- Rate limiting for API endpoints
- Secure file upload handling
[Add your license information here]
For questions or issues, please contact the development team.