比如一个yolo格式的txt标签数据包含多个类别,现在需要将多类别区分训练,就需要将一个txt文件拆分成多个txt文件,python代码实现如下:
import os
import numpy as np
import shutil
img_path = '/train_images_labels_detection/labels/train'
fire_path= '/train_images_labels_detection/dataset1/labels/train'
smoke_path= '/train_images_labels_detection/dataset2/labels/train'if not os.path.exists(fire_path):os.mkdir(fire_path)
if not os.path.exists(smoke_path):os.mkdir(smoke_path)label_list = os.listdir(label_path)
for i in label_list:txtpath = os.path.join(label_path, i)with open(txtpath, 'r', encoding='utf-8') as file:fire = []smoke = []for line_num, line in enumerate(file, start=1):line = line.strip()if line:# 判断第一个字符,这里就列举了2类,此处可以根据自己数据集类别进行补充修改if line[0] == '0':fire.append(line)else:smoke.append(line)if len(fire)!=0:with open(os.path.join(fire_path,i), 'w', encoding='utf-8') as file1:for item in fire:file1.write(item + '\n')if len(smoke)!=0:with open(os.path.join(smoke_path,i), 'w', encoding='utf-8') as file2:for item in smoke:file2.write(item + '\n')
代码解释:
img_path:所有多类别标签数据地址,比如包含了火和烟2种类别,现在需要区分2者
fire_path:输出火的标签地址
smoke_path:输出烟的标签地址