1784214ee07c75c1593a2be13935335d3e8f33f6
- Update zone transition logic to count first detections in entry and exit zones - Refine conditions for counting entries and exits based on zone transitions - Improve comments for clarity on zone assignment and transitions
People Counter Web App
A real-time web application that uses USB camera face detection to count people entering and leaving a room, with zone-based entry/exit tracking.
Features
- Real-time face detection using OpenCV DNN face detector
- Zone-based entry/exit tracking (left zone = entry, right zone = exit)
- Live video streaming via MJPEG
- Real-time count updates via WebSocket
- Visual indicators for overpopulation warnings
- Clean, modern web interface
Prerequisites
- Python 3.7 or higher
- USB camera connected to your computer
- Linux/Windows/macOS
Installation
-
Clone or download this repository
-
Install Python dependencies:
pip install -r requirements.txt -
Download the face detection model files:
python download_models.pyThis will download the required OpenCV DNN model files to the
models/directory.Note: If the automatic download fails, you can manually download:
deploy.prototxtfrom: https://github.com/opencv/opencv/blob/master/samples/dnn/face_detector/deploy.prototxtres10_300x300_ssd_iter_140000.caffemodelfrom: https://github.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel
Place both files in the
models/directory.
Usage
-
Make sure your USB camera is connected
-
Run the Flask application:
python app.py -
Open your web browser and navigate to:
http://localhost:5000 -
The application will:
- Display live video feed from your camera
- Detect faces in real-time
- Count people entering (left zone) and exiting (right zone)
- Display current occupancy and statistics
- Show visual warnings when occupancy is high
Configuration
You can modify the following settings in app.py and camera.py:
- Camera index: Change
camera_index=0to use a different camera - Frame processing rate: Adjust
process_every_n_frames(default: 3) to balance performance and accuracy - Face detection confidence: Modify
face_confidencethreshold (default: 0.5) - Zone boundaries: Adjust
entry_zone_percentandexit_zone_percentinzone_tracker.py - Cooldown period: Change
cooldown_secondsto prevent double-counting (default: 2.0 seconds) - Maximum occupancy: Update
MAX_OCCUPANCYinstatic/js/main.jsfor overpopulation warnings
How It Works
- Camera Capture: The camera module captures frames from your USB camera
- Face Detection: OpenCV DNN detects faces in the video frames
- Zone Tracking: The zone tracker determines which zone each face is in:
- Left 40% = Entry zone (green)
- Right 40% = Exit zone (red)
- Center 10% = Buffer zone (ignored to prevent false counts)
- Counting Logic: People are counted when they appear in entry/exit zones with a cooldown period to prevent double-counting
- Real-time Updates: Counts are sent to the web interface via WebSocket for live updates
Project Structure
PeopleCounter/
├── app.py # Flask main application
├── camera.py # Camera capture wrapper
├── face_detector.py # Face detection module
├── zone_tracker.py # Zone-based tracking logic
├── download_models.py # Script to download model files
├── requirements.txt # Python dependencies
├── models/ # OpenCV DNN model files
│ ├── deploy.prototxt
│ └── res10_300x300_ssd_iter_140000.caffemodel
├── templates/
│ └── index.html # Main web interface
└── static/
├── css/
│ └── style.css # Styling
└── js/
└── main.js # Client-side JavaScript
Troubleshooting
- Camera not found: Make sure your camera is connected and try changing the
camera_indexincamera.py - Model files missing: Run
python download_models.pyto download required files - Slow performance: Increase
process_every_n_framesvalue or reduce video resolution - Double counting: Increase the
cooldown_secondsvalue inzone_tracker.py
Security & Privacy
- All processing runs locally (no cloud storage)
- No face recognition - only detection (no personal identification)
- Video frames processed in memory, not stored
- Optional: You can modify the code to blur faces for display if privacy is a concern
License
This project is open source and available for use and modification.
Description
Languages
Python
71.2%
JavaScript
12.7%
CSS
10.6%
HTML
5.5%