vagheshpatel commited on
Commit
641e004
·
verified ·
1 Parent(s): 60cac40

Sync person-detection from metro-analytics-catalog

Browse files
Files changed (3) hide show
  1. LICENSE +45 -0
  2. README.md +259 -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,259 @@
1
- ---
2
- license: other
3
- license_name: other
4
- license_link: LICENSE
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Person 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 (Person 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** | `person` (COCO class 0) |
14
+
15
+ ---
16
+
17
+ ## Overview
18
+
19
+ Person Detection is a Metro Analytics use case that detects and localizes people 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 the `person` class.
21
+ The quantized INT8 model delivers high throughput on Intel CPUs, GPUs, and NPUs while maintaining strong detection accuracy.
22
+
23
+ Typical Metro deployments include:
24
+
25
+ - **Occupancy Counting** -- count people on platforms, in lobbies, or at transit stops.
26
+ - **Pedestrian Flow Analysis** -- measure directional foot traffic at gates and corridors.
27
+ - **Access Control** -- detect unauthorized presence in restricted zones.
28
+ - **Public Safety** -- support situational awareness in transit hubs and venues.
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 in dense scenes.
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_person_int8.xml` / `yolo26n_person_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 the `person` class, and reports the person count for a single image.
93
+ YOLO26 is end-to-end (NMS-free), so no manual non-maximum suppression is needed.
94
+ Change the `device` string to run on CPU, GPU, or NPU.
95
+
96
+ ```python
97
+ import cv2
98
+ import numpy as np
99
+ import openvino as ov
100
+
101
+ PERSON_CLASS_ID = 0
102
+ CONF_THRESHOLD = 0.4
103
+ INPUT_SIZE = 640
104
+
105
+ core = ov.Core()
106
+ model = core.read_model("yolo26n_openvino_model/yolo26n.xml")
107
+
108
+ # Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
109
+ compiled = core.compile_model(model, "CPU")
110
+
111
+ image = cv2.imread("test.jpg")
112
+ h0, w0 = image.shape[:2]
113
+
114
+ blob = cv2.resize(image, (INPUT_SIZE, INPUT_SIZE))
115
+ blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
116
+ blob = blob.transpose(2, 0, 1)[np.newaxis, ...] # NCHW
117
+
118
+ # YOLO26 end-to-end output: [1, 300, 6] = [x1, y1, x2, y2, confidence, class_id]
119
+ output = compiled([blob])[compiled.output(0)][0]
120
+ mask = (output[:, 4] >= CONF_THRESHOLD) & (output[:, 5].astype(int) == PERSON_CLASS_ID)
121
+ dets = output[mask]
122
+
123
+ sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE
124
+ person_count = len(dets)
125
+ print(f"Detected persons: {person_count}")
126
+
127
+ for det in dets:
128
+ x1 = int(det[0] * sx)
129
+ y1 = int(det[1] * sy)
130
+ x2 = int(det[2] * sx)
131
+ y2 = int(det[3] * sy)
132
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
133
+
134
+ cv2.putText(
135
+ image, f"Persons: {person_count}", (10, 30),
136
+ cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2,
137
+ )
138
+ cv2.imwrite("output.jpg", image)
139
+ ```
140
+
141
+ **Device targets:**
142
+
143
+ - `"CPU"` -- default, works on all Intel platforms.
144
+ - `"GPU"` -- Intel integrated or discrete GPU.
145
+ - `"NPU"` -- Intel NPU (different throughput profile; validate with `benchmark_app -d NPU`).
146
+
147
+ ### Try It on a Sample Image
148
+
149
+ The `export_and_quantize.sh` script downloads `test.jpg` automatically.
150
+ Re-run the OpenVINO sample above.
151
+ The script reads `test.jpg`, prints the person count to the console, and writes the annotated frame to `output.jpg`.
152
+
153
+ Expected console output:
154
+
155
+ ```text
156
+ Detected persons: 4
157
+ ```
158
+
159
+ `output.jpg` shows a green bounding box around each detected person and the text `Persons: 4` in the top-left corner.
160
+
161
+ ### DLStreamer Sample
162
+
163
+ The pipeline below runs the FP16 YOLO26 detector on a single image via
164
+ `gvadetect`, filters detections to the `person` class in a buffer probe using
165
+ the DLStreamer Python bindings (`gstgva.VideoFrame`), overlays bounding boxes,
166
+ and prints the person count.
167
+
168
+ > **Notes on running this sample:**
169
+ >
170
+ > - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml`).
171
+ > On DLStreamer 2026.1, `gvadetect` cannot auto-derive a YOLO post-processor
172
+ > from the INT8 model produced by the bundled script.
173
+ > To use the INT8 model, supply a matching `model-proc` JSON.
174
+ > - Class names are read automatically from the model's embedded
175
+ > `metadata.yaml` by DLStreamer 2026.0+ -- no external `labels-file` is
176
+ > required.
177
+ > - Export `PYTHONPATH` so the DLStreamer Python module is importable:
178
+ >
179
+ > ```bash
180
+ > source /opt/intel/openvino_2026/setupvars.sh
181
+ > source /opt/intel/dlstreamer/scripts/setup_dls_env.sh
182
+ > export PYTHONPATH=/opt/intel/dlstreamer/python:\
183
+ > /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
184
+ > ```
185
+
186
+ **Image-based quick test** (uses `filesrc` with a single JPEG):
187
+
188
+ ```python
189
+ import gi
190
+
191
+ gi.require_version("Gst", "1.0")
192
+ gi.require_version("GstVideo", "1.0")
193
+ from gi.repository import Gst
194
+ from gstgva import VideoFrame
195
+
196
+ Gst.init(None)
197
+
198
+ # For GPU: change device=CPU to device=GPU and add vapostproc after decodebin.
199
+ # For NPU: change device=CPU to device=NPU (batch-size=1 recommended).
200
+ pipeline_str = (
201
+ "filesrc location=test.jpg ! jpegdec ! videoconvert ! "
202
+ "video/x-raw,format=BGR ! "
203
+ "gvadetect model=yolo26n_openvino_model/yolo26n.xml "
204
+ "device=CPU threshold=0.4 ! queue ! "
205
+ "gvawatermark ! videoconvert ! jpegenc ! filesink name=sink location=output.jpg"
206
+ )
207
+ pipeline = Gst.parse_launch(pipeline_str)
208
+
209
+
210
+ def on_buffer(pad, info):
211
+ buf = info.get_buffer()
212
+ caps = pad.get_current_caps()
213
+ frame = VideoFrame(buf, caps=caps)
214
+ person_count = sum(1 for r in frame.regions() if r.label() == "person")
215
+ print(f"Person count: {person_count}", flush=True)
216
+ return Gst.PadProbeReturn.OK
217
+
218
+
219
+ # Attach probe to the src pad of gvawatermark for person counting.
220
+ it = pipeline.iterate_elements()
221
+ while True:
222
+ ok, elem = it.next()
223
+ if not ok:
224
+ break
225
+ if elem.get_factory() and elem.get_factory().get_name() == "gvawatermark":
226
+ pad = elem.get_static_pad("src")
227
+ pad.add_probe(Gst.PadProbeType.BUFFER, on_buffer)
228
+ break
229
+
230
+ pipeline.set_state(Gst.State.PLAYING)
231
+ bus = pipeline.get_bus()
232
+ bus.timed_pop_filtered(
233
+ Gst.CLOCK_TIME_NONE,
234
+ Gst.MessageType.EOS | Gst.MessageType.ERROR,
235
+ )
236
+ pipeline.set_state(Gst.State.NULL)
237
+ ```
238
+
239
+ **Device targets:**
240
+
241
+ - `device=CPU` -- default in the sample code.
242
+ - `device=GPU` -- add `vapostproc` after `decodebin` for zero-copy color conversion.
243
+ - `device=NPU` -- use `batch-size=1` and `nireq=4` for best NPU utilization.
244
+
245
+ ---
246
+
247
+ ## License
248
+
249
+ Copyright (C) Intel Corporation. All rights reserved.
250
+ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
251
+
252
+ ## References
253
+
254
+ - [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/)
255
+ - [OpenVINO YOLO26 Notebook](https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/yolov26-optimization/yolov26-object-detection.ipynb)
256
+ - [COCO Dataset](https://cocodataset.org/)
257
+ - [OpenVINO Documentation](https://docs.openvino.ai/)
258
+ - [NNCF Post-Training Quantization](https://docs.openvino.ai/latest/nncf_ptq_introduction.html)
259
+ - [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 person 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}_person_int8.xml')
87
+ print('Quantization complete: ${MODEL_NAME}_person_int8.xml')
88
+ "
89
+ fi
90
+ echo "--- Done ---"