Real-time gender detection and tracking from a webcam using Python, OpenCV, and DeepFace.
Each person that enters the frame is assigned a stable ID and classified as Man/Woman. A running total is displayed on screen and printed at the end of the session.
git clone https://github.com/aniketshaw29/gender-tracking-video.git
cd gender-tracking-videopython3 -m venv .venv
# macOS / Linux
source .venv/bin/activate
# Windows
.venv\Scripts\activateYou should see (.venv) in your terminal prompt.
.venv/bin/pip install -r requirements.txtThis installs OpenCV, DeepFace, TensorFlow/Keras, and NumPy. Expect ~2–5 minutes.
.venv/bin/python download_models.pyThis downloads OpenCV's ResNet SSD face detector into models/. Without it, the app falls back to a Haar cascade which is less accurate. See docs/face-detection.md for the difference.
.venv/bin/python main.pyPress Q to quit. Session totals are printed to the terminal on exit.
macOS note: Use
.venv/bin/pythonexplicitly rather thanpython. On macOS the barepythoncommand resolves to the system framework Python at/Library/Frameworks/Python.framework/..., which ignores the virtual environment and won't find the installed packages.
.venv/bin/python main.py --camera 1 # use a different camera index
.venv/bin/python main.py --classify-every 20 # re-classify every 20 frames (default 15)| File / Folder | Purpose |
|---|---|
| main.py | Entry point — camera loop, orchestration, display |
| detector.py | Face detection (DNN or Haar fallback) |
| classifier.py | Gender classification via DeepFace |
| tracker.py | Centroid tracker — stable IDs across frames |
| reid.py | Person re-identification via position memory |
| download_models.py | Downloads the ResNet SSD face detector weights |
| requirements.txt | Python dependencies |
captures/ |
Auto-created at runtime — one JPEG saved per unique person when their gender is first confirmed |
Captured photos are named
person_000_Man_20260825_143012.jpg(ID, gender, timestamp). Thecaptures/folder is excluded from git via.gitignore.
| Doc | What it covers |
|---|---|
| docs/architecture.md | System overview, data flow, component responsibilities, threading model |
| docs/face-detection.md | How DNN and Haar face detection work |
| docs/gender-classification.md | How DeepFace classifies gender, model preloading, throttling |
| docs/tracking.md | Centroid tracking algorithm, max_disappeared, relationship to PersonDatabase |
| docs/person-reidentification.md | Position-based re-ID, session-level memory, counting accuracy |
| docs/face-capture.md | How face photos are saved to captures/, filename format, padding |
| docs/performance.md | Tuning classify_every, max_disappeared, position_radius; GPU acceleration |
deactivate