时频图数据集更正程序,去除坐标轴白边及调整对应的标签值

当数据集是时频图时可能有一个尴尬的问题,就是数据集制作好后,发现有白边。

其实这也不影响训练模型,可能对模型训练效果的影响也是微乎其微的,于是大多数情况我会选择直接用整张图片训练模型。但是,有的情况下,去掉白边模型训练效果好,不去白边模型某个类别效果就不好。比如图中的BPSK和Frank信号。

一开始我设置416*416的神经网络输入大小,甚至BPSK,Frank信号检测出来的概率特别低,10个中有2个的样子?上面的检测结果是640*640大小训练出来的,虽然信号检测出来的,BPSK,Frank这种特别窄的信号置信度很低,见下图。

这种情况白边就不是可留可不留的了,是必须要去掉。白边占的面积还是挺大的。

裁剪图片并不难,难的是还要对应修正labels中的数值。于是,我写了一个更正程序,既可以裁剪图片,也可以修正labels。

python 复制代码
#作者:zhouzhichao
#时间:25年7月4日
#功能:裁剪数据集的图片和调整对应标签数值

import os
from PIL import Image, ImageDraw

# origin_img_path = ""
# target_img_path = ""
# origin_label_path = ""
# target_label_path = ""

origin_width = 875
origin_height = 656

x1 = 114
x2 = 791
y1 = 49
y2 = 583

after_cut_width = x2 - x1
after_cut_height = y2 - y1

def cut_img(target_path):


    # 设置文件夹路径
    folder_path = target_path
    output_folder = target_path

    # 如果输出文件夹不存在,则创建
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取文件夹中的所有图片
    for filename in os.listdir(folder_path):
        if filename.endswith(('.png', '.jpg', '.jpeg')):  # 根据需要修改图片格式
            img_path = os.path.join(folder_path, filename)
            img = Image.open(img_path)

            # 裁剪区域:左上角(114, 49)到右下角(791, 583)
            cropped_img = img.crop((x1, y1, x2, y2))

            # 保存裁剪后的图片到输出文件夹
            output_path = os.path.join(output_folder, filename)
            cropped_img.save(output_path)

    print("裁剪完成!")

def modify_label(labels_folder):
    output_folder = labels_folder
    for filename in os.listdir(labels_folder):
        if filename.endswith('.txt'):
            txt_path = os.path.join(labels_folder, filename)
            with open(txt_path, 'r') as file:
                lines = file.readlines()

            # 修改后的数据将存储在这里
            modified_lines = []

            for line in lines:
                parts = line.strip().split()  # 拆分每一行
                class_id = parts[0]
                x_center = float(parts[1])
                y_center = float(parts[2])
                width = float(parts[3])
                height = float(parts[4])

                # 修改第2列为0.5,第4列为1
                modified_y_center = (y_center*origin_height-y1)/after_cut_height
                modified_y_height = height*origin_height/after_cut_height


                # 拼接修改后的行
                modified_line = f"{class_id} {0.5:.3f} {modified_y_center:.3f} {1:.3f} {modified_y_height:.3f}\n"
                modified_lines.append(modified_line)

            # 保存修改后的文件
            output_txt_path = os.path.join(output_folder, filename)
            with open(output_txt_path, 'w') as output_file:
                output_file.writelines(modified_lines)

def watch(images_folder,labels_folder,output_folder):
    for filename in os.listdir(labels_folder):
        if filename.endswith('.txt'):
            txt_path = os.path.join(labels_folder, filename)

            # 获取对应的图片文件名
            img_filename = filename.replace('.txt', '.jpg')
            img_path = os.path.join(images_folder, img_filename)

            # 打开图片
            img = Image.open(img_path)
            draw = ImageDraw.Draw(img)

            # 读取标签文件
            with open(txt_path, 'r') as file:
                lines = file.readlines()

            # 遍历每一行标签,绘制矩形框
            for line in lines:
                parts = line.strip().split()  # 拆分每一行
                class_id = int(parts[0])
                x_center = float(parts[1]) * img.width
                y_center = float(parts[2]) * img.height
                width = float(parts[3]) * img.width
                height = float(parts[4]) * img.height

                # 计算矩形框的左上角和右下角坐标
                x1 = x_center - width / 2
                y1 = y_center - height / 2
                x2 = x_center + width / 2
                y2 = y_center + height / 2

                # 绘制矩形框,使用红色边框
                draw.rectangle([x1, y1, x2, y2], outline="white", width=2)

            # 保存带框的图片
            output_img_path = os.path.join(output_folder, img_filename)
            img.save(output_img_path)

if __name__ == "__main__":
    img_path = "D:\english\yolov7\datasets_higher_cut\images\\train"
    label_path = "D:\english\yolov7\datasets_higher_cut\labels\\train"
    output_img_path = "D:\english\yolov7\datasets_higher_cut\watch"
    # cut_img(img_path)
    # modify_label(label_path)
    watch(img_path, label_path, output_img_path)

其中,cut_img是裁剪图片的,modify_label是更正标签的,watch是检测更正结果的。

①裁剪效果:

每张图片原本大小是875*656,把左上角114,49到右下角的791,583,覆盖原图。

②label更正效果,原标签:

更正后标签:

主要是把这些数字中的第2列都改成0.5,第4列改成1,第3列改成原来的数值称原图片高度减去y1后除以裁剪后的图片高度。最后一列改成原值乘以原图高度除以裁剪后的图片高度。

③检查效果

最后,用这个程序需要注意,对一个文件架不能使用两次,那图片不就被裁了2次嘛,label的数值不就改乱了嘛。

相关推荐
张彦峰ZYF14 分钟前
从检索到生成:RAG 如何重构大模型的知识边界?
人工智能·ai·aigc
刘海东刘海东15 分钟前
结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
人工智能·算法·机器学习
**梯度已爆炸**37 分钟前
NLP文本预处理
人工智能·深度学习·nlp
uncle_ll41 分钟前
李宏毅NLP-8-语音模型
人工智能·自然语言处理·语音识别·语音模型·lm
Liudef0641 分钟前
FLUX.1-Kontext 高效训练 LoRA:释放大语言模型定制化潜能的完整指南
人工智能·语言模型·自然语言处理·ai作画·aigc
静心问道43 分钟前
大型语言模型中的自动化思维链提示
人工智能·语言模型·大模型
众链网络1 小时前
你的Prompt还有很大提升
人工智能·prompt·ai写作·ai工具·ai智能体
汀沿河1 小时前
2 大模型高效参数微调;prompt tunning
人工智能·深度学习·prompt
路溪非溪1 小时前
机器学习之线性回归
人工智能·机器学习·线性回归
搞笑的秀儿3 小时前
信息新技术
大数据·人工智能·物联网·云计算·区块链