βββββββ βββββββ βββββββ βββ ββββββββββ
ββββββββββββββββββββββββββββ βββββββββββ
βββ βββββββββββ ββββββ ββ ββββββ βββ
βββ βββββββββββ ββββββββββββββββ βββ
βββββββββββ ββββββββββββββββββββββββββββββ
ββββββββββ βββ βββββββ ββββββββ βββββββ
ββββ ββββ βββββββ ββββ βββββββββββββββ βββββββ βββββββ
βββββ βββββββββββββββββββ ββββββββββββββββββββββββββββββββ
ββββββββββββββ βββββββββ ββββββ βββ βββ βββββββββββ
ββββββββββββββ ββββββββββββββββ βββ βββ βββββββββββ
βββ βββ βββββββββββββββ βββββββββ βββ ββββββββββββ βββ
βββ βββ βββββββ βββ ββββββββ βββ βββββββ βββ βββ
Real-time crowd density estimation for campus canteens and libraries
Built from scratch using classical Computer Vision β no deep learning required
Campus Crowd Monitor is a privacy-first, real-time crowd density estimation system designed for campus environments β canteens, libraries, study halls, and corridors. It processes video from a fixed overhead or entry-facing camera and delivers live occupancy intelligence using only classical Computer Vision.
No neural network. No cloud. No faces stored. Just elegant signal processing, from pixel to insight.
| Capability | Method |
|---|---|
| π€ Person Detection | HOG descriptor + SVM classifier |
| πΊοΈ Zone Segmentation | Pixel-mapped zones with colour-coded overlays |
| π‘οΈ Density Heatmap | Gaussian Kernel Density Estimation |
| π¦ Occupancy Alerts | Per-zone threshold triggers (Free / Moderate / Crowded) |
| π¬ CV Debug View | Live edges, corners, histograms, pyramids in dashboard |
This project was built to demonstrate techniques across a full Computer Vision curriculum.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β UNIT 1 β Digital Image Formation β
β ββ Resize & colour conversion (BGR β Grayscale β LAB) β
β ββ Gaussian blur (5Γ5 kernel, Ο = 1.0) β
β ββ CLAHE histogram equalisation (contrast enhancement) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β UNIT 3 β Feature Extraction β
β ββ Edge detection : Canny Β· LOG Β· DOG β
β ββ Corner detection : Harris β
β ββ Line detection : Hough transform β
β ββ Descriptors : HOG Β· SIFT keypoints β
β ββ Scale-space : Gaussian image pyramids β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β UNIT 3 β Image Segmentation β
β ββ Region growing β
β ββ GrabCut (graph-cut) β
β ββ Watershed algorithm β
β ββ Mean-shift segmentation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β UNIT 4 β Pattern Analysis β
β ββ HOG + SVM person classifier β
β ββ Non-Maximum Suppression (NMS) β
β ββ Background subtraction (MOG2) β
β ββ Temporal density averaging β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
crowd-detection/
β
βββ π data/
β βββ sample_videos/ β Place your .mp4 / .avi footage here
β βββ annotations/
β βββ counts.csv β Manual frame counts for evaluation
β
βββ π src/
β βββ preprocessing.py β Unit 1 Β· Gaussian blur, CLAHE, MOG2, pyramids
β βββ feature_extraction.py β Unit 3 Β· Canny, Harris, Hough, HOG, SIFT
β βββ detector.py β Unit 3/4 Β· HOG+SVM detection, NMS, zone counting
β βββ segmentation.py β Unit 3 Β· Region growing, GrabCut, Watershed, zones
β βββ density_map.py β Unit 1/3 Β· Gaussian KDE heatmap, alert thresholds
β βββ dashboard.py β Streamlit interactive web dashboard
β
βββ π models/
β βββ hog_svm.pkl β Auto-loaded from OpenCV; swap for custom model
β
βββ π notebooks/
β βββ exploration.ipynb β Step-by-step visualisation of every CV stage
β
βββ π report/
β βββ project_report.pdf
β
βββ requirements.txt
βββ README.md
git clone https://github.com/YOUR_USERNAME/campus-crowd-monitor.git
cd campus-crowd-monitor
pip install -r requirements.txtDrop a .mp4 or .avi file into data/sample_videos/.
Record 5β10 minutes of canteen or library footage from a phone mounted overhead.
(Always get permission from your institution before recording.)
No footage? The dashboard also works directly with your webcam β see step 4.
jupyter notebook notebooks/exploration.ipynbThe notebook walks through every preprocessing and feature-extraction stage with inline visualisations. Run it first β it generates all the figures you'll need for your report and builds intuition for the pipeline before you touch the live system.
streamlit run src/dashboard.pyOpen the URL printed in your terminal β usually http://localhost:8501.
In the dashboard you can:
- Upload a video or click "Start webcam" for live inference
- Toggle heatmap, bounding boxes, zone overlay, and edge debug view from the sidebar
- Adjust occupancy thresholds and detection sensitivity with live sliders
Every video frame travels through this processing chain:
Camera frame
β
βΌ βββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STAGE 1 Β· PREPROCESSING
β
ββ resize_frame() β 640 Γ 480, BGR β Grayscale
ββ gaussian_blur() β 5Γ5 kernel, Ο = 1.0 (noise removal)
ββ apply_clahe_color() β CLAHE on LAB L-channel (contrast boost)
β
βΌ βββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STAGE 2 Β· DETECTION & SEGMENTATION
β
ββ BackgroundSubtractor.apply() β Foreground mask (MOG2)
ββ HOGPersonDetector.detect() β Bounding boxes + NMS
β
βΌ βββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STAGE 3 Β· DENSITY & OVERLAY
β
ββ make_density_map() β Gaussian KDE heatmap
ββ draw_zone_overlay() β Per-zone occupancy labels & colours
β
βΌ
Dashboard frame β
Zones are defined in src/segmentation.py as pixel-coordinate rectangles mapped to your camera's field of view:
# src/segmentation.py
DEFAULT_ZONES = {
"Zone A (tables 1β4)": ( 0, 0, 320, 240),
"Zone B (tables 5β8)": (320, 0, 640, 240),
"Zone C (corridor)": ( 0, 240, 640, 480),
}Format:
(x_min, y_min, x_max, y_max)in pixels, relative to the 640Γ480 frame.
1. Screenshot your camera's empty view
2. Open in any image editor (Paint, Preview, GIMP β¦)
3. Note pixel coordinates at zone corners
4. Update DEFAULT_ZONES with those coordinates
| Status | Colour | Default condition | Dashboard control |
|---|---|---|---|
| π’ Free | Green | < 5 people | Sidebar slider |
| π‘ Moderate | Amber | 5 β 14 people | Sidebar slider |
| π΄ Crowded | Red | β₯ 15 people | Sidebar slider |
Thresholds can also be hard-coded directly in src/density_map.py if you want them locked for deployment.
To measure detection accuracy against ground truth:
Step 1 β Pick 20β30 representative frames from your video
Step 2 β Manually count the people in each frame
Step 3 β Write results to data/annotations/counts.csv
Format: frame_id, manual_count
Step 4 β Run the final cell in exploration.ipynb
β computes Mean Absolute Error (MAE) automatically
Metric: Mean Absolute Error between predicted and manual counts.
A MAE of 1β3 people per zone is typical for controlled indoor environments.
This system is designed to be privacy-safe by construction:
- Detection uses head/shoulder silhouettes only (HOG blobs) β no facial geometry is analysed
- No images or crops are stored to disk at any point
- All computation is local β no data ever leaves the device
- Only aggregate zone counts and density values are logged
| Library | Version | Role |
|---|---|---|
| OpenCV | β₯ 4.8 | All CV algorithms β detection, blur, edges, segmentation |
| NumPy | β₯ 1.24 | Array maths, kernel operations |
| Streamlit | β₯ 1.30 | Interactive web dashboard |
| Matplotlib | β₯ 3.8 | Notebook visualisations |
| PyTorch | β₯ 2.1 | Optional deep learning backbone (plug-in replacement for HOG+SVM) |
pip install -r requirements.txt# requirements.txt
opencv-contrib-python>=4.8
numpy>=1.24
streamlit>=1.30
matplotlib>=3.8
torch>=2.1 # optional β comment out if not needed
scikit-learn>=1.3 # for SVM training utilities
jupyter>=1.0
π Swap in a deep learning detector
Replace HOGPersonDetector in detector.py with a YOLOv8 or Faster R-CNN call. The rest of the pipeline (zone counting, heatmap, dashboard) is detector-agnostic.
# detector.py β drop-in replacement sketch
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
def detect(frame):
results = model(frame)
boxes = results[0].boxes.xyxy.cpu().numpy()
return boxesπ Export data for analytics
Add a CSV writer to density_map.py to log timestamped zone counts. Pipe this into a Grafana dashboard or a simple pandas analysis for usage trend reports.
π‘ Deploy on a Raspberry Pi
Disable PyTorch, reduce frame resolution to 320Γ240, and lower the detection window stride. The HOG+SVM pipeline runs comfortably at 10β15 FPS on a Pi 4.
MIT License β free to use, modify, and adapt for academic and personal projects.
Please retain attribution when submitting as coursework.
Built with classical Computer Vision Β· Privacy-first Β· No cloud required