Skip to content

Commit 4c45f0d

Browse files
authored
Update README.md
1 parent 3ebaf3b commit 4c45f0d

1 file changed

Lines changed: 255 additions & 0 deletions

File tree

  • Gender and age detection using deep learning
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
<div align="center">
2+
3+
# 🧑‍🤝‍🧑 Gender & Age Detection — OpenCV Deep Learning
4+
5+
[![Python](https://img.shields.io/badge/Python-3.7+-3776AB?style=for-the-badge&logo=python&logoColor=white)](https://www.python.org/)
6+
[![OpenCV](https://img.shields.io/badge/OpenCV-DNN-5C3EE8?style=for-the-badge&logo=opencv&logoColor=white)](https://opencv.org/)
7+
[![Caffe](https://img.shields.io/badge/Caffe-Pre--trained%20Models-red?style=for-the-badge)](http://caffe.berkeleyvision.org/)
8+
[![Dataset](https://img.shields.io/badge/Dataset-Adience-blueviolet?style=for-the-badge)](https://talhassner.github.io/home/projects/Adience/Adience-data.html)
9+
[![License](https://img.shields.io/badge/License-MIT-1abc9c?style=for-the-badge)](../LICENSE.md)
10+
11+
> Detects **faces** in images or a live webcam feed and predicts each person's **gender** (Male/Female) and **age range** across 8 age buckets — using three pre-trained deep learning models loaded via **OpenCV DNN**.
12+
13+
[🔙 Back to Main Repository](https://github.com/shsarv/Machine-Learning-Projects)
14+
15+
</div>
16+
17+
---
18+
19+
## 📌 Table of Contents
20+
21+
- [About the Project](#-about-the-project)
22+
- [How It Works](#-how-it-works)
23+
- [The Three Models](#-the-three-models)
24+
- [Age & Gender Classes](#-age--gender-classes)
25+
- [CNN Architecture](#-cnn-architecture)
26+
- [Project Structure](#-project-structure)
27+
- [Getting Started](#-getting-started)
28+
- [Tech Stack](#-tech-stack)
29+
- [References & Citation](#-references--citation)
30+
31+
---
32+
33+
## 🔬 About the Project
34+
35+
This project builds a **real-time gender and age detection system** using three pre-trained models served through OpenCV's DNN module — no model training required. Based on the DataFlair deep learning project, it uses:
36+
37+
- A **TensorFlow SSD** model for face detection
38+
- A **Caffe CNN** (Levi & Hassner, 2015) for gender classification
39+
- A **Caffe CNN** (Levi & Hassner, 2015) for age prediction
40+
41+
The script (`gad.py`) accepts a **static image** via `--image` argument or runs on a **live webcam feed**, draws bounding boxes around detected faces, and overlays the predicted gender and age range on each face.
42+
43+
---
44+
45+
## ⚙️ How It Works
46+
47+
```
48+
Input: Image / Webcam Frame
49+
50+
51+
blobFromImage(frame, 1.0, (300×300), [104,117,123])
52+
53+
54+
┌─────────────────────────────────────┐
55+
│ Face Detection (TensorFlow SSD) │
56+
│ opencv_face_detector_uint8.pb │
57+
│ opencv_face_detector.pbtxt │
58+
└─────────────────────────────────────┘
59+
60+
61+
For each face (confidence > 0.7):
62+
Crop face ROI + 20px padding
63+
blobFromImage(face, 1.0, (227×227), MODEL_MEAN_VALUES)
64+
65+
┌─────┴──────┐
66+
▼ ▼
67+
┌──────────┐ ┌──────────┐
68+
│ Gender │ │ Age │
69+
│ Network │ │ Network │
70+
│ (Caffe) │ │ (Caffe) │
71+
└──────────┘ └──────────┘
72+
│ │
73+
▼ ▼
74+
Male/Female Age Bucket
75+
└─────┬──────┘
76+
77+
"Gender: Male Age: (25-32)"
78+
overlaid on bounding box
79+
```
80+
81+
**Key preprocessing constant:**
82+
```python
83+
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
84+
```
85+
> BGR mean values subtracted from every face blob to normalize for illumination variation across the Adience training data.
86+
87+
---
88+
89+
## 🧠 The Three Models
90+
91+
| Model | Framework | Files | Purpose |
92+
|-------|-----------|-------|---------|
93+
| **Face Detector** | TensorFlow SSD | `opencv_face_detector_uint8.pb` + `opencv_face_detector.pbtxt` | Detect face bounding boxes |
94+
| **Gender Net** | Caffe (Levi & Hassner) | `gender_net.caffemodel` + `gender_deploy.prototxt` | Classify Male / Female |
95+
| **Age Net** | Caffe (Levi & Hassner) | `age_net.caffemodel` + `age_deploy.prototxt` | Predict one of 8 age ranges |
96+
97+
```python
98+
faceNet = cv2.dnn.readNet("opencv_face_detector_uint8.pb", "opencv_face_detector.pbtxt")
99+
ageNet = cv2.dnn.readNet("age_net.caffemodel", "age_deploy.prototxt")
100+
genderNet = cv2.dnn.readNet("gender_net.caffemodel", "gender_deploy.prototxt")
101+
```
102+
103+
---
104+
105+
## 🏷️ Age & Gender Classes
106+
107+
**Gender** (2 classes):
108+
```python
109+
genderList = ['Male', 'Female']
110+
```
111+
112+
**Age** (8 buckets):
113+
```python
114+
ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)',
115+
'(25-32)', '(38-43)', '(48-53)', '(60-100)']
116+
```
117+
118+
> Age is treated as a **classification problem** over 8 discrete ranges rather than regression — Levi & Hassner (2015) found classification over predefined buckets more robust than direct regression on the Adience benchmark.
119+
120+
---
121+
122+
## 🏗️ CNN Architecture
123+
124+
Both age and gender models share the same architecture — a lightweight CNN similar to CaffeNet/AlexNet, trained on the **Adience dataset**:
125+
126+
```
127+
Input: 227 × 227 × 3 face crop (mean-subtracted)
128+
129+
Conv1: 96 filters, 7×7 kernel → ReLU → MaxPool → LRN
130+
Conv2: 256 filters, 5×5 kernel → ReLU → MaxPool → LRN
131+
Conv3: 384 filters, 3×3 kernel → ReLU → MaxPool
132+
133+
FC1: 512 nodes → ReLU → Dropout
134+
FC2: 512 nodes → ReLU → Dropout
135+
136+
Softmax
137+
├── Gender Net output: 2 (Male / Female)
138+
└── Age Net output: 8 (age range buckets)
139+
```
140+
141+
---
142+
143+
## 📁 Project Structure
144+
145+
```
146+
Gender and age detection using deep learning/
147+
148+
├── gad.py # Main script — detection pipeline
149+
150+
├── age_net.caffemodel # Age model weights (Caffe, ~44 MB)
151+
├── age_deploy.prototxt # Age model architecture
152+
├── gender_net.caffemodel # Gender model weights (Caffe, ~44 MB)
153+
├── gender_deploy.prototxt # Gender model architecture
154+
├── opencv_face_detector_uint8.pb # Face detector weights (TensorFlow)
155+
├── opencv_face_detector.pbtxt # Face detector architecture
156+
157+
├── girl1.jpg # Sample test images
158+
├── girl2.jpg # ↑
159+
├── kid1.jpg # ↑
160+
├── man1.jpg # ↑
161+
├── minion.jpg # ↑
162+
├── woman1.jpg # ↑
163+
├── woman3.jpg # ↑
164+
165+
└── README.md
166+
```
167+
168+
> **Note:** The `.caffemodel` files (~44 MB each) may not be included in the repository due to GitHub's file size limits. If missing, download them from [Tal Hassner's Adience page](https://talhassner.github.io/home/projects/Adience/Adience-data.html) and place them in the project root.
169+
170+
---
171+
172+
## 🚀 Getting Started
173+
174+
### 1. Clone the repository
175+
176+
```bash
177+
git clone https://github.com/shsarv/Machine-Learning-Projects.git
178+
cd "Machine-Learning-Projects/Gender and age detection using deep learning"
179+
```
180+
181+
### 2. Set up environment
182+
183+
```bash
184+
python -m venv venv
185+
source venv/bin/activate # Linux / macOS
186+
venv\Scripts\activate # Windows
187+
188+
pip install -r requirements.txt
189+
```
190+
191+
### 3. Run on a sample image
192+
193+
```bash
194+
python gad.py --image girl1.jpg
195+
# Output → Gender: Female Age: (25-32) years
196+
```
197+
198+
Try the included sample images:
199+
200+
```bash
201+
python gad.py --image man1.jpg
202+
python gad.py --image kid1.jpg
203+
python gad.py --image woman1.jpg
204+
python gad.py --image minion.jpg # 🤔
205+
```
206+
207+
### 4. Run on live webcam
208+
209+
```bash
210+
python gad.py
211+
# No --image flag → defaults to webcam (index 0)
212+
# Press Q to quit
213+
```
214+
215+
---
216+
217+
## 🛠️ Tech Stack
218+
219+
| Layer | Technology |
220+
|-------|-----------|
221+
| Language | Python 3.7+ |
222+
| Computer Vision | OpenCV (`cv2.dnn`) |
223+
| Face Detection | TensorFlow SSD (ResNet-10 backbone) |
224+
| Age / Gender Models | Caffe (Levi & Hassner, 2015) |
225+
| Argument Parsing | `argparse` |
226+
| Numerical Processing | NumPy |
227+
228+
---
229+
230+
## 📚 References & Citation
231+
232+
```bibtex
233+
@inproceedings{Levi2015,
234+
author = {Gil Levi and Tal Hassner},
235+
title = {Age and Gender Classification Using Convolutional Neural Networks},
236+
booktitle = {IEEE Workshop on Analysis and Modeling of Faces and Gestures (AMFG),
237+
at the IEEE Conf. on Computer Vision and Pattern Recognition (CVPR)},
238+
year = {2015}
239+
}
240+
```
241+
242+
- [Levi & Hassner (2015) — Original Paper & Models](https://talhassner.github.io/home/projects/Adience/Adience-data.html)
243+
- [Adience Benchmark Dataset](https://talhassner.github.io/home/projects/Adience/Adience-data.html)
244+
- [OpenCV DNN Face Detector](https://github.com/opencv/opencv/tree/master/samples/dnn)
245+
- [LearnOpenCV — Age & Gender Classification](https://learnopencv.com/age-gender-classification-using-opencv-deep-learning-c-python/)
246+
247+
---
248+
249+
<div align="center">
250+
251+
Part of the [Machine Learning Projects](https://github.com/shsarv/Machine-Learning-Projects) collection by [Sarvesh Kumar Sharma](https://github.com/shsarv)
252+
253+
⭐ Star the main repo if this helped you!
254+
255+
</div>

0 commit comments

Comments
 (0)