Crowd counting with computer vision is increasingly relevant for India’s public infrastructure – railway stations, temples, stadiums, and government offices regularly see large gatherings requiring safety monitoring. Running crowd counting with YOLO on Jetson Nano enables real-time people detection at the edge without cloud dependency. This tutorial covers YOLO deployment on Jetson Nano, TensorRT optimisation for faster inference, and building a crowd density monitoring system for India use cases.
Table of Contents
- Crowd Counting Approaches
- YOLO on Jetson Nano Setup
- TensorRT Optimisation
- People Detection Inference Code
- Zone-Based Crowd Density
- India-Specific Use Cases
- Alert System Integration
- FAQ
Crowd Counting Approaches
Three main approaches for crowd counting:
- Detection-based (YOLO): Detect each person with bounding box. Accurate for sparse/medium crowds. Fails in very dense crowds where individuals overlap. Jetson Nano can run YOLOv8n at 8-15 FPS.
- Density map estimation (CSRNet, MCNN): Predicts a density map and integrates it. Works for very dense crowds (Kumbh Mela scale). Requires GPU, runs at 5-10 FPS on Jetson Nano.
- Regression models: Directly predict a count number from image features. Fast but less spatial information. Good for quick estimates.
For India station/temple crowd monitoring with counts under 200 people in frame, YOLO detection is the most practical approach.
YOLO on Jetson Nano Setup
# Install YOLOv8 on Jetson Nano (JetPack 4.6)
sudo apt update
sudo apt install -y python3-pip
# Install PyTorch for Jetson (specific ARM build)
wget https://nvidia.box.com/shared/static/p57jwntv436lfrd78inwl7iml6p13fzh.whl
-O torch-2.1.0-cp38-cp38m-linux_aarch64.whl
pip3 install torch-2.1.0-cp38-cp38m-linux_aarch64.whl
# Install Ultralytics YOLOv8
pip3 install ultralytics
# Test basic detection
python3 -c "from ultralytics import YOLO; m=YOLO('yolov8n.pt'); print('YOLO OK')"
TensorRT Optimisation
TensorRT converts PyTorch models to optimised engine files for Jetson’s GPU. This dramatically improves inference speed:
from ultralytics import YOLO
# Export to TensorRT (run once, takes 5-10 minutes)
model = YOLO('yolov8n.pt')
model.export(format='engine', device=0, half=True) # FP16 for faster inference
# Load TensorRT model for inference (much faster than PyTorch)
model_trt = YOLO('yolov8n.engine')
# Benchmark
results = model_trt.benchmark(data='coco8.yaml', imgsz=640, half=True)
Performance comparison on Jetson Nano (640×640 input):
- YOLOv8n PyTorch: ~4 FPS
- YOLOv8n TensorRT FP16: ~12-15 FPS
- YOLOv8n TensorRT INT8: ~18-22 FPS
Waveshare IMX219-160 Wide Angle Camera for Jetson
160-degree FOV IMX219 camera compatible with Jetson Nano. Wide angle coverage essential for crowd monitoring – captures large areas without panning. Works out of the box with JetPack drivers.
People Detection Inference Code
from ultralytics import YOLO
import cv2
import jetson.utils # For CSI camera
import numpy as np
model = YOLO('yolov8n.engine') # TensorRT model
# Open CSI camera via GStreamer
gst_pipeline = (
'nvarguscamerasrc sensor-id=0 ! '
'video/x-raw(memory:NVMM),width=1280,height=720,framerate=30/1 ! '
'nvvidconv ! video/x-raw,format=BGRx ! '
'videoconvert ! video/x-raw,format=BGR ! appsink'
)
cap = cv2.VideoCapture(gst_pipeline, cv2.CAP_GSTREAMER)
while True:
ret, frame = cap.read()
if not ret: break
# Run YOLO inference - filter class 0 (person)
results = model(frame, classes=[0], conf=0.4, verbose=False)
person_count = len(results[0].boxes)
# Draw bounding boxes
annotated = results[0].plot()
# Overlay count
cv2.putText(annotated, f'People Count: {person_count}',
(10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0), 2)
cv2.imshow('Crowd Counter', annotated)
print(f'Count: {person_count}', end='r')
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release()
Zone-Based Crowd Density
def get_zone_density(boxes, frame_shape, zones):
h, w = frame_shape[:2]
zone_counts = {name: 0 for name, _ in zones.items()}
for box in boxes:
# Get person centre point
x1, y1, x2, y2 = map(int, box.xyxy[0])
px, py = (x1+x2)//2, (y1+y2)//2
for name, (rx, ry, rw, rh) in zones.items():
if rx <= px <= rx+rw and ry <= py = ALERT_THRESHOLD[zone]:
print(f'ALERT: {zone} overcrowded ({count} people)')
India-Specific Use Cases
Railway stations (Indian Railways): Platform crowd monitoring during peak hours. Alert station master when platform exceeds safe density. Particularly relevant for Mumbai local rail stations and Delhi Metro feeder buses.
Temple entrances (Tirupati, Vaishno Devi, Siddhivinayak): Queue length estimation and crowd flow monitoring. Temples already use digital token systems – integrate camera-based counting for accurate wait time estimation.
Election booths: Monitor queue lengths at polling stations for resource allocation. Works well with wide-angle cameras covering the entire queue area.
Market areas: Real-time crowd density in weekly bazaars and shopping centres. Connect to dynamic parking guidance or entry control systems.
Waveshare IMX219-77 Camera Module
77-degree FOV IMX219 compatible with Jetson Nano. Good balance of coverage area and resolution for crowd monitoring applications. Driver included in JetPack 4.6+.
Alert System Integration
import requests, time
BOT_TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
CHAT_ID = 'YOUR_CHAT_ID'
last_alert_time = {}
ALERT_COOLDOWN = 300 # 5 minute cooldown between alerts
def send_crowd_alert(zone, count, frame):
now = time.time()
if now - last_alert_time.get(zone, 0) < ALERT_COOLDOWN:
return # Still in cooldown
msg = f'CROWD ALERT: {zone} has {count} people (threshold exceeded)'
# Save frame to temp file and send
cv2.imwrite('/tmp/crowd_alert.jpg', frame)
with open('/tmp/crowd_alert.jpg', 'rb') as f:
requests.post(
f'https://api.telegram.org/bot{BOT_TOKEN}/sendPhoto',
data={'chat_id': CHAT_ID, 'caption': msg},
files={'photo': f}
)
last_alert_time[zone] = now
FAQ
How accurate is YOLO for crowd counting in India?
YOLOv8n achieves ~85% recall for well-separated people. In dense crowds where people overlap, accuracy drops to 60-70%. For dense crowds (more than 5 people per square metre), use CSRNet density estimation instead.
Can I run crowd counting on Raspberry Pi 5?
YOLOv8n on Pi 5 runs at approximately 3-5 FPS without GPU acceleration. For outdoor deployment where Jetson Nano heat management is a concern, Pi 5 with active cooling is a viable alternative for moderate crowd densities.
What is the legal position for CCTV crowd counting in India?
Counting people in public spaces is generally permitted for safety purposes. The IT Act and surveillance guidelines apply if you store identifiable images. For crowd counting that only logs counts (not images), there are minimal regulatory concerns. Consult DPDP (Digital Personal Data Protection Act 2023) for your specific use case.
Add comment