delivery-package-verification / openvino_sample.py
vagheshpatel's picture
Update native RC1 samples and supported device documentation
af84571 verified
Raw History Blame Contribute Delete
2.87 kB
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Delivery/Package Verification: 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
# COCO classes used as delivery-package proxies; all shown as "package".
PACKAGE_CLASS_IDS = {24, 26, 28} # backpack, handbag, suitcase
PACKAGE_LABEL = "package"
BOX_COLOR = (0, 200, 0)
CONF_THRESHOLD = 0.25
# Select the model: "yolo26n" (default) or "detr-resnet-50".
MODEL = "yolo26n"
INPUT_SIZE = 800 if MODEL.startswith("detr") else 640
INPUT_VIDEO = "test_video.mp4"
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")
def letterbox(image, size=INPUT_SIZE):
"""Resize keeping aspect ratio and pad to a square (matches gvadetect)."""
h, w = image.shape[:2]
ratio = min(size / h, size / w)
nw, nh = int(round(w * ratio)), int(round(h * ratio))
resized = cv2.resize(image, (nw, nh))
canvas = np.full((size, size, 3), 114, dtype=np.uint8)
pad_x, pad_y = (size - nw) // 2, (size - nh) // 2
canvas[pad_y:pad_y + nh, pad_x:pad_x + nw] = resized
return canvas, ratio, pad_x, pad_y
cap = cv2.VideoCapture(INPUT_VIDEO)
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
writer = cv2.VideoWriter(
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
frame_idx = 0
while True:
ok, frame = cap.read()
if not ok:
break
frame_idx += 1
padded, ratio, pad_x, pad_y = letterbox(frame, INPUT_SIZE)
blob = cv2.cvtColor(padded, 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) & np.isin(
output[:, 5].astype(int), list(PACKAGE_CLASS_IDS))
dets = output[mask]
for det in dets:
# Undo the letterbox padding and scaling to map boxes back to the frame.
x1 = int((det[0] - pad_x) / ratio)
y1 = int((det[1] - pad_y) / ratio)
x2 = int((det[2] - pad_x) / ratio)
y2 = int((det[3] - pad_y) / ratio)
conf = float(det[4])
label = f"{PACKAGE_LABEL} {conf:.2f}"
cv2.rectangle(frame, (x1, y1), (x2, y2), BOX_COLOR, 2)
cv2.putText(frame, label, (x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, BOX_COLOR, 2)
writer.write(frame)
print(f"Frame {frame_idx}: Packages detected: {len(dets)}", flush=True)
cap.release()
writer.release()