Update native RC1 samples and supported device documentation
Browse files- README.md +26 -2
- dlstreamer_sample.py +117 -0
- export_and_quantize.sh +19 -19
- openvino_sample.py +103 -0
README.md
CHANGED
|
@@ -80,9 +80,13 @@ Typical Metro deployments include:
|
|
| 80 |
|
| 81 |
## Prerequisites
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
- Python 3.11+
|
| 84 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 85 |
-
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
| 86 |
|
| 87 |
Create and activate a Python virtual environment before running the scripts:
|
| 88 |
|
|
@@ -122,7 +126,16 @@ query. It saves the frame containing the most matching people, drawing a green
|
|
| 122 |
box on every matched person.
|
| 123 |
Change the `device` string to run on CPU, GPU, or NPU.
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
import cv2
|
| 127 |
import numpy as np
|
| 128 |
import openvino as ov
|
|
@@ -221,6 +234,7 @@ print(f"Found {best['count']} person(s) matching [{query_text}]")
|
|
| 221 |
cv2.imwrite("output_openvino.jpg", frame)
|
| 222 |
print("Saved: output_openvino.jpg")
|
| 223 |
```
|
|
|
|
| 224 |
|
| 225 |
**Device targets:**
|
| 226 |
|
|
@@ -259,7 +273,16 @@ Only people whose appearance matches the query are boxed, so the annotated
|
|
| 259 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 260 |
> those tensors before the `appsink` reads them.
|
| 261 |
|
|
|
|
|
|
|
|
|
|
| 262 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
import gi
|
| 264 |
|
| 265 |
gi.require_version("Gst", "1.0")
|
|
@@ -372,6 +395,7 @@ if writer["w"] is not None:
|
|
| 372 |
print(f"Frames with a person matching [{query_text}]: {match_frames}", flush=True)
|
| 373 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 374 |
```
|
|
|
|
| 375 |
|
| 376 |
**Device targets:**
|
| 377 |
|
|
@@ -395,4 +419,4 @@ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
| 395 |
- [person-attributes-recognition-crossroad-0230](https://github.com/openvinotoolkit/open_model_zoo/blob/master/models/intel/person-attributes-recognition-crossroad-0230/README.md)
|
| 396 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 397 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 398 |
-
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
|
|
|
| 80 |
|
| 81 |
## Prerequisites
|
| 82 |
|
| 83 |
+
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**).
|
| 84 |
+
Use the [native RC1 setup](../README.md#native-rc1-runtime) before running these samples; this is a development stack, not a stable release.
|
| 85 |
+
See the [device validation matrix](../DEVICE_SUPPORT_MATRIX.md) for measured CPU, GPU, and NPU results.
|
| 86 |
+
|
| 87 |
- Python 3.11+
|
| 88 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 89 |
+
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
|
| 90 |
|
| 91 |
Create and activate a Python virtual environment before running the scripts:
|
| 92 |
|
|
|
|
| 126 |
box on every matched person.
|
| 127 |
Change the `device` string to run on CPU, GPU, or NPU.
|
| 128 |
|
| 129 |
+
The full sample is in [openvino_sample.py](openvino_sample.py); run it with `python3 openvino_sample.py` or copy the code below.
|
| 130 |
+
|
| 131 |
+
<!-- sample: openvino_sample.py -->
|
| 132 |
```python
|
| 133 |
+
# SPDX-License-Identifier: MIT
|
| 134 |
+
# Copyright (C) Intel Corporation
|
| 135 |
+
#
|
| 136 |
+
# Appearance-Based Search: OpenVINO Runtime sample.
|
| 137 |
+
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
|
| 138 |
+
|
| 139 |
import cv2
|
| 140 |
import numpy as np
|
| 141 |
import openvino as ov
|
|
|
|
| 234 |
cv2.imwrite("output_openvino.jpg", frame)
|
| 235 |
print("Saved: output_openvino.jpg")
|
| 236 |
```
|
| 237 |
+
<!-- /sample -->
|
| 238 |
|
| 239 |
**Device targets:**
|
| 240 |
|
|
|
|
| 273 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 274 |
> those tensors before the `appsink` reads them.
|
| 275 |
|
| 276 |
+
The full sample is in [dlstreamer_sample.py](dlstreamer_sample.py); run it with `python3 dlstreamer_sample.py` or copy the code below.
|
| 277 |
+
|
| 278 |
+
<!-- sample: dlstreamer_sample.py -->
|
| 279 |
```python
|
| 280 |
+
# SPDX-License-Identifier: MIT
|
| 281 |
+
# Copyright (C) Intel Corporation
|
| 282 |
+
#
|
| 283 |
+
# Appearance-Based Search: DLStreamer pipeline sample.
|
| 284 |
+
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
|
| 285 |
+
|
| 286 |
import gi
|
| 287 |
|
| 288 |
gi.require_version("Gst", "1.0")
|
|
|
|
| 395 |
print(f"Frames with a person matching [{query_text}]: {match_frames}", flush=True)
|
| 396 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 397 |
```
|
| 398 |
+
<!-- /sample -->
|
| 399 |
|
| 400 |
**Device targets:**
|
| 401 |
|
|
|
|
| 419 |
- [person-attributes-recognition-crossroad-0230](https://github.com/openvinotoolkit/open_model_zoo/blob/master/models/intel/person-attributes-recognition-crossroad-0230/README.md)
|
| 420 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 421 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 422 |
+
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/index.html)
|
dlstreamer_sample.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Appearance-Based Search: 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 |
+
from gi.repository import Gst
|
| 11 |
+
|
| 12 |
+
Gst.init([])
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import cv2
|
| 16 |
+
from gstgva import VideoFrame
|
| 17 |
+
|
| 18 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 19 |
+
OUTPUT_VIDEO = "output_dlstreamer.mp4"
|
| 20 |
+
DETECTION_MODEL = "intel/person-detection-retail-0013/FP16/person-detection-retail-0013.xml"
|
| 21 |
+
ATTRIBUTES_MODEL = "intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.xml"
|
| 22 |
+
# For CPU: change "GPU" to "CPU". For NPU: change "GPU" to "NPU".
|
| 23 |
+
DEVICE = "GPU"
|
| 24 |
+
DET_THRESHOLD = 0.6
|
| 25 |
+
ATTR_THRESHOLD = 0.5
|
| 26 |
+
|
| 27 |
+
# person-attributes-recognition-crossroad-0230 emits eight binary appearance
|
| 28 |
+
# attributes (output layer "453"), in this order:
|
| 29 |
+
ATTRIBUTE_NAMES = [
|
| 30 |
+
"is_male", "has_bag", "has_backpack", "has_hat",
|
| 31 |
+
"has_longsleeves", "has_longpants", "has_longhair", "has_coat_jacket",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# The appearance being searched for. Set an attribute to True to require it or
|
| 35 |
+
# False to require its absence; omit attributes you do not care about.
|
| 36 |
+
QUERY = {"has_longhair": True}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def person_attributes(video_frame):
|
| 40 |
+
"""Yield ((x, y, w, h), {attribute: score}) for each classified person."""
|
| 41 |
+
for region in video_frame.regions():
|
| 42 |
+
rect = region.rect()
|
| 43 |
+
scores = None
|
| 44 |
+
for tensor in region.tensors():
|
| 45 |
+
if tensor.is_detection():
|
| 46 |
+
continue
|
| 47 |
+
data = np.array(tensor.data(), dtype=np.float32)
|
| 48 |
+
# The attributes vector is the length-8 output ("453"); the model
|
| 49 |
+
# also emits two length-2 colour points that are ignored here.
|
| 50 |
+
if data.size == len(ATTRIBUTE_NAMES):
|
| 51 |
+
scores = {name: float(data[i])
|
| 52 |
+
for i, name in enumerate(ATTRIBUTE_NAMES)}
|
| 53 |
+
if scores is None:
|
| 54 |
+
continue
|
| 55 |
+
yield (int(rect.x), int(rect.y), int(rect.w), int(rect.h)), scores
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def matches_query(scores):
|
| 59 |
+
return all((scores[name] >= ATTR_THRESHOLD) == wanted
|
| 60 |
+
for name, wanted in QUERY.items())
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
query_text = ", ".join(k if v else f"no {k}" for k, v in QUERY.items())
|
| 64 |
+
writer = {"w": None}
|
| 65 |
+
match_frames = 0
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def on_video(sink):
|
| 69 |
+
global match_frames
|
| 70 |
+
sample = sink.emit("pull-sample")
|
| 71 |
+
if sample is None:
|
| 72 |
+
return Gst.FlowReturn.OK
|
| 73 |
+
vf = VideoFrame(sample.get_buffer(), caps=sample.get_caps())
|
| 74 |
+
matches = [(x, y, w, h) for (x, y, w, h), scores in person_attributes(vf)
|
| 75 |
+
if matches_query(scores)]
|
| 76 |
+
|
| 77 |
+
with vf.data() as mat:
|
| 78 |
+
frame = mat.copy()
|
| 79 |
+
|
| 80 |
+
if writer["w"] is None:
|
| 81 |
+
frame_h, frame_w = frame.shape[:2]
|
| 82 |
+
structure = sample.get_caps().get_structure(0)
|
| 83 |
+
ok_fr, fps_n, fps_d = structure.get_fraction("framerate")
|
| 84 |
+
fps = fps_n / fps_d if ok_fr and fps_d else 12
|
| 85 |
+
writer["w"] = cv2.VideoWriter(
|
| 86 |
+
OUTPUT_VIDEO, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 87 |
+
|
| 88 |
+
for x, y, w, h in matches:
|
| 89 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 90 |
+
cv2.putText(frame, f"MATCH: {query_text}", (x, max(15, y - 8)),
|
| 91 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 92 |
+
if matches:
|
| 93 |
+
match_frames += 1
|
| 94 |
+
writer["w"].write(frame)
|
| 95 |
+
return Gst.FlowReturn.OK
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# Convert to BGR before inference so gvaclassify's attribute tensors survive to
|
| 99 |
+
# the appsink (a later format-changing videoconvert would strip them).
|
| 100 |
+
pipeline = Gst.parse_launch(
|
| 101 |
+
f"filesrc location={INPUT_VIDEO} ! decodebin3 ! videoconvert ! "
|
| 102 |
+
f"video/x-raw,format=BGR ! "
|
| 103 |
+
f"gvadetect model={DETECTION_MODEL} device={DEVICE} "
|
| 104 |
+
f"threshold={DET_THRESHOLD} ! queue ! "
|
| 105 |
+
f"gvaclassify model={ATTRIBUTES_MODEL} device={DEVICE} ! queue ! "
|
| 106 |
+
"appsink name=sink emit-signals=true sync=false max-buffers=4 drop=false"
|
| 107 |
+
)
|
| 108 |
+
sink = pipeline.get_by_name("sink")
|
| 109 |
+
sink.connect("new-sample", on_video)
|
| 110 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 111 |
+
pipeline.get_bus().timed_pop_filtered(
|
| 112 |
+
Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR)
|
| 113 |
+
pipeline.set_state(Gst.State.NULL)
|
| 114 |
+
if writer["w"] is not None:
|
| 115 |
+
writer["w"].release()
|
| 116 |
+
print(f"Frames with a person matching [{query_text}]: {match_frames}", flush=True)
|
| 117 |
+
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
export_and_quantize.sh
CHANGED
|
@@ -13,25 +13,6 @@ set -euo pipefail
|
|
| 13 |
# Official Open Model Zoo public model storage (versioned, immutable).
|
| 14 |
OMZ_BASE="https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1"
|
| 15 |
|
| 16 |
-
echo "--- Installing dependencies ---"
|
| 17 |
-
pip install -qU openvino opencv-python numpy
|
| 18 |
-
|
| 19 |
-
# Download both the IR topology (.xml) and weights (.bin) for an OMZ model
|
| 20 |
-
# from the official storage into intel/<model>/<precision>/.
|
| 21 |
-
download_omz_model() {
|
| 22 |
-
local model="$1"
|
| 23 |
-
local precision="$2"
|
| 24 |
-
local dest="intel/${model}/${precision}"
|
| 25 |
-
mkdir -p "${dest}"
|
| 26 |
-
local ext
|
| 27 |
-
for ext in xml bin; do
|
| 28 |
-
if [[ ! -f "${dest}/${model}.${ext}" ]]; then
|
| 29 |
-
wget -q -O "${dest}/${model}.${ext}" \
|
| 30 |
-
"${OMZ_BASE}/${model}/${precision}/${model}.${ext}"
|
| 31 |
-
fi
|
| 32 |
-
done
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
# Ask for approval before downloading models and sample files
|
| 36 |
echo ""
|
| 37 |
echo "This script will download:"
|
|
@@ -55,6 +36,25 @@ else
|
|
| 55 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 56 |
fi
|
| 57 |
echo ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
echo "--- Downloading person-detection-retail-0013 (FP16) ---"
|
| 60 |
download_omz_model person-detection-retail-0013 FP16
|
|
|
|
| 13 |
# Official Open Model Zoo public model storage (versioned, immutable).
|
| 14 |
OMZ_BASE="https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Ask for approval before downloading models and sample files
|
| 17 |
echo ""
|
| 18 |
echo "This script will download:"
|
|
|
|
| 36 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 37 |
fi
|
| 38 |
echo ""
|
| 39 |
+
echo "--- Installing dependencies ---"
|
| 40 |
+
pip install -q openvino opencv-python
|
| 41 |
+
|
| 42 |
+
# Download both the IR topology (.xml) and weights (.bin) for an OMZ model
|
| 43 |
+
# from the official storage into intel/<model>/<precision>/.
|
| 44 |
+
download_omz_model() {
|
| 45 |
+
local model="$1"
|
| 46 |
+
local precision="$2"
|
| 47 |
+
local dest="intel/${model}/${precision}"
|
| 48 |
+
mkdir -p "${dest}"
|
| 49 |
+
local ext
|
| 50 |
+
for ext in xml bin; do
|
| 51 |
+
if [[ ! -f "${dest}/${model}.${ext}" ]]; then
|
| 52 |
+
wget -q -O "${dest}/${model}.${ext}" \
|
| 53 |
+
"${OMZ_BASE}/${model}/${precision}/${model}.${ext}"
|
| 54 |
+
fi
|
| 55 |
+
done
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
|
| 59 |
echo "--- Downloading person-detection-retail-0013 (FP16) ---"
|
| 60 |
download_omz_model person-detection-retail-0013 FP16
|
openvino_sample.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Appearance-Based Search: 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 |
+
DETECTION_MODEL = "intel/person-detection-retail-0013/FP16/person-detection-retail-0013.xml"
|
| 12 |
+
ATTRIBUTES_MODEL = "intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.xml"
|
| 13 |
+
SCENE_VIDEO = "test_video.mp4"
|
| 14 |
+
|
| 15 |
+
# person-attributes-recognition-crossroad-0230 emits eight binary appearance
|
| 16 |
+
# attributes (output layer "453"), in this order:
|
| 17 |
+
ATTRIBUTE_NAMES = [
|
| 18 |
+
"is_male", "has_bag", "has_backpack", "has_hat",
|
| 19 |
+
"has_longsleeves", "has_longpants", "has_longhair", "has_coat_jacket",
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# The appearance being searched for. Set an attribute to True to require it or
|
| 23 |
+
# False to require its absence; omit attributes you do not care about.
|
| 24 |
+
QUERY = {"has_longhair": True}
|
| 25 |
+
|
| 26 |
+
CONF_THRESHOLD = 0.6 # person-detection confidence
|
| 27 |
+
ATTR_THRESHOLD = 0.5 # attribute presence threshold
|
| 28 |
+
|
| 29 |
+
core = ov.Core()
|
| 30 |
+
|
| 31 |
+
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
|
| 32 |
+
det_compiled = core.compile_model(core.read_model(DETECTION_MODEL), "CPU")
|
| 33 |
+
attr_compiled = core.compile_model(core.read_model(ATTRIBUTES_MODEL), "CPU")
|
| 34 |
+
|
| 35 |
+
det_input = det_compiled.input(0)
|
| 36 |
+
det_h, det_w = det_input.shape[2], det_input.shape[3]
|
| 37 |
+
attr_input = attr_compiled.input(0)
|
| 38 |
+
attr_h, attr_w = attr_input.shape[2], attr_input.shape[3]
|
| 39 |
+
attr_output = attr_compiled.output("453")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def detect_persons(img):
|
| 43 |
+
h0, w0 = img.shape[:2]
|
| 44 |
+
blob = cv2.resize(img, (det_w, det_h))
|
| 45 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 46 |
+
dets = det_compiled([blob])[det_compiled.output(0)][0][0]
|
| 47 |
+
persons = []
|
| 48 |
+
for d in dets:
|
| 49 |
+
if float(d[2]) < CONF_THRESHOLD:
|
| 50 |
+
continue
|
| 51 |
+
x1 = max(0, int(d[3] * w0))
|
| 52 |
+
y1 = max(0, int(d[4] * h0))
|
| 53 |
+
x2 = min(w0, int(d[5] * w0))
|
| 54 |
+
y2 = min(h0, int(d[6] * h0))
|
| 55 |
+
if x2 > x1 and y2 > y1:
|
| 56 |
+
persons.append((x1, y1, x2, y2))
|
| 57 |
+
return persons
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_attributes(img, bbox):
|
| 61 |
+
x1, y1, x2, y2 = bbox
|
| 62 |
+
crop = img[y1:y2, x1:x2]
|
| 63 |
+
blob = cv2.resize(crop, (attr_w, attr_h))
|
| 64 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 65 |
+
values = attr_compiled([blob])[attr_output].flatten()
|
| 66 |
+
return {name: float(values[i]) for i, name in enumerate(ATTRIBUTE_NAMES)}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def matches_query(attributes):
|
| 70 |
+
return all((attributes[name] >= ATTR_THRESHOLD) == wanted
|
| 71 |
+
for name, wanted in QUERY.items())
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Scan the scene and keep the frame containing the most people whose appearance
|
| 75 |
+
# matches the query, so the saved image best illustrates the search result.
|
| 76 |
+
cap = cv2.VideoCapture(SCENE_VIDEO)
|
| 77 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 900
|
| 78 |
+
best = {"count": 0, "frame": None, "boxes": []}
|
| 79 |
+
for frame_idx in range(0, total, 15):
|
| 80 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
|
| 81 |
+
ok, frame = cap.read()
|
| 82 |
+
if not ok:
|
| 83 |
+
break
|
| 84 |
+
boxes = [bbox for bbox in detect_persons(frame)
|
| 85 |
+
if matches_query(get_attributes(frame, bbox))]
|
| 86 |
+
if len(boxes) > best["count"]:
|
| 87 |
+
best = {"count": len(boxes), "frame": frame.copy(), "boxes": boxes}
|
| 88 |
+
cap.release()
|
| 89 |
+
|
| 90 |
+
if best["frame"] is None or best["count"] == 0:
|
| 91 |
+
raise SystemExit("No person matching the appearance query was found")
|
| 92 |
+
|
| 93 |
+
# Draw a green box on every person that matches the searched appearance.
|
| 94 |
+
query_text = ", ".join(k if v else f"no {k}" for k, v in QUERY.items())
|
| 95 |
+
frame = best["frame"]
|
| 96 |
+
for x1, y1, x2, y2 in best["boxes"]:
|
| 97 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 98 |
+
cv2.putText(frame, f"MATCH: {query_text}", (x1, max(15, y1 - 8)),
|
| 99 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 100 |
+
print(f"Found {best['count']} person(s) matching [{query_text}]")
|
| 101 |
+
|
| 102 |
+
cv2.imwrite("output_openvino.jpg", frame)
|
| 103 |
+
print("Saved: output_openvino.jpg")
|