A comprehensive face spoof detection system built with TensorFlow.js and Node.js. This project detects whether a face image is real or spoofed (printed image, screen display, etc.).
This project implements a deep learning model to classify faces as either:
- Real: Genuine face from a live person
- Spoof/Fake: Fraudulent face from a printed photo, video, or display
The system uses MobileNet-based architecture optimized for real-time inference in web browsers and Node.js environments.
spoof-detection-tfjs/
├── training/ # Model training scripts
│ ├── train.js # Main training script
│ ├── splitdata.js # Dataset splitting utility
│ ├── data.js # Data loading and preprocessing
│ ├── model.js # Model architecture definition
│ ├── visualise.js # Model visualization tools
│ ├── activations/ # Activation maps from model layers
│ └── Sample_image/ # Sample images for training
│
├── testing/ # Inference and testing scripts
│ ├── app.js # Web app backend logic
│ ├── index.html # Main web interface
│ ├── newface.html # Face detection interface
│ └── newspoof.js # Spoof detection logic
│
├── All_models/ # Trained model checkpoints
│ ├── mobilenet-spoof/ # MobileNet-based model
│ ├── model1/ # Model variant 1
│ ├── model13092024/ # Models from different training runs
│ └── model13092024-OM/ # Optimized MobileNet variant
│
├── alldatasets/ # Training datasets
│ ├── dataset/
│ ├── dataset_fullimg/
│ ├── dataset_new/
│ └── dataset_updated/ # Latest dataset (train/val split)
│
├── activations/ # Activation maps and intermediate outputs
├── checkpoints/ # Training checkpoints (best model)
├── video_frames/ # Video frame datasets
└── package.json # Project dependencies
The project uses face datasets organized as:
alldatasets/dataset_updated/
├── train/
│ ├── real/ # Real face images
│ ├── fake/ # Spoofed/fake face images
│ └── ...
└── val/
├── real/
├── fake/
└── ...
Dataset categories include:
- Real images: Genuine faces from live persons
- Fake/Spoof images: Printed photos, screen displays, masks
- Variants: Different cropping (full image vs. face crop), resolutions, and sources
-
Clone/Setup the repository
cd spoof-detection-tfjs -
Install dependencies
npm install
Required packages:
@tensorflow/tfjs-node: TensorFlow.js backend with GPU support@tensorflow/tfjs-core: Core TensorFlow.js libraryjimp: Image processing library@napi-rs/canvas: Canvas rendering for image manipulationcli-progress: Progress bar for training visualization
To train a new model on the dataset:
npm run trainThis will:
- Load the training dataset from
alldatasets/dataset_updated/train - Load validation dataset from
alldatasets/dataset_updated/val - Train the model for specified epochs (default: 50)
- Save checkpoints to
checkpoints/best_model - Save final model to
modelfullframe/
Training script: training/train.js
To split raw data into train/validation split:
npm run splitThis uses the script training/splitdata.js to organize images into proper directory structure.
For Node.js inference:
npm run inferThe project includes a web interface for real-time spoof detection:
-
Main Interface: Open testing/index.html in a web browser
- Displays model predictions
- Shows confidence scores
-
Face Detection Interface: Open testing/newface.html
- Real-time webcam feed
- Face detection using face-api.js
- Face extraction and spoof classification
Key files:
- testing/app.js: Backend logic
- testing/newspoof.js: Spoof detection inference code
The models in this project use:
- Input size: 128x128 pixels (RGB images)
- Architecture: Convolutional Neural Networks (CNN) with layers like:
- SeparableConv2D layers (efficient convolutions)
- MaxPooling for spatial reduction
- Dense layers for classification
- Output: Binary classification (Real/Spoof)
- Optimization: Models optimized for TensorFlow.js with reduced size for web deployment
Model definition: training/model.js
The project includes activation visualizations stored in the activations/ directory showing intermediate layer outputs for:
- Real face samples
- Fake face samples
- Full images
- Cropped face images
These help understand what features the model learns to distinguish real from spoofed faces.
- training/data.js: Data loading, preprocessing, and batching
- training/model.js: Model architecture
- training/train.js: Training loop and optimization
- training/visualise.js: Model visualization utilities
- testing/app.js: Web app backend
- testing/newspoof.js: Spoof classification logic
The models achieve:
- Binary classification accuracy on spoofed vs. real faces
- Real-time inference capability
- Optimized model size for web deployment
- Support for GPU acceleration via TensorFlow.js GPU backend
Multiple trained models are available in All_models/:
- mobilenet-spoof: MobileNet-based architecture
- model13092024: Full model from training run (September 13, 2024)
- model13092024-OM: Optimized MobileNet variant
- Additional variants: Fine-tuned models for specific use cases
Each model directory contains:
model.json: Model architecture and weights manifest- Associated weight files (.bin)
- Node.js: v14 or higher
- Python: For some data preprocessing utilities (optional)
- GPU (optional): NVIDIA GPU for faster training via TensorFlow.js GPU backend
ISC
- Image preprocessing normalizes inputs to [0, 1] range
- Standard training uses 8 batch size with Adam optimizer
- Learning rate and other hyperparameters can be tuned in training scripts
- Web interface requires modern browser with WebRTC support for webcam access
- Face detection uses face-api.js library for accurate face cropping