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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 将图片和标注数据按比例切分为 训练集和测试集
 
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()

  

posted @   yx啦啦啦  阅读(2302)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2020-03-03 pip 升级或者安装拓展包时遇见的问题
欢迎这位客官来到《YOLOV5——将图片和标注数据按比例切分为训练集和测试集 - yx啦啦啦 - 博客园》
点击右上角即可分享
微信分享提示