facial-recognition / openvino_sample.py
vagheshpatel's picture
Update native RC1 samples and supported device documentation
75986a7 verified
Raw History Blame Contribute Delete
3.53 kB
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Facial Recognition: OpenVINO Runtime sample.
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
import cv2
import numpy as np
import openvino as ov
DETECTION_MODEL = "intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
REID_MODEL = "intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
INPUT_VIDEO = "test_video.mp4"
CONF_THRESHOLD = 0.6
MATCH_THRESHOLD = 0.5
core = ov.Core()
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
det_model = core.compile_model(core.read_model(DETECTION_MODEL), "CPU")
reid_model = core.compile_model(core.read_model(REID_MODEL), "CPU")
det_input = det_model.input(0)
det_h, det_w = det_input.shape[2], det_input.shape[3]
reid_input = reid_model.input(0)
reid_h, reid_w = reid_input.shape[2], reid_input.shape[3]
def detect_faces(img):
h0, w0 = img.shape[:2]
blob = cv2.resize(img, (det_w, det_h))
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
detections = det_model([blob])[det_model.output(0)][0][0]
boxes = []
for det in detections:
if float(det[2]) < CONF_THRESHOLD:
continue
x1 = max(0, int(det[3] * w0))
y1 = max(0, int(det[4] * h0))
x2 = min(w0, int(det[5] * w0))
y2 = min(h0, int(det[6] * h0))
if x2 > x1 and y2 > y1:
boxes.append((x1, y1, x2, y2))
return boxes
def get_embedding(img, bbox):
x1, y1, x2, y2 = bbox
crop = img[y1:y2, x1:x2]
blob = cv2.resize(crop, (reid_w, reid_h))
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
emb = reid_model([blob])[reid_model.output(0)].flatten()
return emb / np.linalg.norm(emb)
# Gallery of (numeric_id, embedding). recognize() returns an existing ID for a
# known face or enrolls a new one, keeping each person's ID stable over time.
gallery = []
next_id = 1
def recognize(embedding):
global next_id
best_index, best_sim = -1, 0.0
for index, (_, gallery_emb) in enumerate(gallery):
sim = float(np.dot(embedding, gallery_emb))
if sim > best_sim:
best_sim, best_index = sim, index
if best_sim >= MATCH_THRESHOLD:
person_id, gallery_emb = gallery[best_index]
# Blend the embedding into the gallery entry to stay robust to pose.
updated = 0.9 * gallery_emb + 0.1 * embedding
gallery[best_index] = (person_id, updated / np.linalg.norm(updated))
return person_id
person_id = next_id
next_id += 1
gallery.append((person_id, embedding))
print(f"Enrolled ID {person_id}")
return person_id
cap = cv2.VideoCapture(INPUT_VIDEO)
fps = cap.get(cv2.CAP_PROP_FPS) or 12
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter(
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_w, frame_h))
while True:
ok, frame = cap.read()
if not ok:
break
for bbox in detect_faces(frame):
person_id = recognize(get_embedding(frame, bbox))
x1, y1, x2, y2 = bbox
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f"ID {person_id}", (x1, max(15, y1 - 8)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
writer.write(frame)
cap.release()
writer.release()
print(f"Total identities recognized: {len(gallery)}")
print("Saved: output_openvino.mp4")