当前位置:   article > 正文

深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查_splitlabels

splitlabels

深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查

一、前言

最近在搞讯飞小车竞赛,完成视觉目标检测的时候,突然用到了这个,所以就写了着个小demo完成一下任务

二、源码

功能:

  • 对数据集的拆分
  • 根据拆分图片拆分labels、
  • 对全部标注标签进行区间检查
import os
import random
import shutil

def get_imlist(path):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jpg')]

def getData(src_path,k):

    dest_dir = src_path+'val' #划分出来的验证集
    if not os.path.isdir(dest_dir):
        os.mkdir(dest_dir)

    img_list = get_imlist(src_path)
    random.shuffle(img_list)
    le = int(len(img_list) * k)  # 这个可以修改划分比例
    for f in img_list[le:]:
        shutil.move(f, dest_dir)

'''
函数功能:
划分数据集
'''
def SplitImg(filePath,k):
    #拆分的数据集
    getData(filePath,k)

'''
函数功能:
根据划分的数据集进行移动标注文件
'''
def MoveAn(filePathAn,filePathImg):

    if not os.path.isdir(filePathAn+'val'):
        os.mkdir(filePathAn+'val')
    Imgs=os.listdir(filePathImg)


    for file in os.listdir(filePathAn):
        #print(filePathAn,filePathImg)
        #print(os.path.join(filePathAn,file),os.path.join(filePathAn+'val',file))
        if file[:-4]+'.jpg' in Imgs:

            shutil.move(os.path.join(filePathAn,file),os.path.join(filePathAn+'val',file))

'''
函数功能:
去除重复的图片
'''
def delReDisplayImg(filePath):
    Imgs=os.listdir(filePath)
    for img in Imgs:
        if Imgs.count(img)>1:
            os.remove(filePath+'/'+img)
            print(filePath+'/'+img)

'''
函数功能:
检查标签是否都符合

'''
def checkAn(filePath,lim):
    Ans=os.listdir(filePath)
    dels=[]#将找到的不合法的文件弄到这个文件夹里面

    for An in Ans:
        with open(os.path.join(filePath,An),'r',encoding='utf-8')as f:
            while True:
                line=f.readline()
                if line=='':
                    break
                #print(An,line)
                nc=int(line[0:2])
               #print(nc)

                #print(nc)
                if nc<lim[0] or nc>lim[1]:
                    print(nc,os.path.join(filePath,An))
                    dels.append(os.path.join(filePath,An))
                    #os.remove(os.path.join(filePath,An)

    print('是否删除以下异常文件?y/n')
    print(dels)
    op=input('执行操作:')
    if op=='y':
        for path in dels:
            os.remove(path)

if __name__=='__main__':
    filePath = 'D:\AI\yolov5-master\\xunfeidata\images\\train'  # 换成你的数据集
    #划分比例
    k=0.8
    #SplitImg(filePath,k)

    filePathAn = 'D:\AI\yolov5-master\\xunfeidata\labels\\train'  # 换成你的标注文件地址
    # 根据数据集进行移动标注文件
    MoveAn(filePathAn,filePath+'val')
    checkAn(filePathAn,[0,7])
  • 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
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/684665
推荐阅读
相关标签
  

闽ICP备14008679号