#!/bin/bash

usage() {
    echo "Usage: $0 -i <ori_image_directory> -l <ori_label_directory> -w <width_of_pic> -h <height_of_pic> [-help]"
    echo "对image图片和label图片进行处理，将其转为PNG格式，并调整图片的宽和高和格式"
    echo "-i:原始图片的路径，-l:原始标签的路径,-w:图片宽度，-h:图片高度，-help：帮助"
}

ori_image_directorys=""
ori_label_directorys=""
pic_width=1920
pic_height=1080

while getopts "l:i:h:w:" opt; do
    case $opt in
        h)
            if [[ $OPTARG =~ ^-?[0-9]+$ ]];then
                pic_height=$OPTARG
                echo pic_height is $pic_height
            elif [ $OPTARG == 'elp' ];then
                usage
                exit 0
            else
                echo "-h(pic_height)必须为整数"
                usage
                exit 1
            fi
            ;;
        i)
            ori_image_directorys=$OPTARG
            ;;
        l)
            ori_label_directorys=$OPTARG
            ;;
        w)
            if [[ $OPTARG =~ ^-?[0-9]+$ ]];then
                pic_width=$OPTARG
                echo pic_width is $pic_width
            else
                echo "-w(pic_height)必须为整数"
                usage
                exit 1
            fi
            ;;
        *)
            echo  -e '\033[31m!!! Error, Illegal input !!!\033[0m'
            usage
            exit 1
            ;;
    esac
done

# 判断输入地址是否都为空
if [ -z "$ori_label_directorys" ] && [ -z "$ori_image_directorys" ]; then
    echo -e "\033[31m输入地址 -i -l 都为空\033[0m"
    usage
    exit 1
fi

# 地址转化
ori_image_directory=$(readlink -f "$ori_image_directorys")
ori_label_directory=$(readlink -f "$ori_label_directorys")
if [ -z "$ori_label_directory" ] && [ -z "$ori_image_directory" ]; then
    echo "无法解析地址，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directorys"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi
if [ ! -d "$ori_label_directory" ] && [ ! -d "$ori_image_directory" ]; then
    echo "image、label两目录都不存在，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directorys"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi

echo -e "\033[32m_____ 2_reformate_data.sh _____\033[0m"

# 获取当前脚本的路径和名称
script_path=$(dirname "$0")
# 将当前目录更改为脚本所在的路径
cd "$script_path"

# 激活conda环境
source /home/"$USER"/miniconda/bin/activate Deal_pics

# 判断image图片路径是否存在
if [ -d "$ori_image_directory" ]; then 
    echo "**** Processing ori_image_directory: $ori_image_directory ****"
    echo "1.Trans pics to png"
    python 2_1_Trans_to_png.py "$ori_image_directory"
    echo -e ""
    echo "2.Resize image pics with nearest"
    echo -e "\033[35m运行：\033[0mpython 2_2_Resize.py "$ori_image_directory" False $pic_width $pic_height "
    python 2_2_Resize.py "$ori_image_directory" False $pic_width $pic_height # False 是不使用最近邻插值
    echo -e ""
else
    echo "**** image图片目录不存在: $ori_image_directory ****"
    echo -e ""
fi

# 判断label图片路径是否存在
if [ -d "$ori_label_directory" ]; then 
    echo "**** Processing ori_label_directory: $ori_label_directory ****"
    echo -e "\033[33m__ 1.Trans pics to png __\033[0m"
    echo -e "\033[35m运行：\033[0mpython 2_1_Trans_to_png.py "$ori_label_directory""
    python 2_1_Trans_to_png.py "$ori_label_directory"
    echo -e ""
    echo -e "\033[33m__ 2.Resize label pics without nearest __\033[0m"
    echo -e "\033[35m运行：\033[0mpython 2_2_Resize.py "$ori_label_directory" True $pic_width $pic_height"
    python 2_2_Resize.py "$ori_label_directory" True $pic_width $pic_height # True 是使用最近邻插值
    echo -e ""
else
    echo -e "\033[33m**** label图片目录不存在: $ori_image_directory ****\033[0m"
    echo -e ""
fi

source /home/"$USER"/miniconda/bin/deactivate

