zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    U盘启动盘恢复为普通盘
    TP框架模板中ifelse
    TP框架中多条件筛选
    日期选择器:jquery datepicker的使用
    配置本地环境,让内网可访问
    FlexSlider插件的详细设置参数
    CentOS+Nginx+PHP+MySQL详细配置(图解)
    Linux 服务器环境启动
    javascript自定义浏览器右键菜单
    强大实用的jQuery幻灯片插件Owl Carousel
  • 原文地址:https://www.cnblogs.com/yxyun/p/14474843.html
Copyright © 2011-2022 走看看