YOLO的txt数据筛选:如何挑选出包含固定类别的标签文件和图片

一共分两步:

第一步:在yolo所有的txt标签文件中挑出只包含固定类别的txt标签文件;

第二步:根据标签文件,筛选图片

第一步:

python 复制代码
import os.path
import shutil

# 遍历指定目录
def eachFile(filepath):
    # os.listdir: 该函数返回指定的文件夹包含的文件
    pathdir = os.listdir(filepath)
    for alldir in pathdir:
        child = os.path.join(filepath,alldir)
        if os.path.isfile(child):
            readfile(child)
            continue
        eachFile(child)

# 遍历出结果,返回文件的名字
def readfile(filename):
    f = open(filename, 'r')
    line = f.readline() # 以行的形式进行读取文件
    list1 = []
    while line:
        # line.split('\t''\n'):'\t'和'\n'是两个字符串,会自动进行拼接,形成'\t\n'作为分隔符。
        # 默认把所有的空格、制表符、回车符当作分隔符,并过滤掉空串。
        a = line.split()
        str = a[0]  # 读取改行的第一个数据(类别)
        if str == '0' or '1':
            list.append(filename)
            shutil.copy(filename, 'E:\multyfs2single\label0')  # 要保存的路径
            print("匹配到:" + filename)
        line = f.readline()
    f.close()

if __name__ == "__main__":
    filename = 'E:\multyfs2single\label'
    arr = []
    eachFile(filename)

第二步:

python 复制代码
import os
import shutil

txt_dir = "E:/multyfs2single/label0/"
image_dir = "E:/multyfs2single/img0/"
origin_img_dir = "E:/multyfs2single/img/"

for file in os.listdir(txt_dir):
    # splitext()是用于从后往前切割文件名,[0]:文件名;[1]:后缀
    shutil.copy(origin_img_dir + '/' + os.path.splitext(file)[0] + '.png',
                image_dir + '/' + os.path.splitext(file)[0] + '.png')
相关推荐
宝贝儿好18 小时前
【强化学习】第十章:连续动作空间强化学习:随机高斯策略、DPG算法
人工智能·python·深度学习·算法·机器人
scott19851219 小时前
Improving Classifier-Free Guidance of Flow Matching via Manifold Projection
人工智能·python·机器学习
iOS开发上架19 小时前
系统架构-信息系统
python·腾讯云
Lupino19 小时前
从逻辑“脑裂”到 AI 重构:不到 2 美金解决物联网电位反转难题
python·物联网
前网易架构师-高司机19 小时前
带标注的驾驶员安全带识别数据集,识别率99.5%,可识别有无系安全带,支持yolo,coco json,pascal voc xml格式
xml·yolo·数据集·交通·安全带
青春不败 177-3266-052019 小时前
最新AI赋能Python长时序植被遥感动态分析、物候提取、时空变异归因及RSEI生态评估
人工智能·python·生态学·植被遥感·遥感·物候提取
NGINX开源社区19 小时前
使用 Microsoft Entra ID 配置 NGINX Plus 以实现 SAML SSO
后端·python·flask
小鸡吃米…19 小时前
基于 TensorFlow 的图像识别
人工智能·python·tensorflow
小鸡吃米…20 小时前
TensorFlow - 构建计算图
人工智能·python·tensorflow
cipher20 小时前
crawl4ai:AI时代的数据采集利器——从入门到实战
后端·爬虫·python