1、转化前yolo格式数据集路径结构
2、转化后yolo格式数据集路径结构
3、转化脚本(可以直接复制使用)
import os
import argparse
import shutil
def makdirs ( opt, tests_flag) :
images_train = os. path. join( opt. save_root_path, 'images/train' )
images_val = os. path. join( opt. save_root_path , 'images/val' )
label_train = os. path. join( opt. save_root_path, 'labels/train' )
label_val = os. path. join( opt. save_root_path, 'labels/val' )
os. makedirs( images_train, exist_ok= True )
os. makedirs( images_val, exist_ok= True )
os. makedirs( label_train, exist_ok= True )
os. makedirs( label_val, exist_ok= True )
if tests_flag:
images_test = os. path. join( opt. save_root_path, 'images/test' )
label_test = os. path. join( opt. save_root_path, 'labels/test' )
os. makedirs( images_test, exist_ok= True )
os. makedirs( label_test, exist_ok= True )
else :
images_test, label_test= 0 , 0
return images_train, images_val, label_train, label_val, images_test, label_test
def transform_dic ( opt) :
train_img= [ ]
train_label= [ ]
val_img= [ ]
val_label= [ ]
test_img= [ ]
test_label= [ ]
if os. path. exists( os. path. join( opt. ori_root_path, 'train' ) ) :
base_train_images= os. path. join( os. path. join( opt. ori_root_path, 'train' ) , 'images' )
base_train_labels = os. path. join( os. path. join( opt. ori_root_path, 'train' ) , 'labels' )
train_img= [ os. path. join( base_train_images, img_name) for img_name in os. listdir( base_train_images) ]
train_label= [ os. path. join( base_train_labels, label_name) for label_name in os. listdir( base_train_labels) ]
if os. path. exists( os. path. join( opt. ori_root_path, 'valid' ) ) :
base_val_images= os. path. join( os. path. join( opt. ori_root_path, 'valid' ) , 'images' )
base_val_labels = os. path. join( os. path. join( opt. ori_root_path, 'valid' ) , 'labels' )
val_img= [ os. path. join( base_val_images, img_name) for img_name in os. listdir( base_val_images) ]
val_label= [ os. path. join( base_val_labels, label_name) for label_name in os. listdir( base_val_labels) ]
if os. path. exists( os. path. join( opt. ori_root_path, 'test' ) ) :
base_test_images = os. path. join( os. path. join( opt. ori_root_path, 'test' ) , 'images' )
base_test_labels = os. path. join( os. path. join( opt. ori_root_path, 'test' ) , 'labels' )
test_img = [ os. path. join( base_test_images, img_name) for img_name in os. listdir( base_test_images) ]
test_label = [ os. path. join( base_test_labels, label_name) for label_name in os. listdir( base_test_labels) ]
tests_flag = True
else :
tests_flag = False
images_train, images_val, label_train, label_val, images_test, label_test = makdirs( opt, tests_flag)
train_len, val_len, test_len= len ( train_img) , len ( val_img) , len ( test_img)
for train_index in range ( train_len) :
shutil. copy2( train_img[ train_index] , os. path. join( images_train, train_img[ train_index] . split( '\\' ) [ - 1 ] ) )
shutil. copy2( train_label[ train_index] , os. path. join( label_train, train_label[ train_index] . split( '\\' ) [ - 1 ] ) )
print ( os. path. join( images_train, train_img[ train_index] . split( '\\' ) [ - 1 ] ) )
for val_index in range ( val_len) :
shutil. copy2( val_img[ val_index] , os. path. join( images_val, val_img[ val_index] . split( '\\' ) [ - 1 ] ) )
shutil. copy2( val_label[ val_index] , os. path. join( label_val, val_label[ val_index] . split( '\\' ) [ - 1 ] ) )
if len ( images_test) != 0 and len ( label_test) != 0 :
for test_index in range ( test_len) :
shutil. copy2( test_img[ test_index] , os. path. join( images_test, test_img[ test_index] . split( '\\' ) [ - 1 ] ) )
shutil. copy2( test_label[ test_index] , os. path. join( label_test, test_label[ test_index] . split( '\\' ) [ - 1 ] ) )
if __name__ == '__main__' :
parser = argparse. ArgumentParser( )
parser. add_argument( '--ori_root_path' , nargs= '+' , type = str , default= './Roadsyolo' , help = '原始数据' )
parser. add_argument( '--save_root_path' , nargs= '+' , type = str , default= './newyolodatas' , help = '新的数据集保存路径' )
opt = parser. parse_args( )
transform_dic( opt)