Skip to content

This repository contains the model development code for Face Spoof Detection.

Notifications You must be signed in to change notification settings

venkatdurga/Face_Spoof_Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spoof Detection TensorFlow.js

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.).

Overview

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.

Project Structure

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

Dataset Structure

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

Installation

  1. Clone/Setup the repository

    cd spoof-detection-tfjs
  2. Install dependencies

    npm install

    Required packages:

    • @tensorflow/tfjs-node: TensorFlow.js backend with GPU support
    • @tensorflow/tfjs-core: Core TensorFlow.js library
    • jimp: Image processing library
    • @napi-rs/canvas: Canvas rendering for image manipulation
    • cli-progress: Progress bar for training visualization

Usage

Training

To train a new model on the dataset:

npm run train

This 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

Dataset Preparation

To split raw data into train/validation split:

npm run split

This uses the script training/splitdata.js to organize images into proper directory structure.

Inference/Testing

For Node.js inference:

npm run infer

Web-Based Testing

The project includes a web interface for real-time spoof detection:

  1. Main Interface: Open testing/index.html in a web browser

    • Displays model predictions
    • Shows confidence scores
  2. Face Detection Interface: Open testing/newface.html

    • Real-time webcam feed
    • Face detection using face-api.js
    • Face extraction and spoof classification

Key files:

Model Architecture

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

Activation Maps

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.

Key Files

Performance

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

Models Available

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)

Requirements

  • Node.js: v14 or higher
  • Python: For some data preprocessing utilities (optional)
  • GPU (optional): NVIDIA GPU for faster training via TensorFlow.js GPU backend

License

ISC

Notes

  • 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

About

This repository contains the model development code for Face Spoof Detection.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published