person-reidentification / export_and_quantize.sh
vagheshpatel's picture
Sync person-reidentification from metro-analytics-catalog
8505bef verified
Raw
History Blame Contribute Delete
4.56 kB
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Download the person detection and person re-identification models from the
# Open Model Zoo for the person-reidentification use case, download the sample
# surveillance video, and capture a reference person crop (person_a.jpg) that
# represents the identity to re-identify across camera views.
# Usage: ./export_and_quantize.sh
set -euo pipefail
# Official Open Model Zoo public model storage (versioned, immutable).
OMZ_BASE="https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1"
echo "--- Installing dependencies ---"
pip install -qU openvino opencv-python numpy
# Download both the IR topology (.xml) and weights (.bin) for an OMZ model
# from the official storage into intel/<model>/<precision>/.
download_omz_model() {
local model="$1"
local precision="$2"
local dest="intel/${model}/${precision}"
mkdir -p "${dest}"
local ext
for ext in xml bin; do
if [[ ! -f "${dest}/${model}.${ext}" ]]; then
wget -q -O "${dest}/${model}.${ext}" \
"${OMZ_BASE}/${model}/${precision}/${model}.${ext}"
fi
done
}
# Ask for approval before downloading models and sample files
echo ""
echo "This script will download:"
echo " - Model weights and/or sample files"
echo ""
read -p "Continue with downloads? (yes/no): " APPROVAL
if [[ "${APPROVAL}" != "yes" ]]; then
echo "Download cancelled by user."
exit 0
fi
echo ""
echo "--- Downloading person-detection-retail-0013 (FP16) ---"
download_omz_model person-detection-retail-0013 FP16
echo "Ready: person-detection-retail-0013"
echo "--- Downloading person-reidentification-retail-0287 (FP16) ---"
download_omz_model person-reidentification-retail-0287 FP16
echo "Ready: person-reidentification-retail-0287"
echo "--- Downloading sample surveillance video ---"
if [[ ! -f test_video.mp4 ]]; then
wget -q -O test_video.mp4 \
"https://github.com/open-edge-platform/edge-ai-resources/raw/main/videos/VIRAT_S_000101.mp4"
echo "Downloaded: test_video.mp4"
else
echo "Already present: test_video.mp4"
fi
echo "--- Capturing the reference person from the Camera A enrollment window ---"
if [[ ! -f person_a.jpg ]]; then
python3 - <<'PY'
import cv2
import numpy as np
import openvino as ov
DET = "intel/person-detection-retail-0013/FP16/person-detection-retail-0013.xml"
core = ov.Core()
det = core.compile_model(core.read_model(DET), "CPU")
inp = det.input(0)
det_h, det_w = inp.shape[2], inp.shape[3]
def detect_persons(frame, thr=0.6):
"""Return [(x1, y1, x2, y2), ...] for every person detected in the frame."""
h, w = frame.shape[:2]
blob = cv2.resize(frame, (det_w, det_h))
blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
out = det([blob])[det.output(0)][0][0]
boxes = []
for d in out:
if float(d[2]) < thr:
continue
x1 = max(0, int(d[3] * w))
y1 = max(0, int(d[4] * h))
x2 = min(w, int(d[5] * w))
y2 = min(h, int(d[6] * h))
if x2 > x1 and y2 > y1:
boxes.append((x1, y1, x2, y2))
return boxes
# Scan the enrollment window (Camera A, the opening seconds) and capture the
# most prominent person. This person becomes the reference identity that the
# samples re-identify in the later Camera B query window.
cap = cv2.VideoCapture("test_video.mp4")
best = {"crop": None, "area": 0}
for frame_idx in range(0, 300, 15):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
ok, frame = cap.read()
if not ok:
break
for x1, y1, x2, y2 in detect_persons(frame):
area = (x2 - x1) * (y2 - y1)
if area > best["area"]:
best["area"] = area
best["crop"] = frame[y1:y2, x1:x2].copy()
cap.release()
if best["crop"] is None or best["crop"].size == 0:
raise SystemExit("Could not capture a reference person from the enrollment window")
cv2.imwrite("person_a.jpg", best["crop"])
print("Captured person_a.jpg (reference identity from the Camera A enrollment window)")
PY
else
echo "Already present: person_a.jpg"
fi
echo "--- Done ---"
echo "Detector : intel/person-detection-retail-0013/FP16/person-detection-retail-0013.xml"
echo "ReID : intel/person-reidentification-retail-0287/FP16/person-reidentification-retail-0287.xml"
echo "Reference : person_a.jpg (identity captured from the Camera A enrollment window)"
echo "Scene : test_video.mp4 (wide-area surveillance clip split into two virtual cameras)"