Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Smart Crowd Management System

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 .avi videos and reports average crowd and violence metrics

Repository structure

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

What the code does

1. Crowd density estimation

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 1x1 output 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.

2. Violence classification

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 likelihood
  • fc_density: predicts a second scalar described in the code as density

Training is implemented in model/train.py using:

  • BCELoss
  • Adam optimizer with learning rate 1e-4
  • image resize to 180x320
  • 3 epochs
  • dataset folders data/train and data/val

Important detail: the dataset loader in model/dataset.py is binary and uses only two class folders:

  • no-violence
  • violence

Because the same label is used for both output heads during training, the second head is not trained from separate density annotations.

3. Streamlit video app

frontend/check.py is the main demo application. It:

  1. loads the density model from weights.pth
  2. loads the ResNet model from a saved .pth checkpoint
  3. accepts an uploaded .avi file
  4. processes the video frame by frame
  5. computes:
    • average estimated number of people
    • average density percentage
    • density class from 0 to 5
    • average violence score
  6. displays a heatmap for the midpoint frame

Data preparation

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/

Setup

This repository does not include a requirements.txt or environment file, so dependencies must be installed manually.

Suggested Python version

  • Python 3.10+

Required packages

Install the libraries imported by the code:

pip install torch torchvision streamlit opencv-python numpy matplotlib pillow h5py tqdm

If you plan to use Jupyter notebooks:

pip install notebook

How to run

Train the ResNet violence model

From the repository root:

cd model
python train.py

This saves timestamped checkpoints inside:

model/checkpoints/

Run standalone ResNet video inference

Edit the hardcoded checkpoint path and video path in model/inference.py, then run:

cd model
python inference.py

Run the Streamlit app

Before launching the app:

  • place the density-model checkpoint at frontend/weights.pth, or update the path in frontend/check.py
  • replace the hardcoded ResNet checkpoint path in frontend/check.py with a valid local checkpoint

Then run:

cd frontend
streamlit run check.py

Upload an .avi file through the UI to start analysis.

Checkpoints and model files

The code expects checkpoint files that are not committed in this repository:

You will need to train or supply these files before inference works.

Known limitations

  • 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 .avi uploads.

Recommended next improvements

  • Add a requirements.txt or pyproject.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.

Summary

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages