#!/usr/bin/env bash
# 对单个 case 目录批量进行 GT 可视化
# 用法:
#   bash tools/scripts_for_gt/visualization/visualize_batch_case.sh [SAMPLE_INTERVAL] [CLASSES]
# 例:
#   bash tools/scripts_for_gt/visualization/visualize_batch_case.sh
#   bash tools/scripts_for_gt/visualization/visualize_batch_case.sh 5 car
#   bash tools/scripts_for_gt/visualization/visualize_batch_case.sh 1 car,pedestrian
#   OUTPUT_DIR=/tmp/vis_car bash tools/scripts_for_gt/visualization/visualize_batch_case.sh 1 car

set -euo pipefail

IMAGE_DIR="/data1/dongying/Mono3d/G1Q3/dataset_for_evaluation/DL_KPI_SCENE/019b6aa1-a8ed-7029-8b8c-68311394dade/images"
LABEL_DIR="/data1/dongying/Mono3d/G1Q3/dataset_for_evaluation/DL_KPI_SCENE/019b6aa1-a8ed-7029-8b8c-68311394dade/labels_json"
DEFAULT_OUTPUT_DIR="/data1/dongying/Mono3d/G1Q3/dataset_for_evaluation/DL_KPI_SCENE/019b6aa1-a8ed-7029-8b8c-68311394dade/vis_json_car_suv"
FILE_EXTENSION=".png"
SAMPLE_INTERVAL="${1:-1}"
TARGET_CLASSES="${2:-${TARGET_CLASSES:-car,suv}}"

sanitize_for_path() {
    local value="$1"
    value="${value//,/__}"
    value="${value// /}"
    value="${value//\//_}"
    value="${value//:/_}"
    printf '%s' "$value"
}

if [[ -n "${OUTPUT_DIR:-}" ]]; then
    FINAL_OUTPUT_DIR="${OUTPUT_DIR}"
elif [[ -n "${TARGET_CLASSES}" ]]; then
    FINAL_OUTPUT_DIR="${DEFAULT_OUTPUT_DIR}_$(sanitize_for_path "${TARGET_CLASSES}")"
else
    FINAL_OUTPUT_DIR="${DEFAULT_OUTPUT_DIR}"
fi

echo "image_dir:        ${IMAGE_DIR}"
echo "label_dir:        ${LABEL_DIR}"
echo "output_dir:       ${FINAL_OUTPUT_DIR}"
echo "sample_interval:  ${SAMPLE_INTERVAL}"
if [[ -n "${TARGET_CLASSES}" ]]; then
    echo "target_classes:   ${TARGET_CLASSES}"
else
    echo "target_classes:   all"
fi

PYTHON_ARGS=(
    python tools/scripts_for_gt/visualization/visualize_batch.py
    --image-dir "${IMAGE_DIR}"
    --label-dir "${LABEL_DIR}"
    --output "${FINAL_OUTPUT_DIR}"
    --file-extension "${FILE_EXTENSION}"
    --sample-interval "${SAMPLE_INTERVAL}"
)

if [[ -n "${TARGET_CLASSES}" ]]; then
    PYTHON_ARGS+=(--classes "${TARGET_CLASSES}")
fi

"${PYTHON_ARGS[@]}"
