Update native RC1 samples and supported device documentation
Browse files- README.md +26 -2
- dlstreamer_sample.py +121 -0
- export_and_quantize.sh +19 -19
- openvino_sample.py +104 -0
README.md
CHANGED
|
@@ -61,9 +61,13 @@ Typical Metro deployments include:
|
|
| 61 |
|
| 62 |
## Prerequisites
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
- Python 3.11+
|
| 65 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 66 |
-
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
| 67 |
|
| 68 |
Create and activate a Python virtual environment before running the scripts:
|
| 69 |
|
|
@@ -106,7 +110,16 @@ person is seen again, the gallery returns their existing ID. Every face is
|
|
| 106 |
annotated with its `ID <n>`, and the result is saved to `output_openvino.mp4`.
|
| 107 |
Change the `device` string to run on CPU, GPU, or NPU.
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
import cv2
|
| 111 |
import numpy as np
|
| 112 |
import openvino as ov
|
|
@@ -206,6 +219,7 @@ writer.release()
|
|
| 206 |
print(f"Total identities recognized: {len(gallery)}")
|
| 207 |
print("Saved: output_openvino.mp4")
|
| 208 |
```
|
|
|
|
| 209 |
|
| 210 |
**Device targets:**
|
| 211 |
|
|
@@ -243,7 +257,16 @@ Every face is annotated with its `ID <n>` and the result is saved to
|
|
| 243 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 244 |
> those tensors before the `appsink` reads them.
|
| 245 |
|
|
|
|
|
|
|
|
|
|
| 246 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
import gi
|
| 248 |
|
| 249 |
gi.require_version("Gst", "1.0")
|
|
@@ -360,6 +383,7 @@ if writer["w"] is not None:
|
|
| 360 |
print(f"Total identities recognized: {len(gallery)}", flush=True)
|
| 361 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 362 |
```
|
|
|
|
| 363 |
|
| 364 |
**Device targets:**
|
| 365 |
|
|
@@ -383,4 +407,4 @@ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
| 383 |
- [face-reidentification-retail-0095](https://docs.openvino.ai/2024/omz_models_model_face_reidentification_retail_0095.html)
|
| 384 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 385 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 386 |
-
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
|
|
|
| 61 |
|
| 62 |
## Prerequisites
|
| 63 |
|
| 64 |
+
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**).
|
| 65 |
+
Use the [native RC1 setup](../README.md#native-rc1-runtime) before running these samples; this is a development stack, not a stable release.
|
| 66 |
+
See the [device validation matrix](../DEVICE_SUPPORT_MATRIX.md) for measured CPU, GPU, and NPU results.
|
| 67 |
+
|
| 68 |
- Python 3.11+
|
| 69 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 70 |
+
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
|
| 71 |
|
| 72 |
Create and activate a Python virtual environment before running the scripts:
|
| 73 |
|
|
|
|
| 110 |
annotated with its `ID <n>`, and the result is saved to `output_openvino.mp4`.
|
| 111 |
Change the `device` string to run on CPU, GPU, or NPU.
|
| 112 |
|
| 113 |
+
The full sample is in [openvino_sample.py](openvino_sample.py); run it with `python3 openvino_sample.py` or copy the code below.
|
| 114 |
+
|
| 115 |
+
<!-- sample: openvino_sample.py -->
|
| 116 |
```python
|
| 117 |
+
# SPDX-License-Identifier: MIT
|
| 118 |
+
# Copyright (C) Intel Corporation
|
| 119 |
+
#
|
| 120 |
+
# Facial Recognition: OpenVINO Runtime sample.
|
| 121 |
+
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
|
| 122 |
+
|
| 123 |
import cv2
|
| 124 |
import numpy as np
|
| 125 |
import openvino as ov
|
|
|
|
| 219 |
print(f"Total identities recognized: {len(gallery)}")
|
| 220 |
print("Saved: output_openvino.mp4")
|
| 221 |
```
|
| 222 |
+
<!-- /sample -->
|
| 223 |
|
| 224 |
**Device targets:**
|
| 225 |
|
|
|
|
| 257 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 258 |
> those tensors before the `appsink` reads them.
|
| 259 |
|
| 260 |
+
The full sample is in [dlstreamer_sample.py](dlstreamer_sample.py); run it with `python3 dlstreamer_sample.py` or copy the code below.
|
| 261 |
+
|
| 262 |
+
<!-- sample: dlstreamer_sample.py -->
|
| 263 |
```python
|
| 264 |
+
# SPDX-License-Identifier: MIT
|
| 265 |
+
# Copyright (C) Intel Corporation
|
| 266 |
+
#
|
| 267 |
+
# Facial Recognition: DLStreamer pipeline sample.
|
| 268 |
+
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
|
| 269 |
+
|
| 270 |
import gi
|
| 271 |
|
| 272 |
gi.require_version("Gst", "1.0")
|
|
|
|
| 383 |
print(f"Total identities recognized: {len(gallery)}", flush=True)
|
| 384 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 385 |
```
|
| 386 |
+
<!-- /sample -->
|
| 387 |
|
| 388 |
**Device targets:**
|
| 389 |
|
|
|
|
| 407 |
- [face-reidentification-retail-0095](https://docs.openvino.ai/2024/omz_models_model_face_reidentification_retail_0095.html)
|
| 408 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 409 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 410 |
+
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/index.html)
|
dlstreamer_sample.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Facial Recognition: 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/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 21 |
+
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 22 |
+
# For CPU: change "GPU" to "CPU". For NPU: change "GPU" to "NPU".
|
| 23 |
+
DEVICE = "GPU"
|
| 24 |
+
DET_THRESHOLD = 0.6
|
| 25 |
+
MATCH_THRESHOLD = 0.5
|
| 26 |
+
|
| 27 |
+
# Gallery of (numeric_id, embedding). recognize() returns an existing ID for a
|
| 28 |
+
# known face or enrolls a new one, keeping each person's ID stable over time.
|
| 29 |
+
gallery = []
|
| 30 |
+
next_id = 1
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def recognize(embedding):
|
| 34 |
+
global next_id
|
| 35 |
+
best_index, best_sim = -1, 0.0
|
| 36 |
+
for index, (_, gallery_emb) in enumerate(gallery):
|
| 37 |
+
sim = float(np.dot(embedding, gallery_emb))
|
| 38 |
+
if sim > best_sim:
|
| 39 |
+
best_sim, best_index = sim, index
|
| 40 |
+
if best_sim >= MATCH_THRESHOLD:
|
| 41 |
+
person_id, gallery_emb = gallery[best_index]
|
| 42 |
+
# Blend the embedding into the gallery entry to stay robust to pose.
|
| 43 |
+
updated = 0.9 * gallery_emb + 0.1 * embedding
|
| 44 |
+
gallery[best_index] = (person_id, updated / np.linalg.norm(updated))
|
| 45 |
+
return person_id
|
| 46 |
+
person_id = next_id
|
| 47 |
+
next_id += 1
|
| 48 |
+
gallery.append((person_id, embedding))
|
| 49 |
+
print(f"Enrolled ID {person_id}", flush=True)
|
| 50 |
+
return person_id
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def face_embeddings(video_frame):
|
| 54 |
+
"""Yield ((x, y, w, h), normalized_embedding) for each classified face."""
|
| 55 |
+
for region in video_frame.regions():
|
| 56 |
+
rect = region.rect()
|
| 57 |
+
emb = None
|
| 58 |
+
for tensor in region.tensors():
|
| 59 |
+
if tensor.is_detection():
|
| 60 |
+
continue
|
| 61 |
+
data = np.array(tensor.data(), dtype=np.float32)
|
| 62 |
+
if data.size >= 256:
|
| 63 |
+
emb = data[:256]
|
| 64 |
+
if emb is None:
|
| 65 |
+
continue
|
| 66 |
+
emb = emb / (np.linalg.norm(emb) + 1e-9)
|
| 67 |
+
yield (int(rect.x), int(rect.y), int(rect.w), int(rect.h)), emb
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Convert to BGR before inference so gvaclassify's embedding tensors survive to
|
| 71 |
+
# the appsink (a later format-changing videoconvert would strip them).
|
| 72 |
+
pipeline = Gst.parse_launch(
|
| 73 |
+
f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
|
| 74 |
+
"videoconvert ! video/x-raw,format=BGR ! "
|
| 75 |
+
f"gvadetect model={DETECTION_MODEL} device={DEVICE} "
|
| 76 |
+
f"threshold={DET_THRESHOLD} ! queue ! "
|
| 77 |
+
f"gvaclassify model={REID_MODEL} device={DEVICE} ! queue ! "
|
| 78 |
+
"appsink name=sink emit-signals=true sync=false max-buffers=4 drop=false"
|
| 79 |
+
)
|
| 80 |
+
sink = pipeline.get_by_name("sink")
|
| 81 |
+
|
| 82 |
+
writer = {"w": None}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def on_video(sink):
|
| 86 |
+
sample = sink.emit("pull-sample")
|
| 87 |
+
if sample is None:
|
| 88 |
+
return Gst.FlowReturn.OK
|
| 89 |
+
vf = VideoFrame(sample.get_buffer(), caps=sample.get_caps())
|
| 90 |
+
labeled = []
|
| 91 |
+
for (x, y, w, h), emb in face_embeddings(vf):
|
| 92 |
+
labeled.append((x, y, w, h, recognize(emb)))
|
| 93 |
+
|
| 94 |
+
with vf.data() as mat:
|
| 95 |
+
frame = mat.copy()
|
| 96 |
+
|
| 97 |
+
if writer["w"] is None:
|
| 98 |
+
frame_h, frame_w = frame.shape[:2]
|
| 99 |
+
structure = sample.get_caps().get_structure(0)
|
| 100 |
+
ok_fr, fps_n, fps_d = structure.get_fraction("framerate")
|
| 101 |
+
fps = fps_n / fps_d if ok_fr and fps_d else 12
|
| 102 |
+
writer["w"] = cv2.VideoWriter(
|
| 103 |
+
OUTPUT_VIDEO, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 104 |
+
|
| 105 |
+
for x, y, w, h, person_id in labeled:
|
| 106 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 107 |
+
cv2.putText(frame, f"ID {person_id}", (x, max(15, y - 8)),
|
| 108 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 109 |
+
writer["w"].write(frame)
|
| 110 |
+
return Gst.FlowReturn.OK
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
sink.connect("new-sample", on_video)
|
| 114 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 115 |
+
pipeline.get_bus().timed_pop_filtered(
|
| 116 |
+
Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR)
|
| 117 |
+
pipeline.set_state(Gst.State.NULL)
|
| 118 |
+
if writer["w"] is not None:
|
| 119 |
+
writer["w"].release()
|
| 120 |
+
print(f"Total identities recognized: {len(gallery)}", flush=True)
|
| 121 |
+
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
export_and_quantize.sh
CHANGED
|
@@ -11,25 +11,6 @@ set -euo pipefail
|
|
| 11 |
# Official Open Model Zoo public model storage (versioned, immutable).
|
| 12 |
OMZ_BASE="https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1"
|
| 13 |
|
| 14 |
-
echo "--- Installing dependencies ---"
|
| 15 |
-
pip install -qU openvino
|
| 16 |
-
|
| 17 |
-
# Download both the IR topology (.xml) and weights (.bin) for an OMZ model
|
| 18 |
-
# from the official storage into intel/<model>/<precision>/.
|
| 19 |
-
download_omz_model() {
|
| 20 |
-
local model="$1"
|
| 21 |
-
local precision="$2"
|
| 22 |
-
local dest="intel/${model}/${precision}"
|
| 23 |
-
mkdir -p "${dest}"
|
| 24 |
-
local ext
|
| 25 |
-
for ext in xml bin; do
|
| 26 |
-
if [[ ! -f "${dest}/${model}.${ext}" ]]; then
|
| 27 |
-
wget -q -O "${dest}/${model}.${ext}" \
|
| 28 |
-
"${OMZ_BASE}/${model}/${precision}/${model}.${ext}"
|
| 29 |
-
fi
|
| 30 |
-
done
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
# Ask for approval before downloading models and sample files
|
| 34 |
echo ""
|
| 35 |
echo "This script will download:"
|
|
@@ -53,6 +34,25 @@ else
|
|
| 53 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 54 |
fi
|
| 55 |
echo ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
echo "--- Downloading face-detection-adas-0001 (FP16) ---"
|
| 58 |
download_omz_model face-detection-adas-0001 FP16
|
|
|
|
| 11 |
# Official Open Model Zoo public model storage (versioned, immutable).
|
| 12 |
OMZ_BASE="https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Ask for approval before downloading models and sample files
|
| 15 |
echo ""
|
| 16 |
echo "This script will download:"
|
|
|
|
| 34 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 35 |
fi
|
| 36 |
echo ""
|
| 37 |
+
echo "--- Installing dependencies ---"
|
| 38 |
+
pip install -qU openvino
|
| 39 |
+
|
| 40 |
+
# Download both the IR topology (.xml) and weights (.bin) for an OMZ model
|
| 41 |
+
# from the official storage into intel/<model>/<precision>/.
|
| 42 |
+
download_omz_model() {
|
| 43 |
+
local model="$1"
|
| 44 |
+
local precision="$2"
|
| 45 |
+
local dest="intel/${model}/${precision}"
|
| 46 |
+
mkdir -p "${dest}"
|
| 47 |
+
local ext
|
| 48 |
+
for ext in xml bin; do
|
| 49 |
+
if [[ ! -f "${dest}/${model}.${ext}" ]]; then
|
| 50 |
+
wget -q -O "${dest}/${model}.${ext}" \
|
| 51 |
+
"${OMZ_BASE}/${model}/${precision}/${model}.${ext}"
|
| 52 |
+
fi
|
| 53 |
+
done
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
|
| 57 |
echo "--- Downloading face-detection-adas-0001 (FP16) ---"
|
| 58 |
download_omz_model face-detection-adas-0001 FP16
|
openvino_sample.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Facial Recognition: 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/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 12 |
+
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 13 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 14 |
+
CONF_THRESHOLD = 0.6
|
| 15 |
+
MATCH_THRESHOLD = 0.5
|
| 16 |
+
|
| 17 |
+
core = ov.Core()
|
| 18 |
+
|
| 19 |
+
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
|
| 20 |
+
det_model = core.compile_model(core.read_model(DETECTION_MODEL), "CPU")
|
| 21 |
+
reid_model = core.compile_model(core.read_model(REID_MODEL), "CPU")
|
| 22 |
+
|
| 23 |
+
det_input = det_model.input(0)
|
| 24 |
+
det_h, det_w = det_input.shape[2], det_input.shape[3]
|
| 25 |
+
reid_input = reid_model.input(0)
|
| 26 |
+
reid_h, reid_w = reid_input.shape[2], reid_input.shape[3]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def detect_faces(img):
|
| 30 |
+
h0, w0 = img.shape[:2]
|
| 31 |
+
blob = cv2.resize(img, (det_w, det_h))
|
| 32 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 33 |
+
detections = det_model([blob])[det_model.output(0)][0][0]
|
| 34 |
+
boxes = []
|
| 35 |
+
for det in detections:
|
| 36 |
+
if float(det[2]) < CONF_THRESHOLD:
|
| 37 |
+
continue
|
| 38 |
+
x1 = max(0, int(det[3] * w0))
|
| 39 |
+
y1 = max(0, int(det[4] * h0))
|
| 40 |
+
x2 = min(w0, int(det[5] * w0))
|
| 41 |
+
y2 = min(h0, int(det[6] * h0))
|
| 42 |
+
if x2 > x1 and y2 > y1:
|
| 43 |
+
boxes.append((x1, y1, x2, y2))
|
| 44 |
+
return boxes
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_embedding(img, bbox):
|
| 48 |
+
x1, y1, x2, y2 = bbox
|
| 49 |
+
crop = img[y1:y2, x1:x2]
|
| 50 |
+
blob = cv2.resize(crop, (reid_w, reid_h))
|
| 51 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 52 |
+
emb = reid_model([blob])[reid_model.output(0)].flatten()
|
| 53 |
+
return emb / np.linalg.norm(emb)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Gallery of (numeric_id, embedding). recognize() returns an existing ID for a
|
| 57 |
+
# known face or enrolls a new one, keeping each person's ID stable over time.
|
| 58 |
+
gallery = []
|
| 59 |
+
next_id = 1
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def recognize(embedding):
|
| 63 |
+
global next_id
|
| 64 |
+
best_index, best_sim = -1, 0.0
|
| 65 |
+
for index, (_, gallery_emb) in enumerate(gallery):
|
| 66 |
+
sim = float(np.dot(embedding, gallery_emb))
|
| 67 |
+
if sim > best_sim:
|
| 68 |
+
best_sim, best_index = sim, index
|
| 69 |
+
if best_sim >= MATCH_THRESHOLD:
|
| 70 |
+
person_id, gallery_emb = gallery[best_index]
|
| 71 |
+
# Blend the embedding into the gallery entry to stay robust to pose.
|
| 72 |
+
updated = 0.9 * gallery_emb + 0.1 * embedding
|
| 73 |
+
gallery[best_index] = (person_id, updated / np.linalg.norm(updated))
|
| 74 |
+
return person_id
|
| 75 |
+
person_id = next_id
|
| 76 |
+
next_id += 1
|
| 77 |
+
gallery.append((person_id, embedding))
|
| 78 |
+
print(f"Enrolled ID {person_id}")
|
| 79 |
+
return person_id
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
cap = cv2.VideoCapture(INPUT_VIDEO)
|
| 83 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 12
|
| 84 |
+
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 85 |
+
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 86 |
+
writer = cv2.VideoWriter(
|
| 87 |
+
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 88 |
+
|
| 89 |
+
while True:
|
| 90 |
+
ok, frame = cap.read()
|
| 91 |
+
if not ok:
|
| 92 |
+
break
|
| 93 |
+
for bbox in detect_faces(frame):
|
| 94 |
+
person_id = recognize(get_embedding(frame, bbox))
|
| 95 |
+
x1, y1, x2, y2 = bbox
|
| 96 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 97 |
+
cv2.putText(frame, f"ID {person_id}", (x1, max(15, y1 - 8)),
|
| 98 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 99 |
+
writer.write(frame)
|
| 100 |
+
|
| 101 |
+
cap.release()
|
| 102 |
+
writer.release()
|
| 103 |
+
print(f"Total identities recognized: {len(gallery)}")
|
| 104 |
+
print("Saved: output_openvino.mp4")
|