vagheshpatel commited on
Commit
248e10c
·
verified ·
1 Parent(s): 90a2359

Sync vehicle-detection from metro-analytics-catalog

Browse files
Files changed (3) hide show
  1. LICENSE +45 -0
  2. README.md +265 -5
  3. export_and_quantize.sh +90 -0
LICENSE CHANGED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.
README.md CHANGED
@@ -1,5 +1,265 @@
1
- ---
2
- license: other
3
- license_name: other
4
- license_link: LICENSE
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Vehicle Detection
2
+
3
+ > **Validated with:** OpenVINO 2026.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 (Vehicle Detection) |
8
+ | **Base Model** | [YOLO26](https://docs.ultralytics.com/models/yolo26/) (Ultralytics) |
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)** | `car` (2), `motorcycle` (3), `bus` (5), `truck` (7) |
14
+
15
+ ---
16
+
17
+ ## Overview
18
+
19
+ Vehicle Detection is a Metro Analytics use case that detects and localizes vehicles in images and video streams.
20
+ It is built on [YOLO26](https://docs.ultralytics.com/models/yolo26/), a state-of-the-art real-time object detector trained on the COCO dataset, quantized to INT8 and filtered at runtime to vehicle-related classes: `car`, `motorcycle`, `bus`, and `truck`.
21
+
22
+ Typical Metro deployments include:
23
+
24
+ - **Traffic Monitoring** -- count vehicles on roads, intersections, and highway ramps.
25
+ - **Parking Lot Occupancy** -- detect available spaces in parking structures.
26
+ - **Toll Gate Analytics** -- classify vehicle types at toll collection points.
27
+ - **Fleet Tracking** -- monitor bus and truck movements at depots and terminals.
28
+ - **Incident Detection** -- flag stopped or wrong-way vehicles on roadways.
29
+
30
+ Available variants: `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`.
31
+ Smaller variants (`yolo26n`, `yolo26s`) are recommended for high-FPS edge deployment; larger variants improve recall for distant or partially occluded vehicles.
32
+
33
+ ---
34
+
35
+ ## Prerequisites
36
+
37
+ - Python 3.11+
38
+ - [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
39
+ - [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
40
+
41
+ Create and activate a Python virtual environment before running the scripts:
42
+
43
+ ```bash
44
+ python3 -m venv .venv --system-site-packages
45
+ source .venv/bin/activate
46
+ ```
47
+
48
+ ---
49
+
50
+ ## Getting Started
51
+
52
+ ### Download and Quantize Model
53
+
54
+ Run the provided script to download, export to OpenVINO IR, and optionally quantize:
55
+
56
+ ```bash
57
+ chmod +x export_and_quantize.sh
58
+ ./export_and_quantize.sh yolo26n # default: FP16
59
+ ./export_and_quantize.sh yolo26n FP32 # full-precision
60
+ ./export_and_quantize.sh yolo26n INT8 # quantized
61
+ ```
62
+
63
+ Replace `yolo26n` with any variant (`yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`).
64
+ The second argument selects the precision (`FP32`, `FP16`, `INT8`); the default is **FP16**.
65
+
66
+ The script performs the following steps:
67
+
68
+ 1. Installs dependencies (`openvino`, `ultralytics`; adds `nncf` for INT8).
69
+ 2. Downloads a sample test image (`test.jpg`).
70
+ 3. Downloads the PyTorch weights and exports to OpenVINO IR.
71
+ 4. *(INT8 only)* Quantizes the model using NNCF post-training quantization.
72
+
73
+ Output files:
74
+
75
+ - `yolo26n_openvino_model/` -- FP32 or FP16 OpenVINO IR model directory.
76
+ - `yolo26n_vehicle_int8.xml` / `yolo26n_vehicle_int8.bin` -- INT8 quantized model *(only when `INT8` is selected)*.
77
+
78
+ #### Precision / Device Compatibility
79
+
80
+ | Precision | CPU | GPU | NPU |
81
+ |---|---|---|---|
82
+ | FP32 | Yes | Yes | No |
83
+ | FP16 | Yes | Yes | Yes |
84
+ | INT8 | Yes | Yes | Yes |
85
+
86
+ > **Note:** For production accuracy, replace the random calibration tensors in
87
+ > `export_and_quantize.sh` with a representative sample of frames from the
88
+ > target deployment site.
89
+
90
+ ### OpenVINO Sample
91
+
92
+ The sample below runs YOLO26 inference, filters to vehicle classes (`car`,
93
+ `motorcycle`, `bus`, `truck`), and reports the vehicle count for a single image.
94
+ YOLO26 is end-to-end (NMS-free), so no manual non-maximum suppression is needed.
95
+ Change the `device` string to run on CPU, GPU, or NPU.
96
+
97
+ ```python
98
+ import cv2
99
+ import numpy as np
100
+ import openvino as ov
101
+
102
+ VEHICLE_CLASS_IDS = {2: "car", 3: "motorcycle", 5: "bus", 7: "truck"}
103
+ CONF_THRESHOLD = 0.4
104
+ INPUT_SIZE = 640
105
+
106
+ core = ov.Core()
107
+ model = core.read_model("yolo26n_openvino_model/yolo26n.xml")
108
+
109
+ # Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
110
+ compiled = core.compile_model(model, "CPU")
111
+
112
+ image = cv2.imread("test.jpg")
113
+ h0, w0 = image.shape[:2]
114
+
115
+ blob = cv2.resize(image, (INPUT_SIZE, INPUT_SIZE))
116
+ blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
117
+ blob = blob.transpose(2, 0, 1)[np.newaxis, ...] # NCHW
118
+
119
+ # YOLO26 end-to-end output: [1, 300, 6] = [x1, y1, x2, y2, confidence, class_id]
120
+ output = compiled([blob])[compiled.output(0)][0]
121
+ mask = (output[:, 4] >= CONF_THRESHOLD) & np.isin(output[:, 5].astype(int), list(VEHICLE_CLASS_IDS.keys()))
122
+ dets = output[mask]
123
+
124
+ sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE
125
+ vehicle_count = len(dets)
126
+ print(f"Detected vehicles: {vehicle_count}")
127
+
128
+ colors = {"car": (0, 255, 0), "motorcycle": (255, 0, 0),
129
+ "bus": (0, 165, 255), "truck": (0, 0, 255)}
130
+ for det in dets:
131
+ x1 = int(det[0] * sx)
132
+ y1 = int(det[1] * sy)
133
+ x2 = int(det[2] * sx)
134
+ y2 = int(det[3] * sy)
135
+ cid = int(det[5])
136
+ conf = float(det[4])
137
+ name = VEHICLE_CLASS_IDS[cid]
138
+ label = f"{name} {conf:.2f}"
139
+ color = colors[name]
140
+ cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
141
+ cv2.putText(image, label, (x1, y1 - 5),
142
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
143
+ print(f" {label} at ({x1},{y1})-({x2},{y2})")
144
+
145
+ cv2.imwrite("output.jpg", image)
146
+ ```
147
+
148
+ **Device targets:**
149
+
150
+ - `"CPU"` -- default, works on all Intel platforms.
151
+ - `"GPU"` -- Intel integrated or discrete GPU.
152
+ - `"NPU"` -- Intel NPU (validate with `benchmark_app -d NPU`).
153
+
154
+ ### Try It on a Sample Image
155
+
156
+ The `export_and_quantize.sh` script downloads `test.jpg` automatically.
157
+ Re-run the OpenVINO sample above.
158
+ The script reads `test.jpg`, prints each detected vehicle to the console, and writes the annotated frame to `output.jpg`.
159
+
160
+ Expected console output:
161
+
162
+ ```text
163
+ Detected vehicles: 1
164
+ bus 0.92 at (0,229)-(804,744)
165
+ ```
166
+
167
+ ### DLStreamer Sample
168
+
169
+ The pipeline below runs the FP16 YOLO26 detector on a single image via
170
+ `gvadetect`, filters detections to vehicle classes in a buffer probe using
171
+ the DLStreamer Python bindings (`gstgva.VideoFrame`), overlays bounding boxes,
172
+ and prints the vehicle count.
173
+
174
+ > **Notes on running this sample:**
175
+ >
176
+ > - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml`). Class names are
177
+ > read automatically from the model's embedded `metadata.yaml` by
178
+ > DLStreamer 2026.0+ -- no external `labels-file` is required.
179
+ > - Export `PYTHONPATH` so the DLStreamer Python module is importable:
180
+ >
181
+ > ```bash
182
+ > source /opt/intel/openvino_2026/setupvars.sh
183
+ > source /opt/intel/dlstreamer/scripts/setup_dls_env.sh
184
+ > export PYTHONPATH=/opt/intel/dlstreamer/python:\
185
+ > /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
186
+ > ```
187
+
188
+ **Image-based quick test** (uses `filesrc` with a single JPEG):
189
+
190
+ ```python
191
+ import gi
192
+
193
+ gi.require_version("Gst", "1.0")
194
+ gi.require_version("GstVideo", "1.0")
195
+ from gi.repository import Gst
196
+ from gstgva import VideoFrame
197
+
198
+ Gst.init(None)
199
+
200
+ VEHICLE_LABELS = {"car", "motorcycle", "bus", "truck"}
201
+
202
+ # For GPU: change device=CPU to device=GPU and add vapostproc after decodebin.
203
+ # For NPU: change device=CPU to device=NPU (batch-size=1 recommended).
204
+ pipeline_str = (
205
+ "filesrc location=test.jpg ! jpegdec ! videoconvert ! "
206
+ "video/x-raw,format=BGR ! "
207
+ "gvadetect model=yolo26n_openvino_model/yolo26n.xml "
208
+ "device=CPU threshold=0.4 ! queue ! "
209
+ "gvawatermark ! videoconvert ! jpegenc ! filesink name=sink location=output.jpg"
210
+ )
211
+ pipeline = Gst.parse_launch(pipeline_str)
212
+
213
+
214
+ def on_buffer(pad, info):
215
+ buf = info.get_buffer()
216
+ caps = pad.get_current_caps()
217
+ frame = VideoFrame(buf, caps=caps)
218
+ vehicles = [r for r in frame.regions() if r.label() in VEHICLE_LABELS]
219
+ if vehicles:
220
+ print(f"Vehicle count: {len(vehicles)}", flush=True)
221
+ for v in vehicles:
222
+ print(f" {v.label()} at ({v.rect().x},{v.rect().y})", flush=True)
223
+ return Gst.PadProbeReturn.OK
224
+
225
+
226
+ it = pipeline.iterate_elements()
227
+ while True:
228
+ ok, elem = it.next()
229
+ if not ok:
230
+ break
231
+ if elem.get_factory() and elem.get_factory().get_name() == "gvawatermark":
232
+ pad = elem.get_static_pad("src")
233
+ pad.add_probe(Gst.PadProbeType.BUFFER, on_buffer)
234
+ break
235
+
236
+ pipeline.set_state(Gst.State.PLAYING)
237
+ bus = pipeline.get_bus()
238
+ bus.timed_pop_filtered(
239
+ Gst.CLOCK_TIME_NONE,
240
+ Gst.MessageType.EOS | Gst.MessageType.ERROR,
241
+ )
242
+ pipeline.set_state(Gst.State.NULL)
243
+ ```
244
+
245
+ **Device targets:**
246
+
247
+ - `device=CPU` -- default in the sample code.
248
+ - `device=GPU` -- add `vapostproc` after `decodebin` for zero-copy color conversion.
249
+ - `device=NPU` -- use `batch-size=1` and `nireq=4` for best NPU utilization.
250
+
251
+ ---
252
+
253
+ ## License
254
+
255
+ Copyright (C) Intel Corporation. All rights reserved.
256
+ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
257
+
258
+ ## References
259
+
260
+ - [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/)
261
+ - [OpenVINO YOLO26 Notebook](https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/yolov26-optimization/yolov26-object-detection.ipynb)
262
+ - [COCO Dataset](https://cocodataset.org/)
263
+ - [OpenVINO Documentation](https://docs.openvino.ai/)
264
+ - [NNCF Post-Training Quantization](https://docs.openvino.ai/latest/nncf_ptq_introduction.html)
265
+ - [Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/index.html)
export_and_quantize.sh ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # SPDX-License-Identifier: MIT
3
+ # Copyright (C) Intel Corporation
4
+ #
5
+ # Export a YOLO26 vehicle detector to OpenVINO IR.
6
+ # Usage: ./export_and_quantize.sh [MODEL_VARIANT] [PRECISION]
7
+ # Example: ./export_and_quantize.sh yolo26n FP16
8
+ #
9
+ # Supported precisions:
10
+ # FP32 -- Full-precision floating-point weights
11
+ # FP16 -- Half-precision floating-point weights (default)
12
+ # INT8 -- Quantized 8-bit integer weights (requires NNCF)
13
+ #
14
+ # Precision / device compatibility:
15
+ # | Precision | CPU | GPU | NPU |
16
+ # |-----------|-----|-----|-----|
17
+ # | FP32 | Yes | Yes | No |
18
+ # | FP16 | Yes | Yes | Yes |
19
+ # | INT8 | Yes | Yes | Yes |
20
+
21
+ set -euo pipefail
22
+
23
+ MODEL_NAME="${1:-yolo26n}"
24
+ PRECISION="${2:-FP16}"
25
+ PRECISION="$(echo "${PRECISION}" | tr '[:lower:]' '[:upper:]')"
26
+
27
+ if [[ "${PRECISION}" != "FP32" && "${PRECISION}" != "FP16" && "${PRECISION}" != "INT8" ]]; then
28
+ echo "ERROR: unsupported precision '${PRECISION}'. Choose FP32, FP16, or INT8." >&2
29
+ exit 1
30
+ fi
31
+
32
+ echo "--- Installing dependencies ---"
33
+ if [[ "${PRECISION}" == "INT8" ]]; then
34
+ pip install -qU "openvino>=2026.0.0" "nncf>=3.0.0" ultralytics
35
+ else
36
+ pip install -qU "openvino>=2026.0.0" ultralytics
37
+ fi
38
+
39
+ echo "--- Downloading sample test image ---"
40
+ if [[ ! -f test.jpg ]]; then
41
+ wget -q -O test.jpg https://ultralytics.com/images/bus.jpg
42
+ echo "Downloaded: test.jpg"
43
+ else
44
+ echo "Already present: test.jpg"
45
+ fi
46
+
47
+ if [[ "${PRECISION}" == "FP32" ]]; then
48
+ HALF_FLAG="False"
49
+ EXPORT_LABEL="FP32"
50
+ else
51
+ HALF_FLAG="True"
52
+ EXPORT_LABEL="FP16"
53
+ fi
54
+
55
+ echo "--- Exporting ${MODEL_NAME} to OpenVINO IR (${EXPORT_LABEL}) ---"
56
+ python3 -c "
57
+ from ultralytics import YOLO
58
+
59
+ model = YOLO('${MODEL_NAME}.pt')
60
+ model.export(format='openvino', half=${HALF_FLAG}, dynamic=False, imgsz=640)
61
+ print('Export complete: ${MODEL_NAME}_openvino_model/')
62
+ "
63
+
64
+ if [[ "${PRECISION}" == "INT8" ]]; then
65
+ echo "--- Quantizing to INT8 with NNCF ---"
66
+ python3 -c "
67
+ import nncf
68
+ import openvino as ov
69
+ import numpy as np
70
+
71
+ core = ov.Core()
72
+ model = core.read_model('${MODEL_NAME}_openvino_model/${MODEL_NAME}.xml')
73
+
74
+ def transform_fn(data_item):
75
+ return np.random.rand(1, 3, 640, 640).astype(np.float32)
76
+
77
+ calibration_dataset = nncf.Dataset(list(range(300)), transform_fn)
78
+
79
+ quantized = nncf.quantize(
80
+ model,
81
+ calibration_dataset,
82
+ preset=nncf.QuantizationPreset.MIXED,
83
+ subset_size=300,
84
+ )
85
+
86
+ ov.save_model(quantized, '${MODEL_NAME}_vehicle_int8.xml')
87
+ print('Quantization complete: ${MODEL_NAME}_vehicle_int8.xml')
88
+ "
89
+ fi
90
+ echo "--- Done ---"