#!/bin/bash

# run-hopper-example.sh
#
# Copyright (c) 2025 Technologic Systems, Inc. (dba embeddedTS)

# We'll fetch and prepare (with vela) the model referenced by
# the Grace Hopper inference_runner example from the i.MX Machine
# Learning User's Guide.

SOURCE_DEST="$HOME/src/NPU-$(uname -r)"

if ! [ -x /usr/local/bin/ethosu/examples/inference_runner ] ; then
    echo "First run install-npu-software.sh" >&2
    exit 1
fi

if ! [ -e ${SOURCE_DEST}/tensorflow-imx ] ; then
    echo "The latest install-npu-software.sh must be run for any new kernel." >&2
    exit 2
fi

# ==================== Install model needed by demo ====================

if ! [ -e ${SOURCE_DEST}/models ] ; then
    mkdir ${SOURCE_DEST}/models
    pushd ${SOURCE_DEST}/models
    wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_1.0_224_quant_and_labels.zip
    unzip mobilenet_v1_1.0_224_quant_and_labels.zip
    . ${SOURCE_DEST}/env/bin/activate
    vela mobilenet_v1_1.0_224_quant.tflite
    # ${SOURCE_DEST}/models/output will now contain the converted model
    popd
fi

# ==================== Demo ====================

/usr/local/bin/ethosu/examples/inference_runner \
    -n ${SOURCE_DEST}/models/output/mobilenet_v1_1.0_224_quant_vela.tflite \
    -i ${SOURCE_DEST}/tensorflow-imx/tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp \
    -l ${SOURCE_DEST}/tensorflow-imx/tensorflow/lite/examples/ios/camera/data/labels.txt \
    -o output.txt
