vagheshpatel commited on
Commit
a82c780
·
verified ·
1 Parent(s): 1866ad2

Update native RC1 samples and supported device documentation

Browse files
Files changed (5) hide show
  1. README.md +65 -15
  2. config.json +11 -3
  3. dlstreamer_sample.py +127 -0
  4. export_and_quantize.sh +119 -11
  5. openvino_sample.py +103 -0
README.md CHANGED
@@ -8,6 +8,7 @@ tags:
8
  - intel
9
  - yolo
10
  - yolo26
 
11
  - heatmap
12
  - speed
13
  - traffic
@@ -24,8 +25,8 @@ language:
24
  | Property | Value |
25
  |---|---|
26
  | **Category** | Object Detection + Speed Heatmap Aggregation |
27
- | **Base Model** | [YOLO26](https://docs.ultralytics.com/models/yolo26/) (Ultralytics) |
28
- | **Source Framework** | PyTorch (Ultralytics) |
29
  | **Supported Precisions** | FP32, FP16, INT8 (mixed-precision) |
30
  | **Inference Engine** | OpenVINO |
31
  | **Hardware** | CPU, GPU, NPU |
@@ -58,17 +59,36 @@ Typical Metro deployments include:
58
  - **Pedestrian Flow Analysis** -- compare fast throughways against lingering areas.
59
  - **Incident Spotting** -- flag unusually fast or stalled movement.
60
 
61
- Available variants: `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`.
62
  Smaller variants (`yolo26n`, `yolo26s`) are recommended for high-FPS edge
63
  deployment; larger variants improve recall for small or distant objects.
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  ---
66
 
67
  ## Prerequisites
68
 
 
 
 
 
69
  - Python 3.11+
70
  - [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
71
- - [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
72
 
73
  Create and activate a Python virtual environment before running the scripts:
74
 
@@ -96,22 +116,25 @@ chmod +x export_and_quantize.sh
96
 
97
  This exports the default **yolo26n** model in **FP16** precision.
98
 
99
- #### Optional: Select a Different Variant or Precision
100
 
101
  ```bash
102
- ./export_and_quantize.sh yolo26n FP32 # full-precision
103
- ./export_and_quantize.sh yolo26n INT8 # quantized
104
- ./export_and_quantize.sh yolo26s # larger variant, default FP16
 
 
105
  ```
106
 
 
107
  The script performs the following steps:
108
 
109
- 1. Installs dependencies (`openvino`, `ultralytics`; adds `nncf` for INT8).
110
  2. Downloads a sample test image (`test.jpg`) and a sample test video (`test_video.mp4`).
111
  3. Downloads the PyTorch weights and exports to OpenVINO IR.
112
  4. *(INT8 only)* Quantizes the model using NNCF post-training quantization.
113
 
114
- Output files:
115
 
116
  - `yolo26n_openvino_model/` -- FP32 or FP16 OpenVINO IR model directory.
117
  - `yolo26n_heatmap_int8.xml` / `.bin` -- INT8 quantized model *(only when `INT8` is selected)*.
@@ -132,13 +155,24 @@ overlay (red = fast, blue = slow) to `output_openvino.mp4`.
132
  It also saves the final speed heatmap as `heatmap.jpg`.
133
  Change the `device` string to run on CPU, GPU, or NPU.
134
 
 
 
 
135
  ```python
 
 
 
 
 
 
136
  import cv2
137
  import numpy as np
138
  import openvino as ov
139
 
140
  CONF_THRESHOLD = 0.4
141
- INPUT_SIZE = 640
 
 
142
  HEATMAP_ALPHA = 0.55
143
  MATCH_DIST = 80.0 # max px between frames to treat detections as the same object
144
  MAX_SPEED = 20.0 # px/frame that maps to full red
@@ -161,7 +195,7 @@ def render_speed_heatmap(frame, speed_sum, count, alpha):
161
 
162
 
163
  core = ov.Core()
164
- model = core.read_model("yolo26n_openvino_model/yolo26n.xml")
165
 
166
  # Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
167
  compiled = core.compile_model(model, "CPU")
@@ -229,6 +263,7 @@ if heatmap_color is not None:
229
 
230
  print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)
231
  ```
 
232
 
233
  **Device targets:**
234
 
@@ -249,7 +284,7 @@ before encoding to `output_dlstreamer.mp4`.
249
 
250
  > **Notes on running this sample:**
251
  >
252
- > - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml`). Class names are
253
  > read automatically from the model's embedded `metadata.yaml` by
254
  > DLStreamer 2026.0+ -- no external `labels-file` is required.
255
  > - Export `PYTHONPATH` so the DLStreamer Python module is importable:
@@ -261,7 +296,16 @@ before encoding to `output_dlstreamer.mp4`.
261
  > /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
262
  > ```
263
 
 
 
 
264
  ```python
 
 
 
 
 
 
265
  import gi
266
 
267
  gi.require_version("Gst", "1.0")
@@ -297,12 +341,15 @@ def render_speed_heatmap(frame, speed_sum, count, alpha):
297
  return overlay.astype(np.uint8)
298
 
299
 
 
 
 
300
  # For CPU: change device=GPU to device=CPU.
301
  # For NPU: change device=GPU to device=NPU (batch-size=1, nireq=4 recommended).
302
  pipeline_str = (
303
  f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
304
  "videoconvert ! video/x-raw,format=BGR ! "
305
- "gvadetect model=yolo26n_openvino_model/yolo26n.xml "
306
  "device=GPU "
307
  "threshold=0.4 ! queue ! "
308
  "appsink name=sink emit-signals=false sync=false"
@@ -381,6 +428,7 @@ if writer:
381
  writer.release()
382
  print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)
383
  ```
 
384
 
385
  **Device targets:**
386
 
@@ -401,7 +449,9 @@ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
401
  ## References
402
 
403
  - [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/)
 
 
404
  - [Ultralytics Heatmap Guide](https://docs.ultralytics.com/guides/heatmaps/)
405
  - [OpenCV Color Maps](https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html)
406
  - [OpenVINO Documentation](https://docs.openvino.ai/)
407
- - [Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/index.html)
 
8
  - intel
9
  - yolo
10
  - yolo26
11
+ - detr
12
  - heatmap
13
  - speed
14
  - traffic
 
25
  | Property | Value |
26
  |---|---|
27
  | **Category** | Object Detection + Speed Heatmap Aggregation |
28
+ | **Base Model** | [YOLO26](https://docs.ultralytics.com/models/yolo26/) (Ultralytics, default) or [DETR](https://huggingface.co/facebook/detr-resnet-50) (Facebook, alternative) |
29
+ | **Source Framework** | PyTorch (Ultralytics, Hugging Face Transformers) |
30
  | **Supported Precisions** | FP32, FP16, INT8 (mixed-precision) |
31
  | **Inference Engine** | OpenVINO |
32
  | **Hardware** | CPU, GPU, NPU |
 
59
  - **Pedestrian Flow Analysis** -- compare fast throughways against lingering areas.
60
  - **Incident Spotting** -- flag unusually fast or stalled movement.
61
 
62
+ Available variants: `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`.
63
  Smaller variants (`yolo26n`, `yolo26s`) are recommended for high-FPS edge
64
  deployment; larger variants improve recall for small or distant objects.
65
 
66
+ ### Model Selection
67
+
68
+ [DETR](https://huggingface.co/facebook/detr-resnet-50) (DEtection TRansformer) is available as an alternative to YOLO26.
69
+ DETR is a transformer-based, NMS-free detector trained on COCO and released under the Apache-2.0 license.
70
+ The export script embeds DETR normalization, box decoding, and the class remap into the OpenVINO IR, so the DETR model produces the same output layout and class ids as YOLO26.
71
+ The same OpenVINO and DLStreamer samples run either model; select one with the `MODEL` constant at the top of each sample.
72
+
73
+ | Model | Variants | Input Size | Output | License |
74
+ |---|---|---|---|---|
75
+ | YOLO26 (default) | `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l` | 640 x 640 | `[1, 300, 6]` | AGPL-3.0 |
76
+ | DETR | `detr-resnet-50` | 800 x 800 | `[1, 100, 6]` | Apache-2.0 |
77
+
78
+ Choose YOLO26 for maximum throughput on edge devices.
79
+ Choose DETR when a permissively licensed transformer detector is preferred; it is heavier and runs at a lower frame rate.
80
+
81
  ---
82
 
83
  ## Prerequisites
84
 
85
+ Target stack: Intel DLStreamer **2026.3.0 RC1** with the matching OpenVINO GenAI bundle **2026.5.0.0.dev20261030** (OpenVINO Runtime **2026.5.0.dev20261030**).
86
+ Use the [native RC1 setup](../README.md#native-rc1-runtime) before running these samples; this is a development stack, not a stable release.
87
+ See the [device validation matrix](../DEVICE_SUPPORT_MATRIX.md) for measured CPU, GPU, and NPU results.
88
+
89
  - Python 3.11+
90
  - [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
91
+ - [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
92
 
93
  Create and activate a Python virtual environment before running the scripts:
94
 
 
116
 
117
  This exports the default **yolo26n** model in **FP16** precision.
118
 
119
+ #### Optional: Select a Different Model, Variant, or Precision
120
 
121
  ```bash
122
+ ./export_and_quantize.sh yolo26n FP32 # full-precision
123
+ ./export_and_quantize.sh yolo26n INT8 # quantized
124
+ ./export_and_quantize.sh yolo26s # larger variant, default FP16
125
+ ./export_and_quantize.sh detr-resnet-50 # DETR alternative, default FP16
126
+ ./export_and_quantize.sh detr-resnet-50 INT8 # DETR quantized
127
  ```
128
 
129
+ The first argument selects the model: any YOLO26 variant (`yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`) or DETR (`detr-resnet-50`).
130
  The script performs the following steps:
131
 
132
+ 1. Installs dependencies (`openvino`, `ultralytics` for YOLO26 or `transformers`, `torch` for DETR; adds `nncf` for INT8).
133
  2. Downloads a sample test image (`test.jpg`) and a sample test video (`test_video.mp4`).
134
  3. Downloads the PyTorch weights and exports to OpenVINO IR.
135
  4. *(INT8 only)* Quantizes the model using NNCF post-training quantization.
136
 
137
+ Output files (replace `yolo26n` with the selected model, for example `detr-resnet-50`):
138
 
139
  - `yolo26n_openvino_model/` -- FP32 or FP16 OpenVINO IR model directory.
140
  - `yolo26n_heatmap_int8.xml` / `.bin` -- INT8 quantized model *(only when `INT8` is selected)*.
 
155
  It also saves the final speed heatmap as `heatmap.jpg`.
156
  Change the `device` string to run on CPU, GPU, or NPU.
157
 
158
+ The full sample is in [openvino_sample.py](openvino_sample.py); run it with `python3 openvino_sample.py` or copy the code below.
159
+
160
+ <!-- sample: openvino_sample.py -->
161
  ```python
162
+ # SPDX-License-Identifier: MIT
163
+ # Copyright (C) Intel Corporation
164
+ #
165
+ # Heatmap Generation: OpenVINO Runtime sample.
166
+ # See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
167
+
168
  import cv2
169
  import numpy as np
170
  import openvino as ov
171
 
172
  CONF_THRESHOLD = 0.4
173
+ # Select the model: "yolo26n" (default) or "detr-resnet-50".
174
+ MODEL = "yolo26n"
175
+ INPUT_SIZE = 800 if MODEL.startswith("detr") else 640
176
  HEATMAP_ALPHA = 0.55
177
  MATCH_DIST = 80.0 # max px between frames to treat detections as the same object
178
  MAX_SPEED = 20.0 # px/frame that maps to full red
 
195
 
196
 
197
  core = ov.Core()
198
+ model = core.read_model(f"{MODEL}_openvino_model/{MODEL}.xml")
199
 
200
  # Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
201
  compiled = core.compile_model(model, "CPU")
 
263
 
264
  print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)
265
  ```
266
+ <!-- /sample -->
267
 
268
  **Device targets:**
269
 
 
284
 
285
  > **Notes on running this sample:**
286
  >
287
+ > - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml` or `detr-resnet-50_openvino_model/detr-resnet-50.xml`). Class names are
288
  > read automatically from the model's embedded `metadata.yaml` by
289
  > DLStreamer 2026.0+ -- no external `labels-file` is required.
290
  > - Export `PYTHONPATH` so the DLStreamer Python module is importable:
 
296
  > /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
297
  > ```
298
 
299
+ The full sample is in [dlstreamer_sample.py](dlstreamer_sample.py); run it with `python3 dlstreamer_sample.py` or copy the code below.
300
+
301
+ <!-- sample: dlstreamer_sample.py -->
302
  ```python
303
+ # SPDX-License-Identifier: MIT
304
+ # Copyright (C) Intel Corporation
305
+ #
306
+ # Heatmap Generation: DLStreamer pipeline sample.
307
+ # See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
308
+
309
  import gi
310
 
311
  gi.require_version("Gst", "1.0")
 
341
  return overlay.astype(np.uint8)
342
 
343
 
344
+ # Select the model: "yolo26n" (default) or "detr-resnet-50".
345
+ MODEL = "yolo26n"
346
+
347
  # For CPU: change device=GPU to device=CPU.
348
  # For NPU: change device=GPU to device=NPU (batch-size=1, nireq=4 recommended).
349
  pipeline_str = (
350
  f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
351
  "videoconvert ! video/x-raw,format=BGR ! "
352
+ f"gvadetect model={MODEL}_openvino_model/{MODEL}.xml "
353
  "device=GPU "
354
  "threshold=0.4 ! queue ! "
355
  "appsink name=sink emit-signals=false sync=false"
 
428
  writer.release()
429
  print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)
430
  ```
431
+ <!-- /sample -->
432
 
433
  **Device targets:**
434
 
 
449
  ## References
450
 
451
  - [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/)
452
+ - [DETR Model Card (facebook/detr-resnet-50)](https://huggingface.co/facebook/detr-resnet-50)
453
+ - [End-to-End Object Detection with Transformers (DETR paper)](https://arxiv.org/abs/2005.12872)
454
  - [Ultralytics Heatmap Guide](https://docs.ultralytics.com/guides/heatmaps/)
455
  - [OpenCV Color Maps](https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html)
456
  - [OpenVINO Documentation](https://docs.openvino.ai/)
457
+ - [Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/index.html)
config.json CHANGED
@@ -3,8 +3,8 @@
3
  "library_name": "openvino",
4
  "pipeline_tag": "object-detection",
5
  "category": "Object Detection + Speed Heatmap Aggregation",
6
- "base_model": "YOLO26 (Ultralytics)",
7
- "source_framework": "PyTorch (Ultralytics)",
8
  "supported_precisions": [
9
  "FP32",
10
  "FP16",
@@ -18,5 +18,13 @@
18
  ],
19
  "detected_classes": "All 80 COCO classes (heatmap colored by object speed)",
20
  "hf_repo_id": "Intel/heatmap-generation",
21
- "description": "Heatmap generation use case using YOLO26 detection with BoT-SORT tracking to aggregate a speed-colored density heatmap over time"
 
 
 
 
 
 
 
 
22
  }
 
3
  "library_name": "openvino",
4
  "pipeline_tag": "object-detection",
5
  "category": "Object Detection + Speed Heatmap Aggregation",
6
+ "base_model": "YOLO26 (Ultralytics, default) or DETR facebook/detr-resnet-50 (alternative)",
7
+ "source_framework": "PyTorch (Ultralytics, Hugging Face Transformers)",
8
  "supported_precisions": [
9
  "FP32",
10
  "FP16",
 
18
  ],
19
  "detected_classes": "All 80 COCO classes (heatmap colored by object speed)",
20
  "hf_repo_id": "Intel/heatmap-generation",
21
+ "description": "Heatmap generation use case using YOLO26 (default) or DETR detection with BoT-SORT tracking to aggregate a speed-colored density heatmap over time",
22
+ "selectable_models": [
23
+ "yolo26n",
24
+ "yolo26s",
25
+ "yolo26m",
26
+ "yolo26l",
27
+ "detr-resnet-50"
28
+ ],
29
+ "default_model": "yolo26n"
30
  }
dlstreamer_sample.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (C) Intel Corporation
3
+ #
4
+ # Heatmap Generation: DLStreamer pipeline sample.
5
+ # See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
6
+
7
+ import gi
8
+
9
+ gi.require_version("Gst", "1.0")
10
+ gi.require_version("GstAnalytics", "1.0")
11
+ from gi.repository import Gst, GLib, GstAnalytics
12
+
13
+ import numpy as np
14
+
15
+ Gst.init([])
16
+
17
+ # Import cv2 after Gst.init to avoid GStreamer re-initialization conflicts.
18
+ import cv2
19
+
20
+ INPUT_VIDEO = "test_video.mp4"
21
+ HEATMAP_ALPHA = 0.55
22
+ MATCH_DIST = 80.0 # max px between frames to treat detections as the same object
23
+ MAX_SPEED = 20.0 # px/frame that maps to full red
24
+
25
+
26
+ def render_speed_heatmap(frame, speed_sum, count, alpha):
27
+ """Color traffic by average speed: blue = slow, yellow = medium,
28
+ red = fast. Only regions where vehicles were seen are tinted, so
29
+ empty background keeps its original color."""
30
+ avg = np.zeros_like(speed_sum)
31
+ seen = count > 0
32
+ avg[seen] = speed_sum[seen] / count[seen]
33
+ avg = cv2.GaussianBlur(avg, (0, 0), sigmaX=15)
34
+ presence = cv2.GaussianBlur(seen.astype(np.float32), (0, 0), sigmaX=15)
35
+ norm = np.clip(avg / MAX_SPEED, 0, 1) # 0 = slow (blue), 1 = fast (red)
36
+ color = cv2.applyColorMap((norm * 255).astype(np.uint8), cv2.COLORMAP_JET)
37
+ weight = (np.clip(presence, 0, 1) * alpha)[..., np.newaxis]
38
+ overlay = frame.astype(np.float32) * (1 - weight) + color.astype(np.float32) * weight
39
+ return overlay.astype(np.uint8)
40
+
41
+
42
+ # Select the model: "yolo26n" (default) or "detr-resnet-50".
43
+ MODEL = "yolo26n"
44
+
45
+ # For CPU: change device=GPU to device=CPU.
46
+ # For NPU: change device=GPU to device=NPU (batch-size=1, nireq=4 recommended).
47
+ pipeline_str = (
48
+ f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
49
+ "videoconvert ! video/x-raw,format=BGR ! "
50
+ f"gvadetect model={MODEL}_openvino_model/{MODEL}.xml "
51
+ "device=GPU "
52
+ "threshold=0.4 ! queue ! "
53
+ "appsink name=sink emit-signals=false sync=false"
54
+ )
55
+ pipeline = Gst.parse_launch(pipeline_str)
56
+ sink = pipeline.get_by_name("sink")
57
+ pipeline.set_state(Gst.State.PLAYING)
58
+
59
+ speed_sum = None
60
+ count = None
61
+ prev_centroids = []
62
+ writer = None
63
+ frame_idx = 0
64
+ total_dets = 0
65
+
66
+ while True:
67
+ sample = sink.emit("pull-sample")
68
+ if sample is None:
69
+ break
70
+ buf = sample.get_buffer()
71
+ caps = sample.get_caps().get_structure(0)
72
+ width = caps.get_value("width")
73
+ height = caps.get_value("height")
74
+
75
+ if speed_sum is None:
76
+ speed_sum = np.zeros((height, width), dtype=np.float32)
77
+ count = np.zeros((height, width), dtype=np.float32)
78
+
79
+ ok, mapinfo = buf.map(Gst.MapFlags.READ)
80
+ if not ok:
81
+ continue
82
+ frame = np.ndarray((height, width, 3), dtype=np.uint8,
83
+ buffer=mapinfo.data).copy()
84
+ buf.unmap(mapinfo)
85
+ frame_idx += 1
86
+
87
+ rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf)
88
+ cur_centroids = []
89
+ det_count = 0
90
+ if rmeta is not None:
91
+ idx = 1
92
+ while True:
93
+ ok_od, od = rmeta.get_od_mtd(idx)
94
+ if not ok_od:
95
+ break
96
+ _, x, y, w, h, _ = od.get_location()
97
+ x1, y1 = max(0, int(x)), max(0, int(y))
98
+ x2, y2 = min(width, int(x + w)), min(height, int(y + h))
99
+ cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
100
+ cur_centroids.append((cx, cy))
101
+
102
+ # Speed = displacement from the nearest detection last frame.
103
+ speed = 0.0
104
+ if prev_centroids:
105
+ d = min(np.hypot(cx - px, cy - py) for px, py in prev_centroids)
106
+ if d <= MATCH_DIST:
107
+ speed = d
108
+ speed_sum[y1:y2, x1:x2] += speed
109
+ count[y1:y2, x1:x2] += 1.0
110
+ det_count += 1
111
+ idx += 1
112
+ prev_centroids = cur_centroids
113
+ total_dets += det_count
114
+
115
+ overlay = render_speed_heatmap(frame, speed_sum, count, HEATMAP_ALPHA)
116
+
117
+ if writer is None:
118
+ writer = cv2.VideoWriter(
119
+ "output_dlstreamer.mp4", cv2.VideoWriter_fourcc(*"mp4v"),
120
+ 30.0, (width, height))
121
+ writer.write(overlay)
122
+ print(f"Frame {frame_idx}: detections={det_count}", flush=True)
123
+
124
+ pipeline.set_state(Gst.State.NULL)
125
+ if writer:
126
+ writer.release()
127
+ print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)
export_and_quantize.sh CHANGED
@@ -2,9 +2,19 @@
2
  # SPDX-License-Identifier: MIT
3
  # Copyright (C) Intel Corporation
4
  #
5
- # Export a YOLO26 detector to OpenVINO IR for the heatmap-generation use case.
6
  # Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION]
7
  # Example: ./export_and_quantize.sh yolo26n FP16
 
 
 
 
 
 
 
 
 
 
8
 
9
  set -euo pipefail
10
 
@@ -17,12 +27,20 @@ if [[ "${PRECISION}" != "FP32" && "${PRECISION}" != "FP16" && "${PRECISION}" !=
17
  exit 1
18
  fi
19
 
20
- echo "--- Installing dependencies ---"
21
- if [[ "${PRECISION}" == "INT8" ]]; then
22
- pip install -qU openvino nncf ultralytics
23
- else
24
- pip install -qU openvino ultralytics
25
- fi
 
 
 
 
 
 
 
 
26
 
27
  # Ask for approval before downloading models and sample files
28
  echo ""
@@ -47,6 +65,17 @@ else
47
  echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
48
  fi
49
  echo ""
 
 
 
 
 
 
 
 
 
 
 
50
  echo "--- Downloading sample test image ---"
51
  if [[ ! -f test.jpg ]]; then
52
  wget -q -O test.jpg https://ultralytics.com/images/bus.jpg
@@ -72,14 +101,92 @@ else
72
  EXPORT_LABEL="FP16"
73
  fi
74
 
75
- echo "--- Exporting ${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---"
76
- python3 -c "
 
77
  from ultralytics import YOLO
78
 
79
  model = YOLO('${MODEL_NAME}.pt')
80
- model.export(format='openvino', half=${HALF_FLAG}, dynamic=False, imgsz=640)
81
  print('Export complete: ${MODEL_NAME}_openvino_model/')
82
  "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  if [[ "${PRECISION}" == "INT8" ]]; then
85
  echo "--- Quantizing to INT8 with NNCF ---"
@@ -93,7 +200,7 @@ core = ov.Core()
93
  model = core.read_model('${MODEL_NAME}_openvino_model/${MODEL_NAME}.xml')
94
 
95
  img = cv2.imread('test.jpg')
96
- img = cv2.resize(img, (640, 640))
97
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
98
  img = img.transpose(2, 0, 1)[np.newaxis, ...]
99
 
@@ -107,6 +214,7 @@ quantized = nncf.quantize(
107
  calibration_dataset,
108
  preset=nncf.QuantizationPreset.MIXED,
109
  subset_size=300,
 
110
  )
111
 
112
  ov.save_model(quantized, '${MODEL_NAME}_heatmap_int8.xml')
 
2
  # SPDX-License-Identifier: MIT
3
  # Copyright (C) Intel Corporation
4
  #
5
+ # Export a YOLO26 or DETR detector to OpenVINO IR for the heatmap-generation use case.
6
  # Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION]
7
  # Example: ./export_and_quantize.sh yolo26n FP16
8
+ # Example: ./export_and_quantize.sh detr-resnet-50 FP16
9
+ #
10
+ # Supported model variants:
11
+ # yolo26n, yolo26s, yolo26m, yolo26l -- Ultralytics YOLO26 (default)
12
+ # detr-resnet-50 -- Facebook DETR (Hugging Face)
13
+ #
14
+ # DETR is exported end-to-end: ImageNet normalization, softmax,
15
+ # box decoding, and the COCO-91 to COCO-80 class remap are embedded in the
16
+ # graph, so the IR produces the same [1, N, 6] output layout and class ids as
17
+ # YOLO26 and runs with the same OpenVINO and DLStreamer samples.
18
 
19
  set -euo pipefail
20
 
 
27
  exit 1
28
  fi
29
 
30
+ case "${MODEL_NAME}" in
31
+ yolo26n|yolo26s|yolo26m|yolo26l)
32
+ MODEL_FAMILY="yolo"
33
+ INPUT_SIZE=640
34
+ ;;
35
+ detr-resnet-50)
36
+ MODEL_FAMILY="detr"
37
+ INPUT_SIZE=800
38
+ ;;
39
+ *)
40
+ echo "ERROR: unsupported model '${MODEL_NAME}'. Choose yolo26[n|s|m|l] or detr-resnet-50." >&2
41
+ exit 1
42
+ ;;
43
+ esac
44
 
45
  # Ask for approval before downloading models and sample files
46
  echo ""
 
65
  echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
66
  fi
67
  echo ""
68
+ echo "--- Installing dependencies ---"
69
+ if [[ "${MODEL_FAMILY}" == "detr" ]]; then
70
+ DEPS=(openvino transformers torch pyyaml opencv-python)
71
+ else
72
+ DEPS=(openvino ultralytics)
73
+ fi
74
+ if [[ "${PRECISION}" == "INT8" ]]; then
75
+ DEPS+=(nncf)
76
+ fi
77
+ pip install -qU "${DEPS[@]}"
78
+
79
  echo "--- Downloading sample test image ---"
80
  if [[ ! -f test.jpg ]]; then
81
  wget -q -O test.jpg https://ultralytics.com/images/bus.jpg
 
101
  EXPORT_LABEL="FP16"
102
  fi
103
 
104
+ if [[ "${MODEL_FAMILY}" == "yolo" ]]; then
105
+ echo "--- Exporting ${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---"
106
+ python3 -c "
107
  from ultralytics import YOLO
108
 
109
  model = YOLO('${MODEL_NAME}.pt')
110
+ model.export(format='openvino', half=${HALF_FLAG}, dynamic=False, imgsz=${INPUT_SIZE}, nms=False)
111
  print('Export complete: ${MODEL_NAME}_openvino_model/')
112
  "
113
+ else
114
+ echo "--- Exporting facebook/${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---"
115
+ python3 - "${MODEL_NAME}" "${HALF_FLAG}" "${INPUT_SIZE}" <<'PYEXPORT'
116
+ import sys
117
+ from pathlib import Path
118
+
119
+ import openvino as ov
120
+ import torch
121
+ import yaml
122
+ from transformers import DetrForObjectDetection
123
+
124
+ model_name, half_flag, size = sys.argv[1], sys.argv[2] == "True", int(sys.argv[3])
125
+ model_id = f"facebook/{model_name}"
126
+
127
+ # The "no_timm" revision uses the transformers ResNet backbone (no timm dependency).
128
+ detr = DetrForObjectDetection.from_pretrained(model_id, revision="no_timm").eval()
129
+ id2label = detr.config.id2label
130
+ # DETR predicts 91 COCO category ids (with "N/A" gaps) plus a no-object class.
131
+ # Keep the 80 real categories so class ids match YOLO26 (person = 0).
132
+ coco80 = [k for k in sorted(id2label) if id2label[k] != "N/A"]
133
+ assert len(coco80) == 80, f"unexpected label map with {len(coco80)} classes"
134
+
135
+
136
+ class DetrEndToEnd(torch.nn.Module):
137
+ """Input: RGB [0, 1] NCHW. Output: [1, 100, 6] = [x1, y1, x2, y2, conf, class_id] in input pixels."""
138
+
139
+ def __init__(self, model: torch.nn.Module) -> None:
140
+ super().__init__()
141
+ self.model = model
142
+ self.register_buffer("mean", torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1))
143
+ self.register_buffer("std", torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1))
144
+ self.register_buffer("coco80", torch.tensor(coco80))
145
+
146
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
147
+ out = self.model(pixel_values=(x - self.mean) / self.std, return_dict=True)
148
+ probs = out.logits.softmax(-1).index_select(-1, self.coco80)
149
+ conf, cls = probs.max(-1)
150
+ cx, cy, w, h = (out.pred_boxes * size).unbind(-1)
151
+ return torch.stack([cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2, conf, cls.float()], -1)
152
+
153
+
154
+ ov_model = ov.convert_model(
155
+ DetrEndToEnd(detr), example_input=torch.zeros(1, 3, size, size), input=[1, 3, size, size]
156
+ )
157
+ ov_model.outputs[0].get_tensor().set_names({"output0"})
158
+
159
+ names = {i: id2label[k] for i, k in enumerate(coco80)}
160
+ # DLStreamer reads preprocessing and the post-processor type from model_info.
161
+ model_info = {
162
+ "model_type": "YOLO",
163
+ "labels": " ".join(n.replace(" ", "_") for n in names.values()),
164
+ "resize_type": "standard",
165
+ "reverse_input_channels": "YES",
166
+ "scale_values": "255",
167
+ "iou_threshold": "0.7",
168
+ }
169
+ for key, value in model_info.items():
170
+ ov_model.set_rt_info(value, ["model_info", key])
171
+
172
+ out_dir = Path(f"{model_name}_openvino_model")
173
+ out_dir.mkdir(exist_ok=True)
174
+ ov.save_model(ov_model, out_dir / f"{model_name}.xml", compress_to_fp16=half_flag)
175
+ metadata = {
176
+ "description": f"{model_id} exported end-to-end with YOLO26-compatible output",
177
+ "license": "Apache-2.0",
178
+ "task": "detect",
179
+ "batch": 1,
180
+ "imgsz": [size, size],
181
+ "names": names,
182
+ "channels": 3,
183
+ "end2end": True,
184
+ }
185
+ with open(out_dir / "metadata.yaml", "w", encoding="utf-8") as f:
186
+ yaml.safe_dump(metadata, f, sort_keys=False)
187
+ print(f"Export complete: {out_dir}/")
188
+ PYEXPORT
189
+ fi
190
 
191
  if [[ "${PRECISION}" == "INT8" ]]; then
192
  echo "--- Quantizing to INT8 with NNCF ---"
 
200
  model = core.read_model('${MODEL_NAME}_openvino_model/${MODEL_NAME}.xml')
201
 
202
  img = cv2.imread('test.jpg')
203
+ img = cv2.resize(img, (${INPUT_SIZE}, ${INPUT_SIZE}))
204
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
205
  img = img.transpose(2, 0, 1)[np.newaxis, ...]
206
 
 
214
  calibration_dataset,
215
  preset=nncf.QuantizationPreset.MIXED,
216
  subset_size=300,
217
+ model_type=nncf.ModelType.TRANSFORMER if '${MODEL_FAMILY}' == 'detr' else None,
218
  )
219
 
220
  ov.save_model(quantized, '${MODEL_NAME}_heatmap_int8.xml')
openvino_sample.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (C) Intel Corporation
3
+ #
4
+ # Heatmap Generation: OpenVINO Runtime sample.
5
+ # See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
6
+
7
+ import cv2
8
+ import numpy as np
9
+ import openvino as ov
10
+
11
+ CONF_THRESHOLD = 0.4
12
+ # Select the model: "yolo26n" (default) or "detr-resnet-50".
13
+ MODEL = "yolo26n"
14
+ INPUT_SIZE = 800 if MODEL.startswith("detr") else 640
15
+ HEATMAP_ALPHA = 0.55
16
+ MATCH_DIST = 80.0 # max px between frames to treat detections as the same object
17
+ MAX_SPEED = 20.0 # px/frame that maps to full red
18
+
19
+
20
+ def render_speed_heatmap(frame, speed_sum, count, alpha):
21
+ """Color traffic by average speed: blue = slow, yellow = medium,
22
+ red = fast. Only regions where vehicles were seen are tinted, so
23
+ empty background keeps its original color."""
24
+ avg = np.zeros_like(speed_sum)
25
+ seen = count > 0
26
+ avg[seen] = speed_sum[seen] / count[seen]
27
+ avg = cv2.GaussianBlur(avg, (0, 0), sigmaX=15)
28
+ presence = cv2.GaussianBlur(seen.astype(np.float32), (0, 0), sigmaX=15)
29
+ norm = np.clip(avg / MAX_SPEED, 0, 1) # 0 = slow (blue), 1 = fast (red)
30
+ color = cv2.applyColorMap((norm * 255).astype(np.uint8), cv2.COLORMAP_JET)
31
+ weight = (np.clip(presence, 0, 1) * alpha)[..., np.newaxis]
32
+ overlay = frame.astype(np.float32) * (1 - weight) + color.astype(np.float32) * weight
33
+ return overlay.astype(np.uint8), color
34
+
35
+
36
+ core = ov.Core()
37
+ model = core.read_model(f"{MODEL}_openvino_model/{MODEL}.xml")
38
+
39
+ # Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
40
+ compiled = core.compile_model(model, "CPU")
41
+
42
+ cap = cv2.VideoCapture("test_video.mp4")
43
+ fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
44
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
45
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
46
+ writer = cv2.VideoWriter(
47
+ "output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
48
+
49
+ speed_sum = np.zeros((height, width), dtype=np.float32)
50
+ count = np.zeros((height, width), dtype=np.float32)
51
+ prev_centroids = []
52
+ heatmap_color = None
53
+ frame_idx = 0
54
+ total_dets = 0
55
+
56
+ while True:
57
+ ok, frame = cap.read()
58
+ if not ok:
59
+ break
60
+ frame_idx += 1
61
+ h0, w0 = frame.shape[:2]
62
+ sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE
63
+
64
+ blob = cv2.resize(frame, (INPUT_SIZE, INPUT_SIZE))
65
+ blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
66
+ blob = blob.transpose(2, 0, 1)[np.newaxis, ...]
67
+
68
+ output = compiled([blob])[compiled.output(0)][0]
69
+ dets = output[output[:, 4] >= CONF_THRESHOLD]
70
+ total_dets += len(dets)
71
+
72
+ cur_centroids = []
73
+ for det in dets:
74
+ x1, y1 = int(det[0] * sx), int(det[1] * sy)
75
+ x2, y2 = int(det[2] * sx), int(det[3] * sy)
76
+ x1, x2 = max(0, x1), min(width, x2)
77
+ y1, y2 = max(0, y1), min(height, y2)
78
+ cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
79
+ cur_centroids.append((cx, cy))
80
+
81
+ # Speed = displacement from the nearest detection in the previous frame.
82
+ speed = 0.0
83
+ if prev_centroids:
84
+ d = min(np.hypot(cx - px, cy - py) for px, py in prev_centroids)
85
+ if d <= MATCH_DIST:
86
+ speed = d
87
+ speed_sum[y1:y2, x1:x2] += speed
88
+ count[y1:y2, x1:x2] += 1.0
89
+ prev_centroids = cur_centroids
90
+
91
+ overlay, heatmap_color = render_speed_heatmap(frame, speed_sum, count, HEATMAP_ALPHA)
92
+ cv2.putText(overlay, f"Detections: {len(dets)}", (10, 30),
93
+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
94
+ writer.write(overlay)
95
+
96
+ cap.release()
97
+ writer.release()
98
+
99
+ if heatmap_color is not None:
100
+ cv2.imwrite("heatmap.jpg", heatmap_color)
101
+ print("Saved: heatmap.jpg")
102
+
103
+ print(f"Processed {frame_idx} frames, {total_dets} total detections", flush=True)