Sync facial-recognition from metro-analytics-catalog
Browse files- .gitattributes +2 -0
- LICENSE +21 -0
- README.md +386 -0
- expected_output_dlstreamer.gif +3 -0
- expected_output_openvino.gif +3 -0
- export_and_quantize.sh +65 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
expected_output_dlstreamer.gif filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
expected_output_openvino.gif filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) Intel Corporation.
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE
|
README.md
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
license_link: LICENSE
|
| 4 |
+
library_name: openvino
|
| 5 |
+
pipeline_tag: image-classification
|
| 6 |
+
tags:
|
| 7 |
+
- openvino
|
| 8 |
+
- intel
|
| 9 |
+
- face-detection
|
| 10 |
+
- face-reidentification
|
| 11 |
+
- edge-ai
|
| 12 |
+
- metro
|
| 13 |
+
- dlstreamer
|
| 14 |
+
language:
|
| 15 |
+
- en
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Facial Recognition
|
| 19 |
+
|
| 20 |
+
| Property | Value |
|
| 21 |
+
|---|---|
|
| 22 |
+
| **Category** | Face Detection + Re-Identification |
|
| 23 |
+
| **Base Model** | [face-detection-adas-0001](https://docs.openvino.ai/2024/omz_models_model_face_detection_adas_0001.html) + [face-reidentification-retail-0095](https://docs.openvino.ai/2024/omz_models_model_face_reidentification_retail_0095.html) (Open Model Zoo) |
|
| 24 |
+
| **Source Framework** | Caffe / PyTorch (Open Model Zoo) |
|
| 25 |
+
| **Supported Precisions** | FP32, FP16 |
|
| 26 |
+
| **Inference Engine** | OpenVINO |
|
| 27 |
+
| **Hardware** | CPU, GPU, NPU |
|
| 28 |
+
| **Detected Class(es)** | Human faces (detection) + 256-d face embeddings (re-identification) |
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## Overview
|
| 33 |
+
|
| 34 |
+
Facial Recognition is a Metro Analytics use case that detects human faces in
|
| 35 |
+
images and video and computes a 256-dimensional embedding vector for each face,
|
| 36 |
+
enabling enrollment, search, and identification against a known gallery.
|
| 37 |
+
|
| 38 |
+
The pipeline composes two Intel Open Model Zoo models:
|
| 39 |
+
|
| 40 |
+
- **face-detection-adas-0001** -- an SSD-based face detector optimized for
|
| 41 |
+
automotive and surveillance cameras (FP16, 384x672 input).
|
| 42 |
+
- **face-reidentification-retail-0095** -- a compact CNN that maps a cropped
|
| 43 |
+
face to a 256-d embedding; cosine similarity between embeddings determines
|
| 44 |
+
identity.
|
| 45 |
+
|
| 46 |
+
These models are well-tested with OpenVINO Runtime and Intel DLStreamer's
|
| 47 |
+
`gvadetect` + `gvaclassify` pipeline.
|
| 48 |
+
|
| 49 |
+
Typical Metro deployments include:
|
| 50 |
+
|
| 51 |
+
- **Access Control** -- match employees or authorized personnel against an enrollment gallery.
|
| 52 |
+
- **VIP Identification** -- recognize known individuals in a crowd.
|
| 53 |
+
- **Search and Forensics** -- find a person of interest across multiple camera feeds.
|
| 54 |
+
- **Attendance Tracking** -- log when enrolled individuals enter or leave a facility.
|
| 55 |
+
|
| 56 |
+
> **Privacy Note:** Facial recognition involves biometric data.
|
| 57 |
+
> Ensure your deployment complies with applicable privacy regulations
|
| 58 |
+
> (GDPR, BIPA, etc.) and has proper consent mechanisms in place.
|
| 59 |
+
|
| 60 |
+
---
|
| 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/2026.0/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
|
| 67 |
+
|
| 68 |
+
Create and activate a Python virtual environment before running the scripts:
|
| 69 |
+
|
| 70 |
+
```bash
|
| 71 |
+
python3 -m venv .venv --system-site-packages
|
| 72 |
+
source .venv/bin/activate
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
> **Note:** The `--system-site-packages` flag is required so the virtual
|
| 76 |
+
> environment can access the system-installed OpenVINO and DLStreamer Python
|
| 77 |
+
> packages.
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## Getting Started
|
| 82 |
+
|
| 83 |
+
### Download Models
|
| 84 |
+
|
| 85 |
+
Run the provided script to download the face detection and re-identification
|
| 86 |
+
models from the Open Model Zoo:
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
chmod +x export_and_quantize.sh
|
| 90 |
+
./export_and_quantize.sh
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
The script performs the following steps:
|
| 94 |
+
|
| 95 |
+
1. Installs `openvino`.
|
| 96 |
+
2. Downloads `face-detection-adas-0001` (FP16) into `./intel/face-detection-adas-0001/FP16/`.
|
| 97 |
+
3. Downloads `face-reidentification-retail-0095` (FP16) into `./intel/face-reidentification-retail-0095/FP16/`.
|
| 98 |
+
4. Downloads a sample test video (`test_video.mp4`).
|
| 99 |
+
|
| 100 |
+
### OpenVINO Sample
|
| 101 |
+
|
| 102 |
+
The sample below runs recognition on the sample video. It detects every face,
|
| 103 |
+
computes a 256-d embedding, and matches it against a gallery of previously seen
|
| 104 |
+
people. Each new person is enrolled and assigned a numeric ID; when the same
|
| 105 |
+
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
|
| 113 |
+
|
| 114 |
+
DETECTION_MODEL = "intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 115 |
+
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 116 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 117 |
+
CONF_THRESHOLD = 0.6
|
| 118 |
+
MATCH_THRESHOLD = 0.5
|
| 119 |
+
|
| 120 |
+
core = ov.Core()
|
| 121 |
+
|
| 122 |
+
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
|
| 123 |
+
det_model = core.compile_model(core.read_model(DETECTION_MODEL), "CPU")
|
| 124 |
+
reid_model = core.compile_model(core.read_model(REID_MODEL), "CPU")
|
| 125 |
+
|
| 126 |
+
det_input = det_model.input(0)
|
| 127 |
+
det_h, det_w = det_input.shape[2], det_input.shape[3]
|
| 128 |
+
reid_input = reid_model.input(0)
|
| 129 |
+
reid_h, reid_w = reid_input.shape[2], reid_input.shape[3]
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def detect_faces(img):
|
| 133 |
+
h0, w0 = img.shape[:2]
|
| 134 |
+
blob = cv2.resize(img, (det_w, det_h))
|
| 135 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 136 |
+
detections = det_model([blob])[det_model.output(0)][0][0]
|
| 137 |
+
boxes = []
|
| 138 |
+
for det in detections:
|
| 139 |
+
if float(det[2]) < CONF_THRESHOLD:
|
| 140 |
+
continue
|
| 141 |
+
x1 = max(0, int(det[3] * w0))
|
| 142 |
+
y1 = max(0, int(det[4] * h0))
|
| 143 |
+
x2 = min(w0, int(det[5] * w0))
|
| 144 |
+
y2 = min(h0, int(det[6] * h0))
|
| 145 |
+
if x2 > x1 and y2 > y1:
|
| 146 |
+
boxes.append((x1, y1, x2, y2))
|
| 147 |
+
return boxes
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def get_embedding(img, bbox):
|
| 151 |
+
x1, y1, x2, y2 = bbox
|
| 152 |
+
crop = img[y1:y2, x1:x2]
|
| 153 |
+
blob = cv2.resize(crop, (reid_w, reid_h))
|
| 154 |
+
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
|
| 155 |
+
emb = reid_model([blob])[reid_model.output(0)].flatten()
|
| 156 |
+
return emb / np.linalg.norm(emb)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
# Gallery of (numeric_id, embedding). recognize() returns an existing ID for a
|
| 160 |
+
# known face or enrolls a new one, keeping each person's ID stable over time.
|
| 161 |
+
gallery = []
|
| 162 |
+
next_id = 1
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def recognize(embedding):
|
| 166 |
+
global next_id
|
| 167 |
+
best_index, best_sim = -1, 0.0
|
| 168 |
+
for index, (_, gallery_emb) in enumerate(gallery):
|
| 169 |
+
sim = float(np.dot(embedding, gallery_emb))
|
| 170 |
+
if sim > best_sim:
|
| 171 |
+
best_sim, best_index = sim, index
|
| 172 |
+
if best_sim >= MATCH_THRESHOLD:
|
| 173 |
+
person_id, gallery_emb = gallery[best_index]
|
| 174 |
+
# Blend the embedding into the gallery entry to stay robust to pose.
|
| 175 |
+
updated = 0.9 * gallery_emb + 0.1 * embedding
|
| 176 |
+
gallery[best_index] = (person_id, updated / np.linalg.norm(updated))
|
| 177 |
+
return person_id
|
| 178 |
+
person_id = next_id
|
| 179 |
+
next_id += 1
|
| 180 |
+
gallery.append((person_id, embedding))
|
| 181 |
+
print(f"Enrolled ID {person_id}")
|
| 182 |
+
return person_id
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
cap = cv2.VideoCapture(INPUT_VIDEO)
|
| 186 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 12
|
| 187 |
+
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 188 |
+
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 189 |
+
writer = cv2.VideoWriter(
|
| 190 |
+
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 191 |
+
|
| 192 |
+
while True:
|
| 193 |
+
ok, frame = cap.read()
|
| 194 |
+
if not ok:
|
| 195 |
+
break
|
| 196 |
+
for bbox in detect_faces(frame):
|
| 197 |
+
person_id = recognize(get_embedding(frame, bbox))
|
| 198 |
+
x1, y1, x2, y2 = bbox
|
| 199 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 200 |
+
cv2.putText(frame, f"ID {person_id}", (x1, max(15, y1 - 8)),
|
| 201 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 202 |
+
writer.write(frame)
|
| 203 |
+
|
| 204 |
+
cap.release()
|
| 205 |
+
writer.release()
|
| 206 |
+
print(f"Total identities recognized: {len(gallery)}")
|
| 207 |
+
print("Saved: output_openvino.mp4")
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
**Device targets:**
|
| 211 |
+
|
| 212 |
+
- `"CPU"` -- default, works on all Intel platforms.
|
| 213 |
+
- `"GPU"` -- Intel integrated or discrete GPU.
|
| 214 |
+
- `"NPU"` -- Intel NPU; face-detection-adas-0001 FP16 is NPU-compatible.
|
| 215 |
+
|
| 216 |
+
#### Expected Output
|
| 217 |
+
|
| 218 |
+

|
| 219 |
+
|
| 220 |
+
### DLStreamer Sample
|
| 221 |
+
|
| 222 |
+
The pipeline below runs the face detector via `gvadetect` and the
|
| 223 |
+
re-identification model via `gvaclassify` on the video. Frames are pulled through
|
| 224 |
+
an `appsink`, where each face's embedding is matched against a gallery to assign
|
| 225 |
+
a stable numeric ID (new people are enrolled, returning people keep their ID).
|
| 226 |
+
Every face is annotated with its `ID <n>` and the result is saved to
|
| 227 |
+
`output_dlstreamer.mp4`.
|
| 228 |
+
|
| 229 |
+
> **Notes on running this sample:**
|
| 230 |
+
>
|
| 231 |
+
> - Export `PYTHONPATH` so the DLStreamer Python modules (`gi`, `gstgva`) are
|
| 232 |
+
> importable:
|
| 233 |
+
>
|
| 234 |
+
> ```bash
|
| 235 |
+
> source /opt/intel/openvino_2026/setupvars.sh
|
| 236 |
+
> source /opt/intel/dlstreamer/scripts/setup_dls_env.sh
|
| 237 |
+
> export PYTHONPATH=/opt/intel/dlstreamer/python:\
|
| 238 |
+
> /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
|
| 239 |
+
> ```
|
| 240 |
+
>
|
| 241 |
+
> - The re-identification embedding is attached as a tensor on each face's
|
| 242 |
+
> region-of-interest metadata. Convert the stream to `BGR` **before**
|
| 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")
|
| 250 |
+
from gi.repository import Gst
|
| 251 |
+
|
| 252 |
+
Gst.init([])
|
| 253 |
+
|
| 254 |
+
import numpy as np
|
| 255 |
+
import cv2
|
| 256 |
+
from gstgva import VideoFrame
|
| 257 |
+
|
| 258 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 259 |
+
OUTPUT_VIDEO = "output_dlstreamer.mp4"
|
| 260 |
+
DETECTION_MODEL = "intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 261 |
+
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 262 |
+
# For CPU: change "GPU" to "CPU". For NPU: change "GPU" to "NPU".
|
| 263 |
+
DEVICE = "GPU"
|
| 264 |
+
DET_THRESHOLD = 0.6
|
| 265 |
+
MATCH_THRESHOLD = 0.5
|
| 266 |
+
|
| 267 |
+
# Gallery of (numeric_id, embedding). recognize() returns an existing ID for a
|
| 268 |
+
# known face or enrolls a new one, keeping each person's ID stable over time.
|
| 269 |
+
gallery = []
|
| 270 |
+
next_id = 1
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def recognize(embedding):
|
| 274 |
+
global next_id
|
| 275 |
+
best_index, best_sim = -1, 0.0
|
| 276 |
+
for index, (_, gallery_emb) in enumerate(gallery):
|
| 277 |
+
sim = float(np.dot(embedding, gallery_emb))
|
| 278 |
+
if sim > best_sim:
|
| 279 |
+
best_sim, best_index = sim, index
|
| 280 |
+
if best_sim >= MATCH_THRESHOLD:
|
| 281 |
+
person_id, gallery_emb = gallery[best_index]
|
| 282 |
+
# Blend the embedding into the gallery entry to stay robust to pose.
|
| 283 |
+
updated = 0.9 * gallery_emb + 0.1 * embedding
|
| 284 |
+
gallery[best_index] = (person_id, updated / np.linalg.norm(updated))
|
| 285 |
+
return person_id
|
| 286 |
+
person_id = next_id
|
| 287 |
+
next_id += 1
|
| 288 |
+
gallery.append((person_id, embedding))
|
| 289 |
+
print(f"Enrolled ID {person_id}", flush=True)
|
| 290 |
+
return person_id
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def face_embeddings(video_frame):
|
| 294 |
+
"""Yield ((x, y, w, h), normalized_embedding) for each classified face."""
|
| 295 |
+
for region in video_frame.regions():
|
| 296 |
+
rect = region.rect()
|
| 297 |
+
emb = None
|
| 298 |
+
for tensor in region.tensors():
|
| 299 |
+
if tensor.is_detection():
|
| 300 |
+
continue
|
| 301 |
+
data = np.array(tensor.data(), dtype=np.float32)
|
| 302 |
+
if data.size >= 256:
|
| 303 |
+
emb = data[:256]
|
| 304 |
+
if emb is None:
|
| 305 |
+
continue
|
| 306 |
+
emb = emb / (np.linalg.norm(emb) + 1e-9)
|
| 307 |
+
yield (int(rect.x), int(rect.y), int(rect.w), int(rect.h)), emb
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
# Convert to BGR before inference so gvaclassify's embedding tensors survive to
|
| 311 |
+
# the appsink (a later format-changing videoconvert would strip them).
|
| 312 |
+
pipeline = Gst.parse_launch(
|
| 313 |
+
f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
|
| 314 |
+
"videoconvert ! video/x-raw,format=BGR ! "
|
| 315 |
+
f"gvadetect model={DETECTION_MODEL} device={DEVICE} "
|
| 316 |
+
f"threshold={DET_THRESHOLD} ! queue ! "
|
| 317 |
+
f"gvaclassify model={REID_MODEL} device={DEVICE} ! queue ! "
|
| 318 |
+
"appsink name=sink emit-signals=true sync=false max-buffers=4 drop=false"
|
| 319 |
+
)
|
| 320 |
+
sink = pipeline.get_by_name("sink")
|
| 321 |
+
|
| 322 |
+
writer = {"w": None}
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
def on_video(sink):
|
| 326 |
+
sample = sink.emit("pull-sample")
|
| 327 |
+
if sample is None:
|
| 328 |
+
return Gst.FlowReturn.OK
|
| 329 |
+
vf = VideoFrame(sample.get_buffer(), caps=sample.get_caps())
|
| 330 |
+
labeled = []
|
| 331 |
+
for (x, y, w, h), emb in face_embeddings(vf):
|
| 332 |
+
labeled.append((x, y, w, h, recognize(emb)))
|
| 333 |
+
|
| 334 |
+
with vf.data() as mat:
|
| 335 |
+
frame = mat.copy()
|
| 336 |
+
|
| 337 |
+
if writer["w"] is None:
|
| 338 |
+
frame_h, frame_w = frame.shape[:2]
|
| 339 |
+
structure = sample.get_caps().get_structure(0)
|
| 340 |
+
ok_fr, fps_n, fps_d = structure.get_fraction("framerate")
|
| 341 |
+
fps = fps_n / fps_d if ok_fr and fps_d else 12
|
| 342 |
+
writer["w"] = cv2.VideoWriter(
|
| 343 |
+
OUTPUT_VIDEO, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
|
| 344 |
+
|
| 345 |
+
for x, y, w, h, person_id in labeled:
|
| 346 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 347 |
+
cv2.putText(frame, f"ID {person_id}", (x, max(15, y - 8)),
|
| 348 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 349 |
+
writer["w"].write(frame)
|
| 350 |
+
return Gst.FlowReturn.OK
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
sink.connect("new-sample", on_video)
|
| 354 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 355 |
+
pipeline.get_bus().timed_pop_filtered(
|
| 356 |
+
Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS | Gst.MessageType.ERROR)
|
| 357 |
+
pipeline.set_state(Gst.State.NULL)
|
| 358 |
+
if writer["w"] is not None:
|
| 359 |
+
writer["w"].release()
|
| 360 |
+
print(f"Total identities recognized: {len(gallery)}", flush=True)
|
| 361 |
+
print(f"Saved: {OUTPUT_VIDEO}", flush=True)
|
| 362 |
+
```
|
| 363 |
+
|
| 364 |
+
**Device targets:**
|
| 365 |
+
|
| 366 |
+
- `DEVICE = "GPU"` -- default in the sample code.
|
| 367 |
+
- `DEVICE = "CPU"` -- change `"GPU"` to `"CPU"`.
|
| 368 |
+
- `DEVICE = "NPU"` -- change `"GPU"` to `"NPU"`; use `batch-size=1` and `nireq=4` for best NPU utilization.
|
| 369 |
+
|
| 370 |
+
#### Expected Output
|
| 371 |
+
|
| 372 |
+

|
| 373 |
+
|
| 374 |
+
---
|
| 375 |
+
|
| 376 |
+
## License
|
| 377 |
+
|
| 378 |
+
Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
| 379 |
+
|
| 380 |
+
## References
|
| 381 |
+
|
| 382 |
+
- [face-detection-adas-0001](https://docs.openvino.ai/2024/omz_models_model_face_detection_adas_0001.html)
|
| 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/2026.0/edge-ai-libraries/dlstreamer/index.html)
|
expected_output_dlstreamer.gif
ADDED
|
Git LFS Details
|
expected_output_openvino.gif
ADDED
|
Git LFS Details
|
export_and_quantize.sh
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
# SPDX-License-Identifier: MIT
|
| 3 |
+
# Copyright (C) Intel Corporation
|
| 4 |
+
#
|
| 5 |
+
# Download face detection and re-identification models from Open Model Zoo
|
| 6 |
+
# for the facial-recognition use case.
|
| 7 |
+
# Usage: ./export_and_quantize.sh
|
| 8 |
+
|
| 9 |
+
set -euo pipefail
|
| 10 |
+
|
| 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:"
|
| 36 |
+
echo " - Model weights and/or sample files"
|
| 37 |
+
echo ""
|
| 38 |
+
read -p "Continue with downloads? (yes/no): " APPROVAL
|
| 39 |
+
if [[ "${APPROVAL}" != "yes" ]]; then
|
| 40 |
+
echo "Download cancelled by user."
|
| 41 |
+
exit 0
|
| 42 |
+
fi
|
| 43 |
+
echo ""
|
| 44 |
+
|
| 45 |
+
echo "--- Downloading face-detection-adas-0001 (FP16) ---"
|
| 46 |
+
download_omz_model face-detection-adas-0001 FP16
|
| 47 |
+
echo "Ready: face-detection-adas-0001"
|
| 48 |
+
|
| 49 |
+
echo "--- Downloading face-reidentification-retail-0095 (FP16) ---"
|
| 50 |
+
download_omz_model face-reidentification-retail-0095 FP16
|
| 51 |
+
echo "Ready: face-reidentification-retail-0095"
|
| 52 |
+
|
| 53 |
+
echo "--- Downloading sample test video ---"
|
| 54 |
+
if [[ ! -f test_video.mp4 ]]; then
|
| 55 |
+
wget -q -O test_video.mp4 \
|
| 56 |
+
"https://github.com/intel-iot-devkit/sample-videos/raw/master/face-demographics-walking-and-pause.mp4"
|
| 57 |
+
echo "Downloaded: test_video.mp4"
|
| 58 |
+
else
|
| 59 |
+
echo "Already present: test_video.mp4"
|
| 60 |
+
fi
|
| 61 |
+
|
| 62 |
+
echo "--- Done ---"
|
| 63 |
+
echo "Detector : intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
|
| 64 |
+
echo "ReID : intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
|
| 65 |
+
echo "Video : test_video.mp4"
|