Tutorial 11 — People Counting and Crowd Analytics with Jetson

Count people entering and leaving a space in real time. Track occupancy, generate dwell-time reports, and trigger automatic alerts when a room reaches capacity — all running locally on the Jetson.

What you will learn

  • Bidirectional counting using a virtual tripwire line
  • Tracking individuals across frames to avoid double-counting
  • Dwell time measurement — how long each person stays in a zone
  • Occupancy alerts via MQTT when a threshold is reached
  • Exporting hourly reports to CSV automatically

Step 1 — Run the people counter

cd ~/tutorials/11-people-counting
python3 count_people.py --source 0 --line-y 360 --show

Step 2 — Bidirectional counting logic

LINE_Y   = 360
entering = 0
leaving  = 0
tracked  = {}   # track_id → last_y

for box in results[0].boxes:
    track_id = int(box.id)
    cy       = int((box.xyxy[0][1] + box.xyxy[0][3]) / 2)

    if track_id in tracked:
        prev_y = tracked[track_id]
        # Crossed the line downward = entering
        if prev_y < LINE_Y <= cy:
            entering += 1
        # Crossed the line upward = leaving
        elif prev_y > LINE_Y >= cy:
            leaving += 1

    tracked[track_id] = cy

occupancy = entering - leaving
print(f"In: {entering}  Out: {leaving}  Current: {occupancy}")

Step 3 — Alert when over capacity

import paho.mqtt.client as mqtt

MAX_CAPACITY = 50
client = mqtt.Client()
client.connect("localhost", 1883)

if occupancy > MAX_CAPACITY:
    client.publish("alerts/occupancy", f"OVER CAPACITY: {occupancy}/{MAX_CAPACITY}")
    print("⚠️  Capacity alert sent!")

Next: Tutorial 12 — PPE Compliance | Back to Jetson Kit

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
0

Subtotal