Update native RC1 samples and supported device documentation
Browse files- README.md +26 -2
- dlstreamer_sample.py +127 -0
- export_and_quantize.sh +19 -19
- openvino_sample.py +93 -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 |
|
|
@@ -103,7 +107,16 @@ contains several people. It detects every face in the frame, embeds each one, an
|
|
| 103 |
draws a green box only on the face whose similarity to the reference is highest
|
| 104 |
and above the match threshold. Change the `device` string to run on CPU, GPU, or NPU.
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
import cv2
|
| 108 |
import numpy as np
|
| 109 |
import openvino as ov
|
|
@@ -192,6 +205,7 @@ else:
|
|
| 192 |
cv2.imwrite("output_openvino.jpg", frame)
|
| 193 |
print("Saved: output_openvino.jpg")
|
| 194 |
```
|
|
|
|
| 195 |
|
| 196 |
**Device targets:**
|
| 197 |
|
|
@@ -229,7 +243,16 @@ subject even when other people are present.
|
|
| 229 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 230 |
> those tensors before the `appsink` reads them.
|
| 231 |
|
|
|
|
|
|
|
|
|
|
| 232 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
import gi
|
| 234 |
|
| 235 |
gi.require_version("Gst", "1.0")
|
|
@@ -352,6 +375,7 @@ if writer["w"] is not None:
|
|
| 352 |
print(f"Frames with a matched face: {match_frames}", flush=True)
|
| 353 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 354 |
```
|
|
|
|
| 355 |
|
| 356 |
**Device targets:**
|
| 357 |
|
|
@@ -375,4 +399,4 @@ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
| 375 |
- [face-reidentification-retail-0095](https://docs.openvino.ai/2024/omz_models_model_face_reidentification_retail_0095.html)
|
| 376 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 377 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 378 |
-
- [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 |
|
|
|
|
| 107 |
draws a green box only on the face whose similarity to the reference is highest
|
| 108 |
and above the match threshold. Change the `device` string to run on CPU, GPU, or NPU.
|
| 109 |
|
| 110 |
+
The full sample is in [openvino_sample.py](openvino_sample.py); run it with `python3 openvino_sample.py` or copy the code below.
|
| 111 |
+
|
| 112 |
+
<!-- sample: openvino_sample.py -->
|
| 113 |
```python
|
| 114 |
+
# SPDX-License-Identifier: MIT
|
| 115 |
+
# Copyright (C) Intel Corporation
|
| 116 |
+
#
|
| 117 |
+
# Face Matching: OpenVINO Runtime sample.
|
| 118 |
+
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
|
| 119 |
+
|
| 120 |
import cv2
|
| 121 |
import numpy as np
|
| 122 |
import openvino as ov
|
|
|
|
| 205 |
cv2.imwrite("output_openvino.jpg", frame)
|
| 206 |
print("Saved: output_openvino.jpg")
|
| 207 |
```
|
| 208 |
+
<!-- /sample -->
|
| 209 |
|
| 210 |
**Device targets:**
|
| 211 |
|
|
|
|
| 243 |
> `gvadetect`/`gvaclassify` so a downstream format conversion does not strip
|
| 244 |
> those tensors before the `appsink` reads them.
|
| 245 |
|
| 246 |
+
The full sample is in [dlstreamer_sample.py](dlstreamer_sample.py); run it with `python3 dlstreamer_sample.py` or copy the code below.
|
| 247 |
+
|
| 248 |
+
<!-- sample: dlstreamer_sample.py -->
|
| 249 |
```python
|
| 250 |
+
# SPDX-License-Identifier: MIT
|
| 251 |
+
# Copyright (C) Intel Corporation
|
| 252 |
+
#
|
| 253 |
+
# Face Matching: DLStreamer pipeline sample.
|
| 254 |
+
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
|
| 255 |
+
|
| 256 |
import gi
|
| 257 |
|
| 258 |
gi.require_version("Gst", "1.0")
|
|
|
|
| 375 |
print(f"Frames with a matched face: {match_frames}", flush=True)
|
| 376 |
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 377 |
```
|
| 378 |
+
<!-- /sample -->
|
| 379 |
|
| 380 |
**Device targets:**
|
| 381 |
|
|
|
|
| 399 |
- [face-reidentification-retail-0095](https://docs.openvino.ai/2024/omz_models_model_face_reidentification_retail_0095.html)
|
| 400 |
- [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)
|
| 401 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 402 |
+
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/index.html)
|
dlstreamer_sample.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Face Matching: 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 |
+
REFERENCE_IMAGE = "face_a.jpg"
|
| 20 |
+
OUTPUT_VIDEO = "output_dlstreamer.mp4"
|
| 21 |
+
DETECTION_MODEL = "intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 22 |
+
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 23 |
+
# For CPU: change "GPU" to "CPU". For NPU: change "GPU" to "NPU".
|
| 24 |
+
DEVICE = "GPU"
|
| 25 |
+
DET_THRESHOLD = 0.6
|
| 26 |
+
MATCH_THRESHOLD = 0.4
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def face_embeddings(video_frame):
|
| 30 |
+
"""Yield ((x, y, w, h), normalized_embedding) for each classified face."""
|
| 31 |
+
for region in video_frame.regions():
|
| 32 |
+
rect = region.rect()
|
| 33 |
+
emb = None
|
| 34 |
+
for tensor in region.tensors():
|
| 35 |
+
if tensor.is_detection():
|
| 36 |
+
continue
|
| 37 |
+
data = np.array(tensor.data(), dtype=np.float32)
|
| 38 |
+
if data.size >= 256:
|
| 39 |
+
emb = data[:256]
|
| 40 |
+
if emb is None:
|
| 41 |
+
continue
|
| 42 |
+
emb = emb / (np.linalg.norm(emb) + 1e-9)
|
| 43 |
+
yield (int(rect.x), int(rect.y), int(rect.w), int(rect.h)), emb
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def run_pipeline(source_desc, on_frame):
|
| 47 |
+
# Convert to BGR before inference so gvaclassify's embedding tensors survive
|
| 48 |
+
# to the appsink (a later format-changing videoconvert would strip them).
|
| 49 |
+
pipeline = Gst.parse_launch(
|
| 50 |
+
f"{source_desc} ! videoconvert ! video/x-raw,format=BGR ! "
|
| 51 |
+
f"gvadetect model={DETECTION_MODEL} device={DEVICE} "
|
| 52 |
+
f"threshold={DET_THRESHOLD} ! queue ! "
|
| 53 |
+
f"gvaclassify model={REID_MODEL} device={DEVICE} ! queue ! "
|
| 54 |
+
"appsink name=sink emit-signals=true sync=false max-buffers=4 drop=false"
|
| 55 |
+
)
|
| 56 |
+
sink = pipeline.get_by_name("sink")
|
| 57 |
+
sink.connect("new-sample", on_frame)
|
| 58 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 59 |
+
pipeline.get_bus().timed_pop_filtered(
|
| 60 |
+
Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR)
|
| 61 |
+
pipeline.set_state(Gst.State.NULL)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# 1. Compute the reference embedding from the reference image.
|
| 65 |
+
ref = {"emb": None, "area": 0}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def on_reference(sink):
|
| 69 |
+
sample = sink.emit("pull-sample")
|
| 70 |
+
if sample is None:
|
| 71 |
+
return Gst.FlowReturn.OK
|
| 72 |
+
vf = VideoFrame(sample.get_buffer(), caps=sample.get_caps())
|
| 73 |
+
for (x, y, w, h), emb in face_embeddings(vf):
|
| 74 |
+
if w * h > ref["area"]:
|
| 75 |
+
ref["area"] = w * h
|
| 76 |
+
ref["emb"] = emb
|
| 77 |
+
return Gst.FlowReturn.OK
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
run_pipeline(f"filesrc location={REFERENCE_IMAGE} ! jpegdec", on_reference)
|
| 81 |
+
if ref["emb"] is None:
|
| 82 |
+
raise SystemExit("No face detected in the reference image")
|
| 83 |
+
ref_emb = ref["emb"]
|
| 84 |
+
|
| 85 |
+
# 2. Process the video, boxing only faces that match the reference identity.
|
| 86 |
+
writer = {"w": None}
|
| 87 |
+
match_frames = 0
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def on_video(sink):
|
| 91 |
+
global match_frames
|
| 92 |
+
sample = sink.emit("pull-sample")
|
| 93 |
+
if sample is None:
|
| 94 |
+
return Gst.FlowReturn.OK
|
| 95 |
+
vf = VideoFrame(sample.get_buffer(), caps=sample.get_caps())
|
| 96 |
+
matches = []
|
| 97 |
+
for (x, y, w, h), emb in face_embeddings(vf):
|
| 98 |
+
similarity = float(np.dot(emb, ref_emb))
|
| 99 |
+
if similarity >= MATCH_THRESHOLD:
|
| 100 |
+
matches.append((x, y, w, h, similarity))
|
| 101 |
+
|
| 102 |
+
with vf.data() as mat:
|
| 103 |
+
frame = mat.copy()
|
| 104 |
+
|
| 105 |
+
if writer["w"] is None:
|
| 106 |
+
frame_h, frame_w = frame.shape[:2]
|
| 107 |
+
structure = sample.get_caps().get_structure(0)
|
| 108 |
+
ok_fr, fps_n, fps_d = structure.get_fraction("framerate")
|
| 109 |
+
fps = fps_n / fps_d if ok_fr and fps_d else 12
|
| 110 |
+
writer["w"] = cv2.VideoWriter(
|
| 111 |
+
OUTPUT_VIDEO, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 112 |
+
|
| 113 |
+
for x, y, w, h, similarity in matches:
|
| 114 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 115 |
+
cv2.putText(frame, f"MATCH {similarity:.2f}", (x, max(15, y - 8)),
|
| 116 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 117 |
+
if matches:
|
| 118 |
+
match_frames += 1
|
| 119 |
+
writer["w"].write(frame)
|
| 120 |
+
return Gst.FlowReturn.OK
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
run_pipeline(f"filesrc location={INPUT_VIDEO} ! decodebin3", on_video)
|
| 124 |
+
if writer["w"] is not None:
|
| 125 |
+
writer["w"].release()
|
| 126 |
+
print(f"Frames with a matched face: {match_frames}", flush=True)
|
| 127 |
+
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 opencv-python numpy
|
| 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 -q openvino opencv-python
|
| 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,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# Face Matching: 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 |
+
REFERENCE_IMAGE = "face_a.jpg" # reference: left subject captured from the video
|
| 14 |
+
SCENE_VIDEO = "test_video.mp4" # scene containing the reference subject plus others
|
| 15 |
+
SCENE_FRAME = 480 # frame index of the two-person scene (~40s)
|
| 16 |
+
CONF_THRESHOLD = 0.5
|
| 17 |
+
MATCH_THRESHOLD = 0.5
|
| 18 |
+
|
| 19 |
+
core = ov.Core()
|
| 20 |
+
|
| 21 |
+
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
|
| 22 |
+
det_compiled = core.compile_model(core.read_model(DETECTION_MODEL), "CPU")
|
| 23 |
+
reid_compiled = core.compile_model(core.read_model(REID_MODEL), "CPU")
|
| 24 |
+
|
| 25 |
+
det_input = det_compiled.input(0)
|
| 26 |
+
det_h, det_w = det_input.shape[2], det_input.shape[3]
|
| 27 |
+
reid_input = reid_compiled.input(0)
|
| 28 |
+
reid_h, reid_w = reid_input.shape[2], reid_input.shape[3]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def detect_faces(img):
|
| 32 |
+
h0, w0 = img.shape[:2]
|
| 33 |
+
blob = cv2.resize(img, (det_w, det_h))
|
| 34 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 35 |
+
dets = det_compiled([blob])[det_compiled.output(0)][0][0]
|
| 36 |
+
faces = []
|
| 37 |
+
for d in dets:
|
| 38 |
+
if float(d[2]) < CONF_THRESHOLD:
|
| 39 |
+
continue
|
| 40 |
+
x1 = max(0, int(d[3] * w0))
|
| 41 |
+
y1 = max(0, int(d[4] * h0))
|
| 42 |
+
x2 = min(w0, int(d[5] * w0))
|
| 43 |
+
y2 = min(h0, int(d[6] * h0))
|
| 44 |
+
if x2 > x1 and y2 > y1:
|
| 45 |
+
faces.append((x1, y1, x2, y2))
|
| 46 |
+
return faces
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_embedding(img, bbox):
|
| 50 |
+
x1, y1, x2, y2 = bbox
|
| 51 |
+
crop = img[y1:y2, x1:x2]
|
| 52 |
+
blob = cv2.resize(crop, (reid_w, reid_h))
|
| 53 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 54 |
+
emb = reid_compiled([blob])[reid_compiled.output(0)].flatten()
|
| 55 |
+
return emb / np.linalg.norm(emb)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# 1. Embed the reference face.
|
| 59 |
+
reference = cv2.imread(REFERENCE_IMAGE)
|
| 60 |
+
ref_faces = detect_faces(reference)
|
| 61 |
+
if not ref_faces:
|
| 62 |
+
raise SystemExit("No face detected in the reference image")
|
| 63 |
+
ref_bbox = max(ref_faces, key=lambda b: (b[2] - b[0]) * (b[3] - b[1]))
|
| 64 |
+
ref_emb = get_embedding(reference, ref_bbox)
|
| 65 |
+
|
| 66 |
+
# 2. Read a scene frame that contains several people.
|
| 67 |
+
cap = cv2.VideoCapture(SCENE_VIDEO)
|
| 68 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, SCENE_FRAME)
|
| 69 |
+
ok, frame = cap.read()
|
| 70 |
+
cap.release()
|
| 71 |
+
if not ok:
|
| 72 |
+
raise SystemExit(f"Could not read frame {SCENE_FRAME} from {SCENE_VIDEO}")
|
| 73 |
+
|
| 74 |
+
# 3. Compare every face in the scene to the reference; keep the best match.
|
| 75 |
+
best_bbox = None
|
| 76 |
+
best_sim = 0.0
|
| 77 |
+
for bbox in detect_faces(frame):
|
| 78 |
+
sim = float(np.dot(get_embedding(frame, bbox), ref_emb))
|
| 79 |
+
if sim > best_sim:
|
| 80 |
+
best_sim = sim
|
| 81 |
+
best_bbox = bbox
|
| 82 |
+
|
| 83 |
+
if best_bbox is not None and best_sim >= MATCH_THRESHOLD:
|
| 84 |
+
x1, y1, x2, y2 = best_bbox
|
| 85 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 86 |
+
cv2.putText(frame, f"MATCH {best_sim:.2f}", (x1, max(15, y1 - 8)),
|
| 87 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 88 |
+
print(f"Matched reference identity, similarity {best_sim:.4f}")
|
| 89 |
+
else:
|
| 90 |
+
print(f"No matching face found (best similarity {best_sim:.4f})")
|
| 91 |
+
|
| 92 |
+
cv2.imwrite("output_openvino.jpg", frame)
|
| 93 |
+
print("Saved: output_openvino.jpg")
|