---
title: "Build people counter with foot-traffic heatmap"
description: "People counter on a Raspberry Pi 5: YOLOX-S on the Sixfab AI HAT+ (DEEPX DX-M1M) finds people; the Raspberry Pi counts entries and exits and maps foot traffic."
url: "https://docs.sixfab.com/projects/ai-hat-plus/build-people-counter-foot-traffic-heatmap/"
last_updated: "2026-10-12"
---

# 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+](https://docs.sixfab.com/edge-ai/ai-hat-plus/) on Raspberry Pi 5

![The live window on the Raspberry Pi desktop: people on a street in green boxes with track numbers, a magenta counting line across the frame, and the In frame, Entries, Exits, Occupancy and FPS counters in the top left corner](https://docs.sixfab.com/wp-content/uploads/sixfab-docs/09-retail-analytics-live-window-green-boxes-counting-line-street.jpg)

## 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](https://docs.sixfab.com/projects/ai-hat-plus/build-people-counter-foot-traffic-heatmap/#live-and-total-heatmaps).

## Requirements

**Before starting**

**Set up first**

[AI HAT+ quickstart](https://docs.sixfab.com/edge-ai/ai-hat-plus/quickstart/) (Ends with the board and its DEEPX runtime (DX-RT) working)

**Hardware**

- **Raspberry Pi 5 with the AI HAT+ (DEEPX DX-M1M)**: The app was run on this board only. [Shop](https://shop.sixfab.com/products/ai-hat-plus-raspberry-pi-5?utm_source=docs.sixfab.com&utm_medium=referral&utm_campaign=ai-projects&utm_content=retail-people-counter-heatmap-raspberry-pi-5)
- **Monitor attached to the Raspberry Pi**: The app draws its windows on the Raspberry Pi’s own screen (`DISPLAY=:0`).
- **A video file, or an IP camera that streams RTSP**: The app’s input: `[VIDEO_FILE]` or `[RTSP_URL]` below. Create a test RTSP feed with [Camera Feeds and MQTT for Frigate Projects](https://docs.sixfab.com/projects/ai-hat-plus/connect-camera-feeds-mqtt-frigate/).

**Software**

- **Raspberry Pi OS with the labwc desktop session**: On the Raspberry Pi.
- **The `sixfab-dx` apt package**: Installs the shared Python environment at `/opt/sixfab-dx/venv`, which provides `dx_engine`.
- **`git`, `curl` and `sha256sum`**: Used 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.

**Install script**

Clone the repository, open the project folder and run the installer. `--rtsp-url` is optional: the installer only prints it in the final run command, so leave it out to pass a video file when you start the app.

```bash
git clone https://github.com/sixfab/sixfab-dx-examples.git
cd sixfab-dx-examples/python_examples/retail-analytics
./install.sh --rtsp-url [RTSP_URL]
```

**Manual install**

1. **Get code**

   Clone the repository and open the project folder.

   ```bash
   git clone https://github.com/sixfab/sixfab-dx-examples.git
   cd sixfab-dx-examples/python_examples/retail-analytics
   ```

2. **Download model**

   Download the model from the project’s GitHub release.

   ```bash
   mkdir -p model
   curl -fL -o model/yolox-s_640x640_ppu.dxnn https://github.com/sixfab/sixfab-dx-examples/releases/download/retail-analytics-v1.0.0/yolox-s_640x640_ppu.dxnn
   ```

3. **Check model**

   Compare the file with the SHA-256 in `models.txt`.

   ```bash
   awk '$1 !~ /^#/ && NF {print $3 "  " $2}' models.txt | sha256sum -c -
   ```

   ```text
   model/yolox-s_640x640_ppu.dxnn: OK
   ```

4. **Run install script**

   The script copies the model and the app, adds the Python packages and runs a check.

   ```bash
   ./install.sh
   ```

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:

```text
== 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
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
bash ~/retail-analytics/run_on_pi.sh [RTSP_URL]
```

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

| Key | Action |
| --- | --- |
| `q` | Quit and save `total_heatmap_final.png` |
| `h` | Show or hide the live heatmap overlay |
| `r` | Reset 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:

```text
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](https://sixfab.com/blog/raspberry-pi-5-ai-hat-benchmark-deepx-vs-hailo/).

## 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](https://docs.sixfab.com/edge-ai/ai-model-deployment/custom-models-dxnn-sdk/dx-com-configuration/#ppu-type-1)). 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.

| Heatmap | Behavior | Where it shows |
| --- | --- | --- |
| Live | Fades 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. |
| Total | Never 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. |

![The TOTAL heatmap window: bright patches on a dark blue map where people walked since the app started](https://docs.sixfab.com/wp-content/uploads/sixfab-docs/09-retail-analytics-total-heatmap-paths-walked-since-start.png)

### 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](https://docs.sixfab.com/projects/ai-hat-plus/build-people-counter-foot-traffic-heatmap/#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](https://docs.sixfab.com/edge-ai/ai-hat-plus/troubleshooting/).

### `install.sh` prints “ERROR: Python environment not found at /opt/sixfab-dx/venv”

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]`.

### The verification step fails with an `ImportError` for `dx_engine`

`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 window does not open

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:

```bash
loginctl list-sessions
```

### The terminal shows `QFontDatabase: Cannot find font directory .../cv2/qt/fonts`

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

### What kind of sensor can I use to count people?

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.

### How do I run a DEEPX Model Zoo .dxnn model on a Raspberry Pi 5?

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

- [Choose project](https://docs.sixfab.com/projects/ai-hat-plus/choose-ai-hat-plus-project/): Every project in this section
- [Boss Detector](https://docs.sixfab.com/projects/ai-hat-plus/get-person-alert-camera-boss-detector/): Person detection with Frigate and an MQTT listener
- [Pose estimation](https://docs.sixfab.com/projects/ai-hat-plus/detect-falls-count-reps-pose-estimation/): Another Python pipeline on the DEEPX NPU
- [Sixfab Model Zoo](https://docs.sixfab.com/edge-ai/ai-model-deployment/sixfab-model-zoo/): More ready-compiled .dxnn models for the DEEPX NPU
- [System monitoring](https://docs.sixfab.com/edge-ai/ai-model-deployment/system-monitoring/): Check the NPU’s load while an app runs

## 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](https://github.com/sixfab/sixfab-dx-examples/releases/tag/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.
