https://github.com/vid852005/face-detection-using-siamese-neural-network
https://github.com/vid852005/face-detection-using-siamese-neural-network
cnn computer-vision deep-learning face-detection face-recognition keras-tensorflow machine-learning neural-network nn opencv siamese-neural-network tensorflow
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/vid852005/face-detection-using-siamese-neural-network
- Owner: Vid852005
- Created: 2025-06-24T14:28:33.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-24T15:14:13.000Z (12 months ago)
- Last Synced: 2025-07-22T11:35:48.524Z (11 months ago)
- Language: Python
- Size: 6.84 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Face Detection and Verification using Siamese Neural Network
## Overview
This project implements a face verification system using a Siamese Neural Network. The system can be trained on your own face images and then deployed as a web application using Flask, allowing users to verify their identity by uploading a photo.
- **Anchor**: Reference images (usually your own face, collected via webcam)
- **Positive**: More images of the same person (collected via webcam)
- **Negative**: Images of other people (e.g., from the [LFW dataset](https://www.kaggle.com/datasets/jessicali9530/lfw-dataset))
The Siamese network learns to distinguish between images of the same person and different people by comparing anchor-positive and anchor-negative pairs.
## Project Structure
```
project-root/
│
├── app.py # Flask backend for web interface
├── Facedetection.py # Training script for Siamese model
├── layer.py # Custom L1 distance layer
├── Siamese_model.keras # Trained model (generated after training)
├── templates/ # HTML templates for Flask
│ ├── index.html
│ └── result.html
├── uploads/ # Uploaded images for verification (Flask)
├── verification_images/ # Reference images for verification (Flask)
├── data/ # Training data (see below)
│ ├── anchor/ # (Not included: your personal face images)
│ ├── positive/ # (Not included: your personal face images)
│ └── negative/ # (Not included, but recommended: LFW dataset from Kaggle)
├── requirements.txt # Python dependencies
└── README.md
```
### Data Folder Details
- **data/anchor/**: Place your own face images here (not included in this repo for privacy).
- **data/positive/**: Place more images of your face here (not included in this repo for privacy).
- **data/negative/**: Place images of other people here. Recommended: [Labeled Faces in the Wild (LFW)](https://www.kaggle.com/datasets/jessicali9530/lfw-dataset) from Kaggle.
**Note:**
- Personal images (anchor/positive) are not included in this repository for privacy reasons.
- The negative images are from a public dataset and are not included due to size; please download them yourself.
## Code Flow
### 1. Data Collection & Training (`Facedetection.py`)
- **Option to collect new images** via webcam or use existing images in `data/anchor`, `data/positive`, and `data/negative`.
- **Pairs are created**: anchor-positive (label 1), anchor-negative (label 0).
- **Mild data augmentation** is applied for robustness.
- **Model is trained** with early stopping and a custom callback to halt when validation accuracy, precision, and recall all exceed 0.90.
- **Trained model is saved** as `Siamese_model.keras`.
### 2. Web App Deployment (`app.py`)
- **Flask backend** loads the trained model and exposes a web interface.
- **Users upload an image** via the frontend (`index.html`).
- **The backend compares** the uploaded image to all images in `verification_images/` using the Siamese model.
- **Result is shown** on `result.html` ("Verified" or "Unverified").
## Setup & Usage
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Prepare Data
- Place anchor and positive images (your face) in `data/anchor` and `data/positive` (not included for privacy).
- Place negative images (other faces) in `data/negative` (recommended: [LFW dataset](https://www.kaggle.com/datasets/jessicali9530/lfw-dataset)).
- You can use the webcam collection option in `Facedetection.py` or add images manually.
### 3. Train the Model
```bash
python Facedetection.py
```
- Choose whether to collect new images or use existing ones.
- The model will stop early when validation metrics are high.
### 4. Deploy the Web App
```bash
python app.py
```
- Open your browser at [http://127.0.0.1:5000/](http://127.0.0.1:5000/)
- Upload an image to verify against your reference images in `verification_images/`.
## Notes
- **Do not retrain the model in the Flask app.** Only use the trained model for inference.
- For best results, use a balanced and diverse dataset.
- You can tune hyperparameters, batch size, and augmentation in `Facedetection.py`.
- **Personal images are not included in this repository for privacy.**
## Reference
- [LFW Dataset](https://www.kaggle.com/datasets/jessicali9530/lfw-dataset)
- [Siamese Neural Network Paper](https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf)
---
Feel free to contribute or open issues for improvements!