YOLO同时对图片和标签进行重命名

import os

image_folder = '/path/to/image/folder'  # 图像文件夹路径
label_folder = '/path/to/label/folder'  # 标签文件夹路径

image_files = sorted(os.listdir(image_folder))  # 获取图像文件列表并排序
label_files = sorted(os.listdir(label_folder))  # 获取标签文件列表并排序

# 确保图像和标签文件数量相同
assert len(image_files) == len(label_files)

# 遍历文件列表
for i in range(len(image_files)):
    old_image_path = os.path.join(image_folder, image_files[i])  # 原始图像文件路径
    old_label_path = os.path.join(label_folder, label_files[i])  # 原始标签文件路径

    new_image_name = f'image_{i}.jpg'  # 新图像文件名
    new_label_name = f'label_{i}.txt'  # 新标签文件名

    new_image_path = os.path.join(image_folder, new_image_name)  # 新图像文件路径
    new_label_path = os.path.join(label_folder, new_label_name)  # 新标签文件路径

    # 重命名图像文件
    os.rename(old_image_path, new_image_path)

    # 重命名标签文件
    os.rename(old_label_path, new_label_path)

你可能感兴趣的:(经验类,YOLO)