perimeter-breach-detection / openvino_sample.py
vagheshpatel's picture
Update native RC1 samples and supported device documentation
64f1534 verified
Raw History Blame Contribute Delete
3.12 kB
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Perimeter Breach Detection: 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
PERSON_CLASS_ID = 0
CONF_THRESHOLD = 0.4
# Select the model: "yolo26n" (default) or "detr-resnet-50".
MODEL = "yolo26n"
INPUT_SIZE = 800 if MODEL.startswith("detr") else 640
INPUT_VIDEO = "worker-zone-detection.mp4"
# Restricted zone traced from the yellow-and-black safety tape in the sample
# video (1920x1080). The tape runs diagonally from (1577, 0) to (434, 1079);
# this polygon covers the keep-out side of that boundary. Edit these points to
# retrace the tape for a different camera.
RESTRICTED_ZONE = np.array([[0, 0], [1577, 0], [434, 1079], [0, 1080]], dtype=np.int32)
core = ov.Core()
model = core.read_model(f"{MODEL}_openvino_model/{MODEL}.xml")
# Change device to "GPU" or "NPU" to run on integrated GPU or NPU.
compiled = core.compile_model(model, "CPU")
cap = cv2.VideoCapture(INPUT_VIDEO)
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
w0 = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h0 = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
zone = RESTRICTED_ZONE
writer = cv2.VideoWriter(
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w0, h0)
)
sx, sy = w0 / INPUT_SIZE, h0 / INPUT_SIZE
frame_idx = 0
while True:
ok, frame = cap.read()
if not ok:
break
frame_idx += 1
blob = cv2.resize(frame, (INPUT_SIZE, INPUT_SIZE))
blob = cv2.cvtColor(blob, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
blob = blob.transpose(2, 0, 1)[np.newaxis, ...] # NCHW
# End-to-end output: [1, N, 6] = [x1, y1, x2, y2, confidence, class_id]
# (N = 300 for YOLO26, 100 for DETR)
output = compiled([blob])[compiled.output(0)][0]
mask = (output[:, 4] >= CONF_THRESHOLD) & (output[:, 5].astype(int) == PERSON_CLASS_ID)
# Draw the translucent restricted zone first, then the person boxes on top.
overlay = frame.copy()
cv2.fillPoly(overlay, [zone], (0, 0, 255))
cv2.addWeighted(overlay, 0.25, frame, 0.75, 0, frame)
cv2.polylines(frame, [zone], True, (0, 255, 255), 2)
breaches = 0
for det in output[mask]:
x1, y1 = int(det[0] * sx), int(det[1] * sy)
x2, y2 = int(det[2] * sx), int(det[3] * sy)
center = (int((x1 + x2) / 2), int((y1 + y2) / 2))
inside = cv2.pointPolygonTest(zone, center, False) >= 0
color = (0, 0, 255) if inside else (0, 255, 0)
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
if inside:
breaches += 1
cv2.putText(
frame, "BREACH", (x1, max(y1 - 6, 12)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2,
)
cv2.putText(
frame, f"Breaches: {breaches}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2,
)
if breaches:
print(f"frame {frame_idx}: perimeter breach - {breaches} person(s) inside zone", flush=True)
writer.write(frame)
cap.release()
writer.release()
print("Saved: output_openvino.mp4")