File size: 4,011 Bytes
ee6192f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# Download face detection and re-identification models from Open Model Zoo
# for the face-matching use case.
# 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 face-detection-adas-0001 (FP16) ---"
download_omz_model face-detection-adas-0001 FP16
echo "Ready: face-detection-adas-0001"

echo "--- Downloading face-reidentification-retail-0095 (FP16) ---"
download_omz_model face-reidentification-retail-0095 FP16
echo "Ready: face-reidentification-retail-0095"

echo "--- Downloading sample test video ---"
if [[ ! -f test_video.mp4 ]]; then
    wget -q -O test_video.mp4 \
        "https://github.com/intel-iot-devkit/sample-videos/raw/master/face-demographics-walking-and-pause.mp4"
    echo "Downloaded: test_video.mp4"
else
    echo "Already present: test_video.mp4"
fi

echo "--- Capturing the reference face of the left subject from the video ---"
if [[ ! -f face_a.jpg ]]; then
    python3 - <<'PY'
import cv2
import numpy as np
import openvino as ov

DET = "intel/face-detection-adas-0001/FP16/face-detection-adas-0001.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 left_face_portrait(frame, thr=0.6):
    """Return a head-and-shoulders crop of the left-most detected face."""
    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 = [(int(d[3] * w), int(d[4] * h), int(d[5] * w), int(d[6] * h))
             for d in out if float(d[2]) >= thr]
    if not boxes:
        return None
    x1, y1, x2, y2 = min(boxes, key=lambda b: (b[0] + b[2]) / 2)  # left-most
    bw, bh = x2 - x1, y2 - y1
    cx1, cx2 = max(0, int(x1 - 0.6 * bw)), min(w, int(x2 + 0.6 * bw))
    cy1, cy2 = max(0, int(y1 - 0.7 * bh)), min(h, int(y2 + 1.6 * bh))
    return frame[cy1:cy2, cx1:cx2]


# During the 37-42s segment a woman pauses on the left of the frame; capture
# a head-and-shoulders portrait of her as the reference face to search for.
cap = cv2.VideoCapture("test_video.mp4")
cap.set(cv2.CAP_PROP_POS_FRAMES, 468)
ok, frame = cap.read()
crop = left_face_portrait(frame) if ok else None
if crop is None or crop.size == 0:
    raise SystemExit("Could not capture a reference face at frame 468")
cv2.imwrite("face_a.jpg", crop)
print("Captured face_a.jpg from frame 468")
cap.release()
PY
else
    echo "Already present: face_a.jpg"
fi

echo "--- Done ---"
echo "Detector  : intel/face-detection-adas-0001/FP16/face-detection-adas-0001.xml"
echo "ReID      : intel/face-reidentification-retail-0095/FP16/face-reidentification-retail-0095.xml"
echo "Reference : face_a.jpg (left subject captured from the sample video)"
echo "Scene     : test_video.mp4 (contains the reference subject plus others)"