Sync motion-tracking from metro-analytics-catalog
Browse files- LICENSE +57 -0
- README.md +336 -5
- export_and_quantize.sh +93 -0
LICENSE
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This directory contains two categories of content under different licenses.
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
Scripts and Documentation
|
| 5 |
+
-------------------------
|
| 6 |
+
|
| 7 |
+
The scripts (export_and_quantize.sh) and documentation (README.md) in this
|
| 8 |
+
directory are original works by Intel Corporation, licensed under the
|
| 9 |
+
MIT License.
|
| 10 |
+
|
| 11 |
+
Copyright (C) Intel Corporation
|
| 12 |
+
|
| 13 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 14 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 15 |
+
in the Software without restriction, including without limitation the rights
|
| 16 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 17 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 18 |
+
furnished to do so, subject to the following conditions:
|
| 19 |
+
|
| 20 |
+
The above copyright notice and this permission notice shall be included in
|
| 21 |
+
all copies or substantial portions of the Software.
|
| 22 |
+
|
| 23 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 24 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 25 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 26 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 27 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 28 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 29 |
+
THE SOFTWARE.
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
YOLO26 Model
|
| 33 |
+
------------
|
| 34 |
+
|
| 35 |
+
The YOLO26 model weights and the Ultralytics framework are developed by
|
| 36 |
+
Ultralytics and licensed under the GNU Affero General Public License v3.0
|
| 37 |
+
(AGPL-3.0).
|
| 38 |
+
|
| 39 |
+
Source: https://github.com/ultralytics/ultralytics
|
| 40 |
+
License: https://github.com/ultralytics/ultralytics/blob/main/LICENSE
|
| 41 |
+
Docs: https://docs.ultralytics.com/models/yolo26/
|
| 42 |
+
|
| 43 |
+
Users must comply with the AGPL-3.0 license terms when using, modifying,
|
| 44 |
+
or distributing the YOLO26 model weights or Ultralytics software.
|
| 45 |
+
For commercial licensing options, see https://www.ultralytics.com/license.
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
BoT-SORT / ByteTrack Trackers
|
| 49 |
+
------------------------------
|
| 50 |
+
|
| 51 |
+
The BoT-SORT and ByteTrack tracking algorithms are integrated into the
|
| 52 |
+
Ultralytics framework. Original implementations:
|
| 53 |
+
|
| 54 |
+
BoT-SORT: https://github.com/NirAharon/BoT-SORT
|
| 55 |
+
ByteTrack: https://github.com/FoundationVision/ByteTrack
|
| 56 |
+
|
| 57 |
+
Users should consult the respective repositories for license terms.
|
README.md
CHANGED
|
@@ -1,5 +1,336 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
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.1.0, NNCF 3.0.0, DLStreamer 2026.0, Ultralytics 8.3.0, Python 3.11+
|
| 4 |
+
|
| 5 |
+
| Property | Value |
|
| 6 |
+
|---|---|
|
| 7 |
+
| **Category** | Object Detection + Multi-Object Tracking |
|
| 8 |
+
| **Base Model** | [YOLO26](https://docs.ultralytics.com/models/yolo26/) (Ultralytics) + [BoT-SORT](https://github.com/NirAharon/BoT-SORT) tracker |
|
| 9 |
+
| **Source Framework** | PyTorch (Ultralytics) |
|
| 10 |
+
| **Supported Precisions** | FP32, FP16, FP16-INT8 |
|
| 11 |
+
| **Inference Engine** | OpenVINO |
|
| 12 |
+
| **Hardware** | CPU, GPU, NPU |
|
| 13 |
+
| **Detected Class(es)** | Configurable (default: all 80 COCO classes) |
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## Overview
|
| 18 |
+
|
| 19 |
+
Motion Tracking is a Metro Analytics use case that detects objects and assigns persistent track IDs across frames, enabling trajectory analysis and temporal event detection.
|
| 20 |
+
It is built on [YOLO26](https://docs.ultralytics.com/models/yolo26/), a state-of-the-art real-time object detector quantized to INT8, paired with a multi-object tracker:
|
| 21 |
+
|
| 22 |
+
- **OpenVINO pipeline:** YOLO26 INT8 detection + Ultralytics built-in [BoT-SORT](https://github.com/NirAharon/BoT-SORT) or [ByteTrack](https://github.com/FoundationVision/ByteTrack) tracker via `model.track()`.
|
| 23 |
+
- **DLStreamer pipeline:** YOLO26 FP16 detection via `gvadetect` + `gvatrack` element with `tracking-type=short-term-imageless`.
|
| 24 |
+
|
| 25 |
+
Each detected object receives a unique `track_id` that persists across frames as long as the object remains visible.
|
| 26 |
+
Outputs include per-object trajectories suitable for path analysis, dwell-time computation, and zone-based event triggers.
|
| 27 |
+
|
| 28 |
+
Typical Metro deployments include:
|
| 29 |
+
|
| 30 |
+
- **Pedestrian Trajectory Analysis** -- map walking paths through stations for flow optimization.
|
| 31 |
+
- **Dwell-Time Measurement** -- measure how long individuals stay in specific zones.
|
| 32 |
+
- **Zone-Based Event Detection** -- trigger alerts when tracked objects enter or exit defined areas.
|
| 33 |
+
- **Traffic Flow Analytics** -- track vehicles through intersections for signal timing optimization.
|
| 34 |
+
- **Incident Replay** -- reconstruct object paths for post-event forensic review.
|
| 35 |
+
|
| 36 |
+
Available YOLO26 variants: `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`.
|
| 37 |
+
Smaller variants (`yolo26n`, `yolo26s`) are recommended for high-FPS edge deployment.
|
| 38 |
+
The default tracker is BoT-SORT; ByteTrack is available as an alternative with lower computational overhead.
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## Prerequisites
|
| 43 |
+
|
| 44 |
+
- Python 3.11+
|
| 45 |
+
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 46 |
+
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
|
| 47 |
+
|
| 48 |
+
Create and activate a Python virtual environment before running the scripts:
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
python3 -m venv .venv --system-site-packages
|
| 52 |
+
source .venv/bin/activate
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## Getting Started
|
| 58 |
+
|
| 59 |
+
### Download and Quantize Model
|
| 60 |
+
|
| 61 |
+
Run the provided script to download, export to OpenVINO IR, and optionally quantize:
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
chmod +x export_and_quantize.sh
|
| 65 |
+
./export_and_quantize.sh yolo26n # default: FP16
|
| 66 |
+
./export_and_quantize.sh yolo26n FP32 # full-precision
|
| 67 |
+
./export_and_quantize.sh yolo26n INT8 # quantized
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
Replace `yolo26n` with any variant (`yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`).
|
| 71 |
+
The second argument selects the precision (`FP32`, `FP16`, `INT8`); the default is **FP16**.
|
| 72 |
+
|
| 73 |
+
The script performs the following steps:
|
| 74 |
+
|
| 75 |
+
1. Installs dependencies (`openvino`, `ultralytics`; adds `nncf` for INT8).
|
| 76 |
+
2. Downloads the sample test video (`test_video.mp4`).
|
| 77 |
+
3. Downloads the PyTorch weights and exports to OpenVINO IR.
|
| 78 |
+
4. *(INT8 only)* Quantizes the model using NNCF post-training quantization.
|
| 79 |
+
|
| 80 |
+
Output files:
|
| 81 |
+
|
| 82 |
+
- `yolo26n_openvino_model/` -- FP32 or FP16 OpenVINO IR model directory.
|
| 83 |
+
- `yolo26n_tracking_int8.xml` / `yolo26n_tracking_int8.bin` -- INT8 quantized model *(only when `INT8` is selected)*.
|
| 84 |
+
|
| 85 |
+
#### Precision / Device Compatibility
|
| 86 |
+
|
| 87 |
+
| Precision | CPU | GPU | NPU |
|
| 88 |
+
|---|---|---|---|
|
| 89 |
+
| FP32 | Yes | Yes | No |
|
| 90 |
+
| FP16 | Yes | Yes | Yes |
|
| 91 |
+
| INT8 | Yes | Yes | Yes |
|
| 92 |
+
|
| 93 |
+
> **Note:** For production accuracy, replace the random calibration tensors in
|
| 94 |
+
> `export_and_quantize.sh` with a representative sample of frames from the
|
| 95 |
+
> target deployment site.
|
| 96 |
+
|
| 97 |
+
### OpenVINO Sample
|
| 98 |
+
|
| 99 |
+
The sample below uses the Ultralytics `model.track()` API with the FP16
|
| 100 |
+
OpenVINO model directory to detect and track objects in a video, assigning
|
| 101 |
+
persistent track IDs via the built-in BoT-SORT tracker.
|
| 102 |
+
Each annotated frame -- with bounding boxes, track IDs, and per-track
|
| 103 |
+
trajectory polylines -- is written to `output.mp4`.
|
| 104 |
+
Change the `device` string to run on CPU, GPU, or NPU.
|
| 105 |
+
|
| 106 |
+
> **Note:** Ultralytics requires the OpenVINO model directory (e.g.,
|
| 107 |
+
> `yolo26n_openvino_model/`) rather than a bare `.xml` file.
|
| 108 |
+
> The INT8 model (`yolo26n_tracking_int8.xml`) can be used directly
|
| 109 |
+
> with the OpenVINO Python API but not with Ultralytics `YOLO()`.
|
| 110 |
+
|
| 111 |
+
```python
|
| 112 |
+
import subprocess
|
| 113 |
+
from collections import defaultdict
|
| 114 |
+
|
| 115 |
+
import cv2
|
| 116 |
+
import numpy as np
|
| 117 |
+
from ultralytics import YOLO
|
| 118 |
+
|
| 119 |
+
# Load the FP16 OpenVINO model directory for tracking.
|
| 120 |
+
# Change device to "gpu:0" or "npu:0" for GPU/NPU.
|
| 121 |
+
model = YOLO("yolo26n_openvino_model", task="detect")
|
| 122 |
+
|
| 123 |
+
video_path = "test_video.mp4"
|
| 124 |
+
cap = cv2.VideoCapture(video_path)
|
| 125 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
| 126 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 127 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 128 |
+
|
| 129 |
+
# Pipe frames to ffmpeg for H.264 output (universally playable).
|
| 130 |
+
proc = subprocess.Popen(
|
| 131 |
+
["ffmpeg", "-y", "-f", "rawvideo", "-pix_fmt", "bgr24",
|
| 132 |
+
"-s", f"{width}x{height}", "-r", str(fps),
|
| 133 |
+
"-i", "pipe:0", "-c:v", "libx264", "-pix_fmt", "yuv420p",
|
| 134 |
+
"-movflags", "+faststart", "output.mp4"],
|
| 135 |
+
stdin=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Distinct colors for trajectory lines (one per track ID).
|
| 139 |
+
COLORS = [
|
| 140 |
+
(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
|
| 141 |
+
(255, 0, 255), (0, 255, 255), (128, 0, 255), (255, 128, 0),
|
| 142 |
+
]
|
| 143 |
+
track_history: dict[int, list[tuple[float, float]]] = defaultdict(list)
|
| 144 |
+
|
| 145 |
+
while cap.isOpened():
|
| 146 |
+
success, frame = cap.read()
|
| 147 |
+
if not success:
|
| 148 |
+
break
|
| 149 |
+
|
| 150 |
+
# Run YOLO26 tracking with BoT-SORT (default).
|
| 151 |
+
# Use tracker="bytetrack.yaml" for ByteTrack alternative.
|
| 152 |
+
results = model.track(frame, persist=True, conf=0.4, tracker="botsort.yaml")
|
| 153 |
+
result = results[0]
|
| 154 |
+
|
| 155 |
+
annotated = result.plot()
|
| 156 |
+
|
| 157 |
+
if result.boxes and result.boxes.is_track:
|
| 158 |
+
boxes = result.boxes.xywh.cpu()
|
| 159 |
+
track_ids = result.boxes.id.int().cpu().tolist()
|
| 160 |
+
classes = result.boxes.cls.int().cpu().tolist()
|
| 161 |
+
|
| 162 |
+
for box, track_id in zip(boxes, track_ids):
|
| 163 |
+
x, y, _w, _h = box
|
| 164 |
+
track = track_history[track_id]
|
| 165 |
+
track.append((float(x), float(y)))
|
| 166 |
+
if len(track) > 30:
|
| 167 |
+
track.pop(0)
|
| 168 |
+
|
| 169 |
+
color = COLORS[track_id % len(COLORS)]
|
| 170 |
+
points = np.array(track, dtype=np.int32).reshape((-1, 1, 2))
|
| 171 |
+
cv2.polylines(annotated, [points], False, color, 2)
|
| 172 |
+
|
| 173 |
+
for tid, cls_id in zip(track_ids, classes):
|
| 174 |
+
cx, cy = track_history[tid][-1]
|
| 175 |
+
print(f" Track {tid}: class={cls_id} center=({cx:.0f},{cy:.0f})", flush=True)
|
| 176 |
+
|
| 177 |
+
proc.stdin.write(annotated.tobytes())
|
| 178 |
+
|
| 179 |
+
cap.release()
|
| 180 |
+
proc.stdin.close()
|
| 181 |
+
proc.wait()
|
| 182 |
+
print("Wrote output.mp4", flush=True)
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
**Device targets:**
|
| 186 |
+
|
| 187 |
+
- Default runs on CPU via OpenVINO.
|
| 188 |
+
- For GPU: set `device="gpu:0"` in the `model.track()` call.
|
| 189 |
+
- For NPU: set `device="npu:0"` (validate availability with `benchmark_app -d NPU`).
|
| 190 |
+
|
| 191 |
+
### DLStreamer Sample
|
| 192 |
+
|
| 193 |
+
The pipeline below runs the YOLO26 FP16 detector via `gvadetect` on
|
| 194 |
+
`test_video.mp4`, attaches persistent track IDs with `gvatrack`
|
| 195 |
+
(`short-term-imageless` tracker), and overlays bounding boxes with
|
| 196 |
+
`gvawatermark`. Frames are pulled from an `appsink`, per-track trajectory
|
| 197 |
+
polylines are drawn with OpenCV, and the result is muxed to `output.mp4`
|
| 198 |
+
(H.264 via ffmpeg).
|
| 199 |
+
|
| 200 |
+
> **Notes on running this sample:**
|
| 201 |
+
>
|
| 202 |
+
> - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml`). Class names are
|
| 203 |
+
> read automatically from the model's embedded `metadata.yaml` by
|
| 204 |
+
> DLStreamer 2026.0+ -- no external `labels-file` is required.
|
| 205 |
+
> - Export `PYTHONPATH` so the DLStreamer Python module is importable:
|
| 206 |
+
>
|
| 207 |
+
> ```bash
|
| 208 |
+
> source /opt/intel/openvino_2026/setupvars.sh
|
| 209 |
+
> source /opt/intel/dlstreamer/scripts/setup_dls_env.sh
|
| 210 |
+
> export PYTHONPATH=/opt/intel/dlstreamer/python:\
|
| 211 |
+
> /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
|
| 212 |
+
> ```
|
| 213 |
+
|
| 214 |
+
```python
|
| 215 |
+
import subprocess
|
| 216 |
+
from collections import defaultdict
|
| 217 |
+
|
| 218 |
+
import cv2
|
| 219 |
+
import numpy as np
|
| 220 |
+
import gi
|
| 221 |
+
|
| 222 |
+
gi.require_version("Gst", "1.0")
|
| 223 |
+
from gi.repository import Gst
|
| 224 |
+
from gstgva import VideoFrame
|
| 225 |
+
|
| 226 |
+
Gst.init(None)
|
| 227 |
+
|
| 228 |
+
# For GPU: change device=CPU to device=GPU and add vapostproc after decodebin.
|
| 229 |
+
# For NPU: change device=CPU to device=NPU (batch-size=1, nireq=4 recommended).
|
| 230 |
+
pipeline_str = (
|
| 231 |
+
"filesrc location=test_video.mp4 ! decodebin ! videoconvert ! "
|
| 232 |
+
"video/x-raw,format=BGR ! "
|
| 233 |
+
"gvadetect model=yolo26n_openvino_model/yolo26n.xml "
|
| 234 |
+
"device=CPU threshold=0.4 ! queue ! "
|
| 235 |
+
"gvatrack tracking-type=short-term-imageless ! queue ! "
|
| 236 |
+
"gvawatermark ! appsink name=sink emit-signals=false sync=false"
|
| 237 |
+
)
|
| 238 |
+
pipeline = Gst.parse_launch(pipeline_str)
|
| 239 |
+
appsink = pipeline.get_by_name("sink")
|
| 240 |
+
|
| 241 |
+
# Distinct colors for trajectory lines (one per track ID).
|
| 242 |
+
COLORS = [
|
| 243 |
+
(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
|
| 244 |
+
(255, 0, 255), (0, 255, 255), (128, 0, 255), (255, 128, 0),
|
| 245 |
+
]
|
| 246 |
+
track_history: dict[int, list[tuple[int, int]]] = defaultdict(list)
|
| 247 |
+
|
| 248 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 249 |
+
|
| 250 |
+
proc = None
|
| 251 |
+
|
| 252 |
+
while True:
|
| 253 |
+
sample = appsink.emit("pull-sample")
|
| 254 |
+
if sample is None:
|
| 255 |
+
break
|
| 256 |
+
|
| 257 |
+
buf = sample.get_buffer()
|
| 258 |
+
caps = sample.get_caps()
|
| 259 |
+
struct = caps.get_structure(0)
|
| 260 |
+
width = struct.get_value("width")
|
| 261 |
+
height = struct.get_value("height")
|
| 262 |
+
|
| 263 |
+
# Start ffmpeg encoder on the first frame.
|
| 264 |
+
if proc is None:
|
| 265 |
+
ok, fps_num, fps_den = struct.get_fraction("framerate")
|
| 266 |
+
fps = fps_num / fps_den if ok and fps_den > 0 else 30.0
|
| 267 |
+
proc = subprocess.Popen(
|
| 268 |
+
["ffmpeg", "-y", "-f", "rawvideo", "-pix_fmt", "bgr24",
|
| 269 |
+
"-s", f"{width}x{height}", "-r", str(fps),
|
| 270 |
+
"-i", "pipe:0", "-c:v", "libx264", "-pix_fmt", "yuv420p",
|
| 271 |
+
"-movflags", "+faststart", "output.mp4"],
|
| 272 |
+
stdin=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# Read detection / tracking metadata.
|
| 276 |
+
frame = VideoFrame(buf, caps=caps)
|
| 277 |
+
regions_data = []
|
| 278 |
+
for region in frame.regions():
|
| 279 |
+
tid = region.object_id()
|
| 280 |
+
label = region.label()
|
| 281 |
+
rect = region.rect()
|
| 282 |
+
cx = int(rect.x + rect.w / 2)
|
| 283 |
+
cy = int(rect.y + rect.h / 2)
|
| 284 |
+
regions_data.append((tid, label, cx, cy))
|
| 285 |
+
|
| 286 |
+
# Map buffer read-only and copy pixels to a writable numpy array.
|
| 287 |
+
success, map_info = buf.map(Gst.MapFlags.READ)
|
| 288 |
+
if not success:
|
| 289 |
+
continue
|
| 290 |
+
arr = np.ndarray((height, width, 3), dtype=np.uint8,
|
| 291 |
+
buffer=map_info.data).copy()
|
| 292 |
+
buf.unmap(map_info)
|
| 293 |
+
|
| 294 |
+
# Draw per-track trajectory polylines on the frame copy.
|
| 295 |
+
for tid, label, cx, cy in regions_data:
|
| 296 |
+
track = track_history[tid]
|
| 297 |
+
track.append((cx, cy))
|
| 298 |
+
if len(track) > 30:
|
| 299 |
+
track.pop(0)
|
| 300 |
+
color = COLORS[tid % len(COLORS)]
|
| 301 |
+
pts = np.array(track, dtype=np.int32).reshape((-1, 1, 2))
|
| 302 |
+
cv2.polylines(arr, [pts], False, color, 2)
|
| 303 |
+
print(f" Track {tid}: {label} center=({cx},{cy})", flush=True)
|
| 304 |
+
|
| 305 |
+
proc.stdin.write(arr.tobytes())
|
| 306 |
+
|
| 307 |
+
pipeline.set_state(Gst.State.NULL)
|
| 308 |
+
if proc:
|
| 309 |
+
proc.stdin.close()
|
| 310 |
+
proc.wait()
|
| 311 |
+
print("Wrote output.mp4", flush=True)
|
| 312 |
+
```
|
| 313 |
+
|
| 314 |
+
**Device targets:**
|
| 315 |
+
|
| 316 |
+
- `device=CPU` -- default in the sample code.
|
| 317 |
+
- `device=GPU` -- add `vapostproc` after `decodebin` for zero-copy color conversion.
|
| 318 |
+
- `device=NPU` -- use `batch-size=1` and `nireq=4` for best NPU utilization.
|
| 319 |
+
|
| 320 |
+
---
|
| 321 |
+
|
| 322 |
+
## License
|
| 323 |
+
|
| 324 |
+
Copyright (C) Intel Corporation. All rights reserved.
|
| 325 |
+
Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
| 326 |
+
|
| 327 |
+
## References
|
| 328 |
+
|
| 329 |
+
- [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/)
|
| 330 |
+
- [Ultralytics Multi-Object Tracking](https://docs.ultralytics.com/modes/track/)
|
| 331 |
+
- [BoT-SORT Tracker](https://github.com/NirAharon/BoT-SORT)
|
| 332 |
+
- [ByteTrack Tracker](https://github.com/FoundationVision/ByteTrack)
|
| 333 |
+
- [Intel DLStreamer gvatrack](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/elements/gvatrack.html)
|
| 334 |
+
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 335 |
+
- [NNCF Post-Training Quantization](https://docs.openvino.ai/latest/nncf_ptq_introduction.html)
|
| 336 |
+
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/index.html)
|
export_and_quantize.sh
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
# SPDX-License-Identifier: MIT
|
| 3 |
+
# Copyright (C) Intel Corporation
|
| 4 |
+
#
|
| 5 |
+
# Export a YOLO26 detector for motion tracking to OpenVINO IR.
|
| 6 |
+
# The tracker itself (BoT-SORT / ByteTrack) runs at inference time via
|
| 7 |
+
# Ultralytics or DLStreamer gvatrack; no separate model export is needed.
|
| 8 |
+
# Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION]
|
| 9 |
+
# Example: ./export_and_quantize.sh yolo26n FP16
|
| 10 |
+
#
|
| 11 |
+
# Supported precisions:
|
| 12 |
+
# FP32 -- Full-precision floating-point weights
|
| 13 |
+
# FP16 -- Half-precision floating-point weights (default)
|
| 14 |
+
# INT8 -- Quantized 8-bit integer weights (requires NNCF)
|
| 15 |
+
#
|
| 16 |
+
# Precision / device compatibility:
|
| 17 |
+
# | Precision | CPU | GPU | NPU |
|
| 18 |
+
# |-----------|-----|-----|-----|
|
| 19 |
+
# | FP32 | Yes | Yes | No |
|
| 20 |
+
# | FP16 | Yes | Yes | Yes |
|
| 21 |
+
# | INT8 | Yes | Yes | Yes |
|
| 22 |
+
|
| 23 |
+
set -euo pipefail
|
| 24 |
+
|
| 25 |
+
MODEL_NAME="${1:-yolo26n}"
|
| 26 |
+
PRECISION="${2:-FP16}"
|
| 27 |
+
PRECISION="$(echo "${PRECISION}" | tr '[:lower:]' '[:upper:]')"
|
| 28 |
+
|
| 29 |
+
if [[ "${PRECISION}" != "FP32" && "${PRECISION}" != "FP16" && "${PRECISION}" != "INT8" ]]; then
|
| 30 |
+
echo "ERROR: unsupported precision '${PRECISION}'. Choose FP32, FP16, or INT8." >&2
|
| 31 |
+
exit 1
|
| 32 |
+
fi
|
| 33 |
+
|
| 34 |
+
echo "--- Installing dependencies ---"
|
| 35 |
+
if [[ "${PRECISION}" == "INT8" ]]; then
|
| 36 |
+
pip install -qU "openvino>=2026.0.0" "nncf>=3.0.0" ultralytics
|
| 37 |
+
else
|
| 38 |
+
pip install -qU "openvino>=2026.0.0" ultralytics
|
| 39 |
+
fi
|
| 40 |
+
|
| 41 |
+
echo "--- Downloading sample test video ---"
|
| 42 |
+
if [[ ! -f test_video.mp4 ]]; then
|
| 43 |
+
wget -q -O test_video.mp4 \
|
| 44 |
+
https://github.com/intel-iot-devkit/sample-videos/raw/master/people-detection.mp4
|
| 45 |
+
echo "Downloaded: test_video.mp4"
|
| 46 |
+
else
|
| 47 |
+
echo "Already present: test_video.mp4"
|
| 48 |
+
fi
|
| 49 |
+
|
| 50 |
+
if [[ "${PRECISION}" == "FP32" ]]; then
|
| 51 |
+
HALF_FLAG="False"
|
| 52 |
+
EXPORT_LABEL="FP32"
|
| 53 |
+
else
|
| 54 |
+
HALF_FLAG="True"
|
| 55 |
+
EXPORT_LABEL="FP16"
|
| 56 |
+
fi
|
| 57 |
+
|
| 58 |
+
echo "--- Exporting ${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---"
|
| 59 |
+
python3 -c "
|
| 60 |
+
from ultralytics import YOLO
|
| 61 |
+
|
| 62 |
+
model = YOLO('${MODEL_NAME}.pt')
|
| 63 |
+
model.export(format='openvino', half=${HALF_FLAG}, dynamic=False, imgsz=640)
|
| 64 |
+
print('Export complete: ${MODEL_NAME}_openvino_model/')
|
| 65 |
+
"
|
| 66 |
+
|
| 67 |
+
if [[ "${PRECISION}" == "INT8" ]]; then
|
| 68 |
+
echo "--- Quantizing to INT8 with NNCF ---"
|
| 69 |
+
python3 -c "
|
| 70 |
+
import nncf
|
| 71 |
+
import openvino as ov
|
| 72 |
+
import numpy as np
|
| 73 |
+
|
| 74 |
+
core = ov.Core()
|
| 75 |
+
model = core.read_model('${MODEL_NAME}_openvino_model/${MODEL_NAME}.xml')
|
| 76 |
+
|
| 77 |
+
def transform_fn(data_item):
|
| 78 |
+
return np.random.rand(1, 3, 640, 640).astype(np.float32)
|
| 79 |
+
|
| 80 |
+
calibration_dataset = nncf.Dataset(list(range(300)), transform_fn)
|
| 81 |
+
|
| 82 |
+
quantized = nncf.quantize(
|
| 83 |
+
model,
|
| 84 |
+
calibration_dataset,
|
| 85 |
+
preset=nncf.QuantizationPreset.MIXED,
|
| 86 |
+
subset_size=300,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
ov.save_model(quantized, '${MODEL_NAME}_tracking_int8.xml')
|
| 90 |
+
print('Quantization complete: ${MODEL_NAME}_tracking_int8.xml')
|
| 91 |
+
"
|
| 92 |
+
fi
|
| 93 |
+
echo "--- Done ---"
|