This repository contains a computer-vision prototype for crowd analysis. It combines:
- a crowd-density estimator built on a VGG-style density map network
- a violence classifier built on top of
ResNet18 - a Streamlit app that runs both models on uploaded
.avivideos and reports average crowd and violence metrics
Smart-Crowd-Management-System/
├── frontend/
│ ├── check.py # Streamlit app for video upload and analysis
│ ├── inference.py # Density-map inference helper
│ ├── model.py # VGG-style crowd density network
│ ├── modelR.py # ResNet18-based violence/density classifier
│ ├── dataset.py # Binary image dataset loader
│ ├── utils.py # Density-model checkpoint helpers
│ └── utilsR.py # ResNet save/load helpers
└── model/
├── train.py # Training script for the ResNet model
├── inference.py # Video inference script for the ResNet model
├── model.py # ResNet18-based violence/density classifier
├── dataset.py # Binary image dataset loader
├── image.py # Density ground-truth loading helper
├── utils.py # ResNet save/load helpers
└── make_dataset.ipynb# Notebook to extract frames from videos
The density pipeline in frontend/model.py defines CrowdAnalyser, a convolutional network with:
- a VGG16-style frontend initialized from pretrained ImageNet weights
- a dilated backend for density-map regression
- a
1x1output layer producing a single-channel density map
The helper in frontend/inference.py:
- reads an image with OpenCV
- resizes it to
224x224 - normalizes it with ImageNet statistics
- returns the predicted density map as a NumPy array
The Streamlit app sums this density map to estimate the number of people in each frame.
The violence model in model/model.py and frontend/modelR.py wraps a pretrained ResNet18 backbone and adds two sigmoid heads:
fc_violence: predicts violence likelihoodfc_density: predicts a second scalar described in the code as density
Training is implemented in model/train.py using:
BCELossAdamoptimizer with learning rate1e-4- image resize to
180x320 - 3 epochs
- dataset folders
data/trainanddata/val
Important detail: the dataset loader in model/dataset.py is binary and uses only two class folders:
no-violenceviolence
Because the same label is used for both output heads during training, the second head is not trained from separate density annotations.
frontend/check.py is the main demo application. It:
- loads the density model from
weights.pth - loads the ResNet model from a saved
.pthcheckpoint - accepts an uploaded
.avifile - processes the video frame by frame
- computes:
- average estimated number of people
- average density percentage
- density class from 0 to 5
- average violence score
- displays a heatmap for the midpoint frame
The notebook model/make_dataset.ipynb extracts frames from videos and saves them as .jpg files.
The notebook currently expects input videos under:
new/val/Violence/
and writes extracted frames to:
data/val/violence/
For training with the current dataset loader, organize image data like this:
data/
├── train/
│ ├── no-violence/
│ └── violence/
└── val/
├── no-violence/
└── violence/
This repository does not include a requirements.txt or environment file, so dependencies must be installed manually.
- Python
3.10+
Install the libraries imported by the code:
pip install torch torchvision streamlit opencv-python numpy matplotlib pillow h5py tqdmIf you plan to use Jupyter notebooks:
pip install notebookFrom the repository root:
cd model
python train.pyThis saves timestamped checkpoints inside:
model/checkpoints/
Edit the hardcoded checkpoint path and video path in model/inference.py, then run:
cd model
python inference.pyBefore launching the app:
- place the density-model checkpoint at
frontend/weights.pth, or update the path infrontend/check.py - replace the hardcoded ResNet checkpoint path in
frontend/check.pywith a valid local checkpoint
Then run:
cd frontend
streamlit run check.pyUpload an .avi file through the UI to start analysis.
The code expects checkpoint files that are not committed in this repository:
frontend/weights.pthfor the density model- a ResNet checkpoint path currently hardcoded to a local Windows path in:
You will need to train or supply these files before inference works.
- There is no dependency lockfile or reproducible environment configuration.
- Several scripts contain hardcoded absolute Windows paths and will need editing on another machine.
- The frontend and model folders duplicate some code instead of sharing a common package.
- The ResNet training pipeline uses the same binary label for both the violence head and the density head.
- The density-estimation training script is not included here; only the density model definition and inference code are present.
- The Streamlit UI currently accepts only
.aviuploads.
- Add a
requirements.txtorpyproject.toml. - Move repeated model and utility code into a shared package.
- Replace hardcoded paths with CLI arguments or configuration.
- Add a proper training pipeline for the density model.
- Store checkpoints under project-relative paths.
- Add sample data and example checkpoints for easier onboarding.
This project is best understood as an experimental smart crowd analysis demo:
- the
model/folder trains and tests a binary violence classifier based on frame images - the
frontend/folder combines a pretrained density-map model with the ResNet model in a Streamlit interface for video analysis
If you want to make the project easier to run across machines, the first thing to fix is path/config management and dependency packaging.