| --- |
| license: mit |
| license_link: LICENSE |
| library_name: openvino |
| pipeline_tag: object-detection |
| tags: |
| - openvino |
| - intel |
| - yolo |
| - yolo26 |
| - object-counting |
| - coco |
| - edge-ai |
| - metro |
| - dlstreamer |
| language: |
| - en |
| --- |
| |
| # Object Counting |
|
|
| | Property | Value | |
| |---|---| |
| | **Category** | Object Detection + Counting (80-class COCO) | |
| | **Base Model** | [YOLO26](https://docs.ultralytics.com/models/yolo26/) (Ultralytics) | |
| | **Source Framework** | PyTorch (Ultralytics) | |
| | **Supported Precisions** | FP32, FP16, INT8 (mixed-precision) | |
| | **Inference Engine** | OpenVINO | |
| | **Hardware** | CPU, GPU, NPU | |
| | **Detected Class(es)** | All 80 COCO classes (counted per class) | |
|
|
| --- |
|
|
| ## Overview |
|
|
| Object Counting is a Metro Analytics use case that detects objects and reports |
| how many of each class are present in an image or per video frame. |
| It is built on [YOLO26](https://docs.ultralytics.com/models/yolo26/), a |
| state-of-the-art real-time object detector, quantized to INT8 for efficient |
| inference on Intel hardware. |
| Counting is implemented as a thin aggregation layer on top of the strongest |
| general-purpose detector, which keeps it accurate and reusable across classes. |
| The DLStreamer sample below demonstrates this on a traffic scene sample video, |
| counting **person**, **bicycle**, and **car** detections per frame. |
|
|
| Typical Metro deployments include: |
|
|
| - **Occupancy Counting** -- count people on a platform or in a waiting area. |
| - **Vehicle Counting** -- count cars, buses, and trucks at an intersection. |
| - **Inventory Counting** -- count bags, bottles, or other items in a zone. |
| - **Throughput Metrics** -- aggregate per-frame counts into time series. |
|
|
| Available variants: `yolo26n`, `yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`. |
| Smaller variants (`yolo26n`, `yolo26s`) are recommended for high-FPS edge deployment; larger variants improve recall for small or distant objects. |
|
|
| For line-crossing counts (directional entry/exit), see the |
| [vehicle-entry-exit-logging](../vehicle-entry-exit-logging/) use case. |
|
|
| --- |
|
|
| ## Prerequisites |
|
|
| - Python 3.11+ |
| - [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version) |
| - [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version) |
|
|
| Create and activate a Python virtual environment before running the scripts: |
|
|
| ```bash |
| python3 -m venv .venv --system-site-packages |
| source .venv/bin/activate |
| ``` |
|
|
| > **Note:** The `--system-site-packages` flag is required so the virtual |
| > environment can access the system-installed OpenVINO and DLStreamer Python |
| > packages. |
|
|
| --- |
|
|
| ## Getting Started |
|
|
| ### Download and Quantize Model |
|
|
| Run the provided script to download, export to OpenVINO IR, and optionally quantize: |
|
|
| ```bash |
| chmod +x export_and_quantize.sh |
| ./export_and_quantize.sh |
| ``` |
|
|
| This exports the default **yolo26n** model in **FP16** precision. |
|
|
| #### Optional: Select a Different Variant or Precision |
|
|
| ```bash |
| ./export_and_quantize.sh yolo26n FP32 # full-precision |
| ./export_and_quantize.sh yolo26n INT8 # quantized |
| ./export_and_quantize.sh yolo26s # larger variant, default FP16 |
| ``` |
|
|
| Replace `yolo26n` with any variant (`yolo26s`, `yolo26m`, `yolo26l`, `yolo26x`). |
| The second argument selects the precision (`FP32`, `FP16`, `INT8`); the default is **FP16**. |
|
|
| The script performs the following steps: |
|
|
| 1. Installs dependencies (`openvino`, `ultralytics`; adds `nncf` for INT8). |
| 2. Downloads a sample test image (`test.jpg`) and a sample test video |
| (`test_video.mp4`, the |
| [`person-bicycle-car-detection.mp4`](https://github.com/intel-iot-devkit/sample-videos/blob/master/person-bicycle-car-detection.mp4) |
| street scene from Intel IoT DevKit's sample-videos repository). |
| 3. Downloads the PyTorch weights and exports to OpenVINO IR. |
| 4. *(INT8 only)* Quantizes the model using NNCF post-training quantization. |
|
|
| Output files: |
|
|
| - `yolo26n_openvino_model/` -- FP32 or FP16 OpenVINO IR model directory. |
| - `yolo26n_objcount_int8.xml` / `yolo26n_objcount_int8.bin` -- INT8 quantized model *(only when `INT8` is selected)*. |
|
|
| #### Precision / Device Compatibility |
|
|
| | Precision | CPU | GPU | NPU | |
| |---|---|---|---| |
| | FP32 | Yes | Yes | No | |
| | FP16 | Yes | Yes | Yes | |
| | INT8 | Yes | Yes | Yes | |
|
|
| > **Note:** The INT8 calibration uses the bundled sample image. |
| > For production accuracy, replace it with a representative set of frames from |
| > the target deployment site. |
|
|
| ### OpenVINO Sample |
|
|
| The sample below runs YOLO26 inference, then aggregates detections into a |
| per-class count and a total count for a single image. |
| YOLO26 is end-to-end (NMS-free), so no manual non-maximum suppression is needed. |
| Change the `device` string to run on CPU, GPU, or NPU. |
|
|
| ```python |
| from collections import Counter |
| |
| import cv2 |
| import numpy as np |
| import openvino as ov |
| |
| CONF_THRESHOLD = 0.4 |
| INPUT_SIZE = 640 |
| |
| core = ov.Core() |
| model = core.read_model("yolo26n_openvino_model/yolo26n.xml") |
| |
| # YOLO26 embeds the 80 COCO class names in rt_info -- read them instead of |
| # hardcoding the list. Ultralytics separates multi-word names with |
| # underscores (e.g. "traffic_light"), so restore spaces for display. |
| COCO_NAMES = [ |
| name.replace("_", " ") |
| for name in model.get_rt_info()["model_info"]["labels"].value.split() |
| ] |
| |
| # Change device to "GPU" or "NPU" to run on integrated GPU or NPU. |
| compiled = core.compile_model(model, "CPU") |
| |
| image = cv2.imread("test.jpg") |
| h0, w0 = image.shape[:2] |
| |
| blob = cv2.resize(image, (INPUT_SIZE, INPUT_SIZE)) |
| blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0 |
| blob = blob.transpose(2, 0, 1)[np.newaxis, ...] # NCHW |
| |
| # YOLO26 end-to-end output: [1, 300, 6] = [x1, y1, x2, y2, confidence, class_id] |
| output = compiled([blob])[compiled.output(0)][0] |
| dets = output[output[:, 4] >= CONF_THRESHOLD] |
| |
| sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE |
| counts = Counter(COCO_NAMES[int(d[5])] for d in dets) |
| |
| print(f"Total objects: {len(dets)}") |
| print("Object counts:") |
| for name, n in sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])): |
| print(f" {name}: {n}") |
| |
| colors = np.random.RandomState(42).randint(0, 255, (80, 3)).tolist() |
| for det in dets: |
| x1, y1, x2, y2 = (int(det[0] * sx), int(det[1] * sy), |
| int(det[2] * sx), int(det[3] * sy)) |
| cid = int(det[5]) |
| cv2.rectangle(image, (x1, y1), (x2, y2), colors[cid], 2) |
| cv2.putText(image, COCO_NAMES[cid], (x1, y1 - 5), |
| cv2.FONT_HERSHEY_SIMPLEX, 0.6, colors[cid], 2) |
| |
| summary = ", ".join(f"{n} {name}" for name, n in counts.items()) |
| cv2.putText(image, summary[:60], (10, 30), |
| cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) |
| cv2.imwrite("output_openvino.jpg", image) |
| ``` |
|
|
| **Device targets:** |
|
|
| - `"CPU"` -- default, works on all Intel platforms. |
| - `"GPU"` -- Intel integrated or discrete GPU. |
| - `"NPU"` -- Intel NPU (validate with `benchmark_app -d NPU`). |
|
|
| ### Try It on a Sample Image |
|
|
| The `export_and_quantize.sh` script downloads `test.jpg` automatically. |
| Re-run the OpenVINO sample above. |
| The script reads `test.jpg`, prints the per-class counts to the console, and writes the annotated frame to `output_openvino.jpg`. |
|
|
| Expected console output (representative): |
|
|
| ```text |
| Total objects: 5 |
| Object counts: |
| person: 4 |
| bus: 1 |
| ``` |
|
|
| #### Expected Output |
|
|
|  |
|
|
| ### DLStreamer Sample |
|
|
| The pipeline below runs the FP16 YOLO26 detector on the sample video via |
| `gvadetect`, overlays bounding boxes with `gvawatermark`, saves the annotated |
| result to `output_dlstreamer.mp4`, and prints the per-frame **person**, |
| **bicycle**, and **car** counts by reading the `GstAnalytics` detection |
| metadata. The sample video |
| ([`person-bicycle-car-detection.mp4`](https://github.com/intel-iot-devkit/sample-videos/blob/master/person-bicycle-car-detection.mp4)) |
| is a street scene containing pedestrians, a cyclist, and cars, matching the |
| three classes counted below. |
|
|
| > **Notes on running this sample:** |
| > |
| > - Use the FP16 IR (`yolo26n_openvino_model/yolo26n.xml`). Class names are |
| > read automatically from the model's embedded `metadata.yaml` by |
| > DLStreamer 2026.0+ -- no external `labels-file` is required. |
| > - Export `PYTHONPATH` so the DLStreamer Python module is importable: |
| > |
| > ```bash |
| > source /opt/intel/openvino_2026/setupvars.sh |
| > source /opt/intel/dlstreamer/scripts/setup_dls_env.sh |
| > export PYTHONPATH=/opt/intel/dlstreamer/python:\ |
| > /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-} |
| > ``` |
| |
| ```python |
| from collections import Counter |
| |
| import gi |
| |
| gi.require_version("Gst", "1.0") |
| gi.require_version("GstAnalytics", "1.0") |
| from gi.repository import Gst, GLib, GstAnalytics |
| |
| Gst.init([]) |
| |
| INPUT_VIDEO = "test_video.mp4" |
| |
| # Only these classes are counted; the sample video contains pedestrians, |
| # a cyclist, and cars. |
| CLASSES_OF_INTEREST = {"person", "bicycle", "car"} |
| |
| # For CPU: change device=GPU to device=CPU. |
| # For NPU: change device=GPU to device=NPU (batch-size=1, nireq=4 recommended). |
| pipeline_str = ( |
| f"filesrc location={INPUT_VIDEO} ! decodebin3 ! " |
| "videoconvert ! " |
| "gvadetect model=yolo26n_openvino_model/yolo26n.xml " |
| "device=GPU " |
| "threshold=0.4 ! queue ! " |
| "gvawatermark ! videoconvert ! video/x-raw,format=I420 ! " |
| "openh264enc ! h264parse ! " |
| "mp4mux ! filesink name=sink location=output_dlstreamer.mp4" |
| ) |
| pipeline = Gst.parse_launch(pipeline_str) |
| |
|
|
| def on_buffer(pad, info): |
| buf = info.get_buffer() |
| rmeta = GstAnalytics.buffer_get_analytics_relation_meta(buf) |
| if rmeta is None: |
| return Gst.PadProbeReturn.OK |
| counts = Counter() |
| idx = 1 |
| while True: |
| ok, od = rmeta.get_od_mtd(idx) |
| if not ok: |
| break |
| label = GLib.quark_to_string(od.get_obj_type()) |
| if label in CLASSES_OF_INTEREST: |
| counts[label] += 1 |
| idx += 1 |
| if counts: |
| summary = ", ".join(f"{n} {name}" for name, n in counts.items()) |
| print(f"Object counts: {summary}", flush=True) |
| return Gst.PadProbeReturn.OK |
| |
|
|
| sink = pipeline.get_by_name("sink") |
| sink.get_static_pad("sink").add_probe(Gst.PadProbeType.BUFFER, on_buffer) |
|
|
| pipeline.set_state(Gst.State.PLAYING) |
| bus = pipeline.get_bus() |
| bus.timed_pop_filtered( |
| Gst.CLOCK_TIME_NONE, |
| Gst.MessageType.EOS | Gst.MessageType.ERROR, |
| ) |
| pipeline.set_state(Gst.State.NULL) |
| ``` |
| |
| **Device targets:** |
|
|
| - `device=GPU` -- default in the sample code. |
| - `device=CPU` -- change `device=GPU` to `device=CPU`. |
| - `device=NPU` -- change `device=GPU` to `device=NPU`; use `batch-size=1` and `nireq=4` for best NPU utilization. |
|
|
| #### Expected Output |
|
|
|  |
|
|
| --- |
|
|
| ## License |
|
|
| Licensed under the MIT License. See [LICENSE](LICENSE) for details. |
|
|
| ## References |
|
|
| - [YOLO26 Documentation](https://docs.ultralytics.com/models/yolo26/) |
| - [Ultralytics Object Counting Guide](https://docs.ultralytics.com/guides/object-counting/) |
| - [Intel DLStreamer gvadetect](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/elements/gvadetect.html) |
| - [OpenVINO Documentation](https://docs.openvino.ai/) |
| - [NNCF Post-Training Quantization](https://docs.openvino.ai/latest/nncf_ptq_introduction.html) |
| - [Intel DLStreamer](https://docs.openedgeplatform.intel.com/2026.0/edge-ai-libraries/dlstreamer/index.html) |
|
|