0% found this document useful (0 votes)
25 views7 pages

Crowd Survilliance Heat Map

The document outlines a step-by-step guide for implementing a crowd-counting model using CSRNet in Python. It covers importing necessary libraries, mounting Google Drive, defining the CSRNet model, preprocessing video frames, and processing video frame-by-frame to generate and save a density heatmap. The final output is a processed video saved to Google Drive, displaying crowd density visualizations.

Uploaded by

santoshkuruventi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Crowd Survilliance Heat Map

The document outlines a step-by-step guide for implementing a crowd-counting model using CSRNet in Python. It covers importing necessary libraries, mounting Google Drive, defining the CSRNet model, preprocessing video frames, and processing video frame-by-frame to generate and save a density heatmap. The final output is a processed video saved to Google Drive, displaying crowd density visualizations.

Uploaded by

santoshkuruventi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Step-by-Step Code Explanation

Import Required Libraries


python
CopyEdit
import cv2
import numpy as np
import torch
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import time
from torchvision import models
from IPython.display import clear_output
from google.colab.patches import cv2_imshow
from google.colab import drive

What This Does:​


✔ cv2, numpy: Used for image processing (OpenCV and NumPy arrays)​
✔ torch, torchvision.transforms: PyTorch for loading the CSRNet deep learning
model​
✔ matplotlib.pyplot: Used to display heatmaps in Google Colab​
✔ time: Used for adding a delay (time.sleep(0.2)) to slow down frame visualization​
✔ IPython.display.clear_output: Clears the previous output so only the latest frame is
shown​
✔ google.colab.patches: Allows displaying images in Google Colab​
✔ google.colab.drive: Mounts Google Drive to access/save files

Mount Google Drive


python
CopyEdit
drive.mount('/content/drive')

What This Does:​


✔ Gives access to read/write files from Google Drive​
✔ Required because we are reading a video file from Drive and saving processed video
back to Drive

Define CSRNet Model


python
CopyEdit
class CSRNet(torch.nn.Module):
def __init__(self):
super(CSRNet, self).__init__()
self.frontend = models.vgg16_bn(pretrained=True).features[:33]
# Extract features
self.density_map = torch.nn.Conv2d(512, 1, kernel_size=1) #
Output 1-channel density map

def forward(self, x):


x = self.frontend(x)
x = self.density_map(x)
return x

What This Does:​


✔ Defines CSRNet, a crowd-counting deep learning model​
✔ Uses VGG16 as the base model (pretrained on ImageNet)​
✔ The final layer outputs a density map (1-channel grayscale image)​
✔ Higher density = more people in that region

Define Frame Preprocessing Function


python
CopyEdit
def preprocess_frame(frame):
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)), # Resize based on model input
requirements
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229,
0.224, 0.225])
])
return transform(frame).unsqueeze(0)

What This Does:​


✔ Converts OpenCV image (NumPy array) to PyTorch Tensor​
✔ Resizes it to 224×224 (CSRNet requires this input size)​
✔ Normalizes using ImageNet mean & standard deviation

Load and Open Video


python
CopyEdit
video_path =
"/content/drive/MyDrive/vecteezy_a-crowd-walking-on-the-street-in-slow
-motion_13996591.mp4"
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Could not open video file at {video_path}")
exit()

What This Does:​


✔ Loads video file from Google Drive​
✔ Checks if video opened successfully (if not, prints an error)

Load the Model and Move to GPU if Available


python
CopyEdit
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = CSRNet().to(device)
model.eval() # Set model to evaluation mode

What This Does:​


✔ Moves model to GPU if available (faster inference)​
✔ Puts model in evaluation mode (disables training layers like dropout)
Define Density Thresholds
python
CopyEdit
low_threshold = 50
medium_threshold = 150

What This Does:​


✔ Defines threshold values to classify density:

●​ Low Crowd (< 50 people)


●​ Medium Crowd (50–150 people)
●​ High Crowd (> 150 people)

Setup Video Writer for Processed Output


python
CopyEdit
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_path = "/content/drive/MyDrive/crowd_heatmap_output.mp4"
out = None # Will initialize after reading frame size

What This Does:​


✔ Prepares a video writer to save the processed video​
✔ Uses MP4 format (mp4v) for output​
✔ Output file is stored in Google Drive

Process Video Frame-by-Frame


python
CopyEdit
frame_count = 0
frame_skip = 1 # Process every frame

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
What This Does:​
✔ Reads each frame from the video​
✔ If no more frames, it exits the loop

Convert Frame & Make Prediction


python
CopyEdit
frame_count += 1
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
input_tensor = preprocess_frame(frame).to(device)

with torch.no_grad():
density_map = model(input_tensor)
crowd_count = density_map.sum().item()

print(f"Frame {frame_count} - Crowd Count: {crowd_count}")

What This Does:​


✔ Converts frame from BGR (OpenCV format) to RGB​
✔ Preprocesses frame (resize, normalize, convert to tensor)​
✔ Passes frame through CSRNet model (gets density map)​
✔ Calculates total crowd count from density map

Resize Density Map and Apply Heatmap


python
CopyEdit
density_map_np = density_map.squeeze().cpu().numpy()
density_map_resized = cv2.resize(density_map_np, (frame.shape[1],
frame.shape[0]), interpolation=cv2.INTER_CUBIC)

density_map_norm = cv2.normalize(density_map_resized, None, 0,


255, cv2.NORM_MINMAX)
density_map_uint8 = density_map_norm.astype(np.uint8)
heatmap = cv2.applyColorMap(density_map_uint8, cv2.COLORMAP_JET)

overlay = cv2.addWeighted(frame, 0.6, heatmap, 0.4, 0)


What This Does:​
✔ Converts density map to NumPy array​
✔ Resizes it to match original frame size​
✔ Normalizes density values (0 to 255 for visibility)​
✔ Applies a heatmap to visualize density​
✔ Overlays heatmap on the original frame

Save Processed Frame to Output Video


python
CopyEdit
if out is None:
out = cv2.VideoWriter(output_path, fourcc, 10,
(frame.shape[1], frame.shape[0]))

out.write(overlay)

What This Does:​


✔ Initializes video writer if not already done​
✔ Saves processed frame to output video

Display Heatmap in Google Colab


python
CopyEdit
plt.figure(figsize=(6, 4))
plt.axis("off")
plt.imshow(cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB))
plt.show()

time.sleep(0.2)
clear_output(wait=True)

What This Does:​


✔ Uses Matplotlib to display heatmap (since cv2.imshow() doesn’t work in Colab)​
✔ Adds delay (0.2s) so frames don’t update too fast​
✔ Clears previous output to avoid clutter

Release Video & Provide Download Link


python
CopyEdit
cap.release()
out.release()
print(f" ✅ Processed video saved: {output_path} 🎉")
What This Does:​
✔ Closes video file​
✔ Saves processed video to Google Drive

You might also like