Build people counter with foot-traffic heatmap

Run YOLOX-S person detection on the AI HAT+ (DEEPX DX-M1M) and count entries, exits and occupancy across a line on the Raspberry Pi 5, with live and total foot-traffic heatmaps on its screen.

Works with

AI HAT+Product overviewonRaspberry Pi 5

How it works

The app is a standalone Python app that works in 4 stages. Detection runs on the DEEPX DX-M1M NPU (neural processing unit); the other stages run on the Raspberry Pi 5 CPU. The app does not use Frigate NVR, and its source calls no cloud service.

  1. Detect (NPU). The app shrinks each frame to a 1280-pixel-wide working frame, pads it to the model’s square input, and sends it to yolox-s_640x640_ppu.dxnn (.dxnn is the DEEPX compiled model format) through dx_engine, the DEEPX runtime’s Python package.
  2. Decode and filter (CPU). The app decodes the model’s output records, keeps only the person class (class 0) with a score above 0.45, and removes overlapping boxes with OpenCV non-maximum suppression at 0.45.
  3. Track and count (CPU). A centroid tracker links each existing track to the nearest unmatched box center. The counter checks whether a track’s center crossed the counting line.
  4. Heatmap (CPU). Each tracked person adds heat at their position to a live heatmap and a total heatmap.

Read the counts in the live window on the Raspberry Pi’s screen, and the paths walked since the app started in the total heatmap window: see Live and total heatmaps.

Requirements

Before starting

Hardware

  • Raspberry Pi 5 with the AI HAT+ (DEEPX DX-M1M)The app was run on this board only.Shop
  • Monitor attached to the Raspberry PiThe app draws its windows on the Raspberry Pi’s own screen (DISPLAY=:0).
  • A video file, or an IP camera that streams RTSPThe app’s input: [VIDEO_FILE] or [RTSP_URL] below. Create a test RTSP feed with Camera Feeds and MQTT for Frigate Projects.

Software

  • Raspberry Pi OS with the labwc desktop sessionOn the Raspberry Pi.
  • The sixfab-dx apt packageInstalls the shared Python environment at /opt/sixfab-dx/venv, which provides dx_engine.
  • git, curl and sha256sumUsed by the install script.

Install

Run every step on the Raspberry Pi 5 itself, not from another machine. install.sh downloads the model yolox-s_640x640_ppu.dxnn from the project’s GitHub release, checks its SHA-256, copies the model and the app into ~/retail-analytics, and adds opencv-python and numpy to the shared sixfab-dx virtual environment. It does not install dx_engine: it reuses the copy that the sixfab-dx package put there, and stops with an error if that environment is missing.

The script prints 3 step headings, then Install complete. and the command to start the app. The release tag, the OpenCV version and the dxrt-cli -s lines are those of your board:

== 1/3: model and files (release retail-analytics-v1.0.0) ==
== 2/3: Python environment (shared sixfab-dx venv) ==
== 3/3: verification ==
dx_engine + cv2 OK, cv2 [VERSION]
…
Install complete.

Start app

Open a terminal on the Raspberry Pi’s desktop session, not an SSH session, and pass the input to run_on_pi.sh. The script sets DISPLAY to :0 if it is not already set, and the app opens the input with OpenCV. To run on a video file:

bash ~/retail-analytics/run_on_pi.sh [VIDEO_FILE]

To run on an IP camera, pass its RTSP stream address. A password in the address is saved in your shell history, so use a camera account with limited rights.

bash ~/retail-analytics/run_on_pi.sh [RTSP_URL]

These keys control the app while one of its windows has focus:

KeyAction
qQuit and save total_heatmap_final.png
hShow or hide the live heatmap overlay
rReset the entry and exit counters and the live heatmap. The total heatmap keeps its data.

Verify

  1. The install script printed dx_engine + cv2 OK, cv2 followed by your OpenCV version, then the first lines of dxrt-cli -s.
  2. When the app starts, the terminal prints Model loaded (input followed by the model’s input size, and Keys: q=quit h=heatmap r=reset.
  3. 2 windows open on the Raspberry Pi’s screen: Retail Analytics - live (q=quit h=heatmap r=reset) and Retail Analytics - TOTAL heatmap (updates every 60s).
  4. The live window shows a green box around each detected person, a track number (#1, #2, …) at each person’s center, the magenta counting line, and the In frame, Entries, Exits and Occupancy values with a frames-per-second reading. Pressing h shows or hides the heatmap tint.
  5. When you press q, the terminal prints the final counts and the path of the saved image:
Closed. Final: entries=[N] exits=[N] occupancy=[N]
Total heatmap saved: [PATH]/total_heatmap_final.png

The image is saved in the folder that holds watch_retail_local.py, which is ~/retail-analytics.

Results

Tested on November 10, 2026 on a Raspberry Pi 5 (8 GB) with Raspberry Pi OS Trixie, the AI HAT+ (DEEPX DX-M1M) and dx_engine 3.5.0 for Python 3.13, on the Raspberry Pi’s own screen, with a recorded 4K (3840 × 2160) street clip.

No frame rate or accuracy figure is published for this app. The live window shows the rate for your board, input and resolution: the reading counts completed passes through the whole pipeline, updated once per second. Board-level figures, not measured with this app, are in the AI HAT+ benchmark results.

Details

YOLOX-S PPU model

yolox-s_640x640_ppu.dxnn is a ready-compiled YOLOX-S model from the DEEPX Model Zoo. The app uses only its person class.

PPU stands for Post-Processing Unit (see PPU type 1 in the DX-COM reference). A PPU model returns its detections as fixed-size records instead of a raw tensor, up to 750 records per frame. Each record is 32 bytes: box values in bytes 0 to 16, grid position in bytes 16 to 20, a float32 score in bytes 20 to 24 and a uint32 class label in bytes 24 to 28.

YOLOX has a multi-scale head with strides of 8, 16 and 32, and its box values are offsets from a grid cell plus a log-scale size. The app decodes them with the grid position and stride of each record. It filters score, class and grid position for all records in one NumPy operation instead of a Python loop.

Entry and exit counting

The counter uses one horizontal line at 62% of the frame height. A track that moves down across the line adds one entry; a track that moves up across it adds one exit. Occupancy is entries minus exits. Counting uses OpenCV and plain Python, with no extra counting library.

Each track is counted once. The tracker matches a detection to a track when their centers are less than 90 pixels apart in the working frame, and it can drop a track that has gone unmatched for more than 15 frames.

The line position, the matching distance and the track age limit are constants at the top of watch_retail_local.py: LINE_Y_FRAC, TRACK_MAX_DIST and TRACK_MAX_AGE.

Live and total heatmaps

On every frame, both heatmaps add 1 unit of heat to a 30 × 30 pixel square around each tracked person’s center. Both are drawn with the JET color map.

HeatmapBehaviorWhere it shows
LiveFades by a factor of 0.98 every frame, so it shows recent activity. r resets it.An overlay on the video, off at start because its dark base color dims the video. Press h to show it.
TotalNever fades and keeps every position since the app started. r does not reset it.A second window, redrawn every 60 seconds with a countdown to the next redraw. Saved as total_heatmap_final.png when you quit.

Working frame size

The app shrinks each frame to a working width of 1280 pixels before it draws or builds heatmaps. A 4K (3840 × 2160) frame has 9 times the pixels of a 1280 × 720 frame, so the heatmap, drawing and display work touch 9 times fewer pixels. No speedup was measured.

Limitations

  • Outputs. The app shows counts on screen and writes total_heatmap_final.png when you quit. It does not publish counts over MQTT, export them to a file, or keep them after a restart.
  • Inputs. The app reads 1 input per run. Multi-camera use is not documented.
  • Desktop only. The app is a desktop app that you start by hand, not a headless service. Headless use is not documented.
  • Re-entry. A person whose track the tracker drops gets a new track when they reappear and can be counted again: see Entry and exit counting.
  • Line geometry. Only the vertical direction of a track’s movement across the horizontal line matters. People who walk mostly horizontally across the frame may not be counted.
  • Tracker. The nearest-centroid tracker has no appearance matching, so crossing paths in a crowd can swap track numbers.
  • Tested scene. The test run used a street clip, not a store entrance.
  • RTSP. No run with an RTSP camera is documented, and no camera model or codec has been tested. Any stream that OpenCV opens should work; test your camera on a short run first.

Privacy

The app does not store video. It keeps frames in memory, and the only file it writes is total_heatmap_final.png on exit. That image is the color-mapped heatmap alone; the code does not draw a video frame into it.

The app’s source imports only os, sys, time, OpenCV, NumPy and dx_engine, and contains no code that sends counts or images to another host. With an RTSP camera, the video crosses your local network from the camera to the Raspberry Pi.

The app detects people but does not identify them, and it does not recognize faces. Sixfab makes no GDPR or other compliance claim for this project. Check your local rules on recording or analyzing customers before you point a camera at a shop floor.

Troubleshooting

For board-level problems, such as the NPU not showing up, see AI HAT+ troubleshooting.

The shared sixfab-dx virtual environment does not exist. Install the sixfab-dx apt package (see the README at the root of the sixfab-dx-examples repository) and run ./install.sh again. To use your own environment that already has dx_engine, pass VENV_DIR=[PATH].

dx_engine is missing from the virtual environment (step == 3/3: verification ==). Reinstall the sixfab-dx package, then run ./install.sh again; the script is safe to re-run. It reuses the package’s dx_engine and does not install or pin a version of its own.

The app needs an active desktop session (labwc). Start it from a terminal on the Raspberry Pi’s desktop, and check that a session exists:

loginctl list-sessions

OpenCV’s bundled Qt backend cannot find system fonts. The warning is harmless: it affects only the window title and Qt frame, not the text the app draws on the video.

Frequently asked questions

A camera, an infrared beam or a thermal sensor. A camera detects people in video, as in this project, and gives positions for heatmaps. An infrared beam counts each time something breaks the beam at a doorway, and a thermal sensor detects body heat.

Load the file with InferenceEngine from dx_engine and call engine.run on each frame. watch_retail_local.py does this with yolox-s_640x640_ppu.dxnn.

Next steps

License and credits

The code is licensed under the MIT License. The model is YOLOX from the DEEPX Model Zoo, licensed under Apache-2.0; its details are in the release notes of retail-analytics-v1.0.0 on GitHub.

The app runs on the DEEPX DX-RT runtime and its dx_engine Python package, and uses OpenCV and NumPy.