• YOLOV5——将图片和标注数据按比例切分为训练集和测试集


    将图片和标注数据按比例切分后存储至新的路径下

    # 将图片和标注数据按比例切分为 训练集和测试集
    
    import os
    from shutil import copy2
    # 原始路径
    image_original_path = "../image_data/seed/images/"
    label_original_path = "../image_data/seed/labels/"
    # 上级目录
    parent_path = os.path.dirname(os.getcwd())
    # 训练集路径
    train_image_path = os.path.join(parent_path, "image_data/seed/train/images/")
    train_label_path = os.path.join(parent_path, "image_data/seed/train/labels/")
    # 测试集路径
    test_image_path = os.path.join(parent_path, 'image_data/seed/test/images/')
    test_label_path = os.path.join(parent_path, 'image_data/seed/test/labels/')
    
    # 检查文件夹是否存在
    def mkdir():
        if not os.path.exists(train_image_path):
            os.makedirs(train_image_path)
        if not os.path.exists(train_label_path):
            os.makedirs(train_label_path)
    
        if not os.path.exists(test_image_path):
            os.makedirs(test_image_path)
        if not os.path.exists(test_label_path):
            os.makedirs(test_label_path)
    
    
    def main():
        mkdir()
        # 复制移动图片数据
        all_image = os.listdir(image_original_path)
        for i in range(len(all_image)):
            if i % 10 != 0:
                copy2(os.path.join(image_original_path, all_image[i]), train_image_path)
            else:
                copy2(os.path.join(image_original_path, all_image[i]), test_image_path)
    
        # 复制移动标注数据
        all_label = os.listdir(label_original_path)
        for i in range(len(all_label)):
            if i % 10 != 0:
                copy2(os.path.join(label_original_path, all_label[i]), train_label_path)
            else:
                copy2(os.path.join(label_original_path, all_label[i]), test_label_path)
    
    
    if __name__ == '__main__':
        main()
    

      

  • 相关阅读:
    pyautogui页面点击和键盘输入
    eclipse把函数内容折叠的方法
    python 文件夹下文件及文件夹名称获取
    python中strftime和strptime函数
    python数组中在某一元素前插入数据
    python的递归函数
    java定义时间
    如何在word中插入代码块
    【图文详解】用Eclipse创建Maven Web项目
    【图文详解】Eclipse中添加Tomcat服务器
  • 原文地址:https://www.cnblogs.com/yxyun/p/14474843.html
Copyright © 2020-2023  润新知