#!/usr/bin/env bash
# fetch_models.sh - obtain the pose models for a PLAYER with NO Python / tf2onnx.
#
# Produces (into MODELS_DEST, default tracker-native/models/):
# pose_landmarks_detector.onnx REQUIRED at runtime - kinect-tracker loads
# only this model (ONNX Runtime, see
# src/win_main.cpp kModelName, pose_estimator.cpp).
# pose_landmarks_detector.tflite dev/reference (BlazePose landmark weights).
# pose_detector.tflite dev/reference (ROI detector; tf2onnx cannot
# convert it, so it is not used at runtime).
#
# Pinned, checksummed sources (see README.md "Models & ONNX Runtime acquisition"):
#
# 1) MediaPipe BlazePose "pose_landmarker_full" bundle (Apache-2.0), hosted on
# Google's model storage at a version-pinned URL. A .task file is a plain
# zip, extracted here with `unzip` - no Python.
#
# 2) pose_landmarks_detector.onnx: the project's tf2onnx export of the bundle's
# landmark .tflite, published as a redistributable asset on the project
# mirror. Players fetch this prebuilt .onnx and NEVER run tf2onnx.
# Regenerating it from the .tflite is a DEV-only step (scripts/setup_models.sh,
# needs Python + tf2onnx).
#
# Every artifact's SHA256 is pinned below. Any mismatch (tampering, partial or
# stale download, wrong upstream version) aborts loudly with a non-zero exit.
#
# Overrides (for testing / mirrors):
# MODELS_DEST=
where the models are written (default tracker-native/models)
# TASK_URL= MediaPipe .task bundle
# MODEL_ONNX_URL= prebuilt landmark .onnx (http(s):// or file://)
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MODELS="${MODELS_DEST:-${HERE}/models}"
TASK_URL="${TASK_URL:-https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_full/float16/1/pose_landmarker_full.task}"
TASK_SHA256="5134a3aad27a58b93da0088d431f366da362b44e3ccfbe3462b3827a839011b1"
# Prebuilt landmark ONNX, published as a redistributable asset on the project
# mirror (Apache-2.0). If this asset is not yet published, publish the file whose
# sha256 is ONNX_SHA256 (see MANIFEST.sha256) to the mirror, or point
# MODEL_ONNX_URL at a local copy (file://...). Do NOT fall back to tf2onnx here.
MODEL_ONNX_URL="${MODEL_ONNX_URL:-https://github.com/cga-management/third-party-mirror/releases/download/mediapipe-pose-landmarker-full-1/pose_landmarks_detector.onnx}"
ONNX_SHA256="a92b05663bf998f8353539d0ee0ec1566c1ab598d5457769005b05d659889abf"
TFLITE_LM_SHA256="82be6d591b9dad7d29fe21dc9fd892bf8b9602c458fb05209283de8282a0c488"
TFLITE_DET_SHA256="04f5a96483f8ce5913f8730ad2aa0c4c999c80a4b75653bc0e4712de18e104b2"
need() { command -v "$1" >/dev/null 2>&1 || { echo "FATAL: required tool '$1' not found on PATH" >&2; exit 3; }; }
verify() {
local f="$1" want="$2" label="${3:-$(basename "$1")}" got
got="$(sha256sum "$f" | cut -d' ' -f1)"
if [ "$got" != "$want" ]; then
echo "FATAL: SHA256 mismatch for ${label}" >&2
echo " expected ${want}" >&2
echo " got ${got}" >&2
exit 2
fi
echo "ok: ${label} sha256 verified"
}
need curl; need sha256sum; need unzip
mkdir -p "${MODELS}"
TMP="$(mktemp -d)"
trap 'rm -rf "${TMP}"' EXIT
echo "fetching MediaPipe BlazePose bundle ..."
curl -fSL --retry 3 --connect-timeout 30 -o "${TMP}/pose.task" "${TASK_URL}"
verify "${TMP}/pose.task" "${TASK_SHA256}" "pose_landmarker_full.task"
unzip -o -q "${TMP}/pose.task" -d "${TMP}/ext"
cp -f "${TMP}/ext/pose_landmarks_detector.tflite" "${MODELS}/"
cp -f "${TMP}/ext/pose_detector.tflite" "${MODELS}/"
verify "${MODELS}/pose_landmarks_detector.tflite" "${TFLITE_LM_SHA256}"
verify "${MODELS}/pose_detector.tflite" "${TFLITE_DET_SHA256}"
echo "fetching prebuilt pose_landmarks_detector.onnx ..."
if ! curl -fSL --retry 3 --connect-timeout 30 -o "${TMP}/pose_landmarks_detector.onnx" "${MODEL_ONNX_URL}"; then
echo "FATAL: could not download the prebuilt landmark model from:" >&2
echo " ${MODEL_ONNX_URL}" >&2
echo "If the mirror asset is not published yet, publish the file whose sha256 is" >&2
echo " ${ONNX_SHA256} (see MANIFEST.sha256)" >&2
echo "to the project mirror, or re-run with MODEL_ONNX_URL=file:///path/to/pose_landmarks_detector.onnx" >&2
echo "Do NOT run tf2onnx for the player path - setup_models.sh is DEV-only." >&2
exit 4
fi
verify "${TMP}/pose_landmarks_detector.onnx" "${ONNX_SHA256}" "pose_landmarks_detector.onnx"
cp -f "${TMP}/pose_landmarks_detector.onnx" "${MODELS}/"
echo
echo "models ready in ${MODELS}:"
ls -la "${MODELS}"