吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 328|回复: 3
收起左侧

[求助] YOLO类数据集划分

[复制链接]
CangMing 发表于 2024-4-20 15:58

YOLO类数据集划分代码

import os
import random
import shutil

# 定义文件路径
image_folder = "F:/Data_increase/images"
annotation_folder = "F:/Data_increase/yolo_txt"
train_folder = "F:/Data_increase/train"
train_xml_folder = "F:/Data_increase/train_txt"
val_folder = "F:/Data_increase/val"
val_xml_folder = "F:/Data_increase/val_txt"
test_folder = "F:/Data_increase/test"
test_xml_folder = "F:/Data_increase/test_txt"

# 创建保存文件夹
os.makedirs(train_folder, exist_ok=True)
os.makedirs(train_xml_folder, exist_ok=True)
os.makedirs(val_folder, exist_ok=True)
os.makedirs(val_xml_folder, exist_ok=True)
os.makedirs(test_folder, exist_ok=True)
os.makedirs(test_xml_folder, exist_ok=True)

# 获取所有图像文件的路径
image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg')]

# 随机打乱图像文件列表
random.shuffle(image_files)

# 计算划分数量
total_images = len(image_files)
train_split = int(0.7 * total_images)
val_split = int(0.2 * total_images)

# 划分并复制图像和对应的标注文件
for i, image_file in enumerate(image_files):
    annotation_file = image_file.replace('.jpg', '.txt')

    if i < train_split:
        shutil.copy(os.path.join(image_folder, image_file), os.path.join(train_folder, image_file))
        shutil.copy(os.path.join(annotation_folder, annotation_file), os.path.join(train_xml_folder, annotation_file))
    elif i < train_split + val_split:
        shutil.copy(os.path.join(image_folder, image_file), os.path.join(val_folder, image_file))
        shutil.copy(os.path.join(annotation_folder, annotation_file), os.path.join(val_xml_folder, annotation_file))
    else:
        shutil.copy(os.path.join(image_folder, image_file), os.path.join(test_folder, image_file))
        shutil.copy(os.path.join(annotation_folder, annotation_file), os.path.join(test_xml_folder, annotation_file))

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

dsax7 发表于 2024-4-20 18:04
厉害 厉害
Cateners 发表于 2024-4-20 22:46
 楼主| CangMing 发表于 2024-4-27 11:08
Cateners 发表于 2024-4-20 22:46
楼主的代码整齐得像ChatGPT写的一样

没有哦,这份代码是gpt出世之前写的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 17:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表