#!/bin/bash
# Example script for running YOLOv5-3D evaluation
# 
# Usage: ./eval_tools/run_evaluation_example.sh

# =============================================================================
# Configuration
# =============================================================================

# Paths (modify these according to your data location)


# Set PYTHONPATH to project root for module imports
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
export PYTHONPATH="${PROJECT_ROOT}:${PYTHONPATH}"
GT_PATH="/data1/xdzhu/Testdata_0129"

OUTPUT_DIR="eval_results/mono3d/$(date +%Y%m%d_%H%M%S)"

# Image properties
IMG_WIDTH=1920
IMG_HEIGHT=1080

# Evaluation parameters
IOU_THRESHOLD=0.5
CONF_THRESHOLD=0.5
AP_METHOD="voc2010"  # or "coco"

DET_PATH="/data1/dongying/Mono3d/G1M3/inference_results/mono3d/evalset_roi0"

# Distance ranges for 3D evaluation (optional)
# Comment out to disable distance-based statistics
ENABLE_DISTANCE_RANGES=true
DISTANCE_RANGES="[[0, 30], [30, 60], [60, 100], [100, 999]]"

# =============================================================================
# Prepare Configuration
# =============================================================================

# Create temporary config file
CONFIG_FILE="/tmp/eval_config_$$.yaml"

if [ "$ENABLE_DISTANCE_RANGES" = true ]; then
    cat > "$CONFIG_FILE" << EOF
dataset:
  det_path: "$DET_PATH"
  gt_path: "$GT_PATH"

image:
  width: $IMG_WIDTH
  height: $IMG_HEIGHT

matching:
  iou_threshold: $IOU_THRESHOLD

metrics_2d:
  enabled: true
  conf_threshold: $CONF_THRESHOLD
  ap_method: "$AP_METHOD"

metrics_3d:
  enabled: true
  distance_ranges:
    - [0, 30]
    - [30, 60]
    - [60, 100]
    - [100, 999]

output:
  save_path: "$OUTPUT_DIR"
EOF
else
    cat > "$CONFIG_FILE" << EOF
dataset:
  det_path: "$DET_PATH"
  gt_path: "$GT_PATH"

image:
  width: $IMG_WIDTH
  height: $IMG_HEIGHT

matching:
  iou_threshold: $IOU_THRESHOLD

metrics_2d:
  enabled: true
  conf_threshold: $CONF_THRESHOLD
  ap_method: "$AP_METHOD"

metrics_3d:
  enabled: true

output:
  save_path: "$OUTPUT_DIR"
EOF
fi

# =============================================================================
# Run Evaluation
# =============================================================================

echo "=========================================="
echo "YOLOv5-3D Model Evaluation"
echo "=========================================="
echo "Detection path: $DET_PATH"
echo "Ground truth path: $GT_PATH"
echo "Output directory: $OUTPUT_DIR"
if [ "$ENABLE_DISTANCE_RANGES" = true ]; then
    echo "Distance ranges: ENABLED (0-30m, 30-60m, 60-100m, 100-999m)"
else
    echo "Distance ranges: DISABLED"
fi
echo "=========================================="

# Check if paths exist
if [ ! -d "$DET_PATH" ]; then
    echo "Error: Detection path does not exist: $DET_PATH"
    exit 1
fi

if [ ! -d "$GT_PATH" ]; then
    echo "Error: Ground truth path does not exist: $GT_PATH"
    exit 1
fi

# Run evaluation
python eval_tools/core/eval.py \
    --config "$CONFIG_FILE"

# Check if evaluation was successful
if [ $? -eq 0 ]; then
    echo ""
    echo "✓ Evaluation completed successfully!"
    echo "Results saved to: $OUTPUT_DIR"
    echo ""
    echo "View results:"
    echo "  JSON: $OUTPUT_DIR/evaluation_report.json"
    echo "  Text: $OUTPUT_DIR/evaluation_report.txt"
    
    # Cleanup
    rm -f "$CONFIG_FILE"
else
    echo ""
    echo "✗ Evaluation failed!"
    rm -f "$CONFIG_FILE"
    exit 1
fi
