kabir-mathur-999/anti_spoofing
0
Anti-Spoofing Face Detection
Model Overview
This project implements a real-time anti-spoofing face detection system using a Vision Transformer (ViT) model. The goal is to distinguish between real (live) and spoof (fake, e.g. printed or displayed) faces in webcam or image input.
Workflow
- Data Collection & Preparation
- The dataset consists of two classes:
real_video(real faces) andattack(spoofed faces), each containing images for training and evaluation. - Images are preprocessed and faces are cropped using OpenCV Haar cascades to focus the model on facial regions.
- Model Architecture
- The core model is a Vision Transformer (ViT) loaded from the
timmlibrary, pretrained on ImageNet and fine-tuned for the anti-spoofing task. - The model receives cropped face images as input and outputs a prediction: "Real" or "Fake".
- Training
- The model is trained using the local dataset, with standard augmentation and normalization transforms.
- After training, the model achieves high accuracy on the test set and is saved as a PyTorch checkpoint (
models/antispoof_vit.pth).
- Inference Pipeline
- During inference, an input image is processed as follows:
- The image is converted to OpenCV format and a face is detected and cropped.
- If no face is detected, the system returns "No face detected".
- The cropped face is transformed and passed through the ViT model.
- The model outputs a prediction: "Real" or "Fake".
- Web Application Integration
- The backend is powered by Flask, exposing an API endpoint for predictions.
- The frontend is a web page that captures images from the user's webcam and displays the prediction result in real time.
Summary
The system provides robust, real-time anti-spoofing detection by leveraging modern deep learning (Vision Transformers) and classic face detection, integrated into a user-friendly web application.
2. **Prepare your dataset:**
- Create a `data` folder in the project root
- Add two subfolders: `real_video` and `attack`
- Place your training images in the respective folders
3. **Train the model:**python train_model.py
4. **Run the application:**python app.py
5. **Access the web interface:**
Open your browser and go to `http://localhost:5000`
### 2. Deployment on Render
1. **Push to GitHub:**
- Create a new repository on GitHub
- Push all files to the repository
2. **Deploy on Render:**
- Go to [Render.com](https://render.com)
- Create a new Web Service
- Connect your GitHub repository
- Render will automatically detect the `render.yaml` configuration
3. **Upload trained model:**
- After training locally, you'll need to upload the `models/antispoof_vit.pth` file
- You can do this through Render's file upload or by committing it to your repository
## Usage
1. **Start Camera:** Click the "Start Camera" button to access your webcam
2. **Position Face:** Make sure your face is clearly visible in the camera view
3. **Capture & Analyze:** Click "Capture & Analyze" to take a photo and get results
4. **View Results:** The system will show:
- Prediction (Real or Fake)
- Confidence percentage
- Whether a face was detected
## Model Details
- **Architecture:** Vision Transformer (ViT) Tiny
- **Input Size:** 224x224 pixels
- **Classes:** 2 (Real, Fake)
- **Preprocessing:** Face detection and cropping using Haar cascades
## Dataset Requirements
Your dataset should be organized as follows:
- `data/real_video/`: Images of real faces
- `data/attack/`: Images of fake faces (photos of photos, screens, etc.)
Supported image formats: PNG, JPG, JPEG
## API Endpoints
- `GET /`: Web interface
- `POST /predict`: Image analysis endpoint
- `GET /health`: Health check endpoint
## Security Considerations
- The model runs inference on the server side
- Images are processed in memory and not stored
- HTTPS should be enabled in production
## Troubleshooting
1. **Camera not working:** Ensure your browser supports WebRTC and you've granted camera permissions
2. **Model not found:** Make sure to train the model first using `train_model.py`
3. **Poor accuracy:** Ensure your training dataset is balanced and high-quality
## License
This project is for educational and research purposes.
