我正在为 Yolo 模型训练创建数据集。目前,多个视频的帧和注释文件存在于其单独的文件夹中。
这是我编写的用于从不同文件夹中分割图像和注释文件的代码。
test/val 文件夹中要添加的图像是随机选择并移动的。 我尝试了各种复制方法(包括
copy2
、copyfileobj
中的shutils
等),但只有少数图像被正确复制,其余都是大小为38kb的空文件。
#Assume other libraries are imported
for video_folder in video_folders:
os.chdir(os.path.join(video_folder, "obj_train_data"))
print(f"Currently in {os.getcwd()}")
sample_files = os.listdir("./")
for image_file in sample_files:
label_file = os.path.join(str(image_file[:-3] + "txt"))
print(f"Image file name = {image_file}")
print(f"label file name = {label_file}")
#Generate full path.
image_file = os.path.join(os.getcwd(), image_file)
label_file = os.path.join(os.getcwd(), label_file)
assert(os.path.exists(image_file))
assert(os.path.exists(label_file))
#rename the iamge and label file to avoid overwriting
new_image_file_name = f"frame_{sample_moved}.PNG"
new_label_file_name = f"frame_{sample_moved}.txt"
# Copy the background file
if not (os.path.exists(label_file)):
os.system(f"cp {image_file} {os.path.join(__IMAGE_TRAIN_FOLDER__,new_image_file_name)}")
continue
if(sample_moved == random_indices[0]):
try:
random_indices.pop(0)
except IndexError as e:
print("array is empty!!")
#Move file to test folder
if(test_counter < max_test_samples_count):
print("moving to test folder")
os.system(f"cp {image_file} {os.path.join(__IMAGE_TEST_FOLDER__,new_image_file_name)}")
os.system(f"cp {label_file} {os.path.join(__LABEL_TEST_FOLDER__,new_label_file_name)}")
sleep(0.1)
test_counter+=1
#Move file to validation counter
elif(val_counter < max_val_samples_count):
print("moving to validation folder")
os.system(f"cp {image_file} {os.path.join(__IMAGE_VAL_FOLDER__,new_image_file_name)}")
os.system(f"cp {label_file} {os.path.join(__LABEL_VAL_FOLDER__,new_label_file_name)}")
sleep(0.1)
val_counter+=1
else:
print("Moving to training folder")
os.system(f"cp {image_file} {os.path.join(__IMAGE_TRAIN_FOLDER__,new_image_file_name)}")
os.system(f"cp {label_file} {os.path.join(__LABEL_TRAIN_FOLDER__,new_label_file_name)}")
sleep(0.1)
sample_moved+=1
# Move to base directory.
os.chdir("../../")
print(f"Total samples moved = {sample_moved}")
以下是目录结构:
-Images
-Labels
-video_1
-obj_train_data
-video_2
--obj_train_data
-video_3
--obj_train_data
我使用了以下方法:
os.system("cp src dest")
shutil.copy(" src dest")
我的期望:
发生了什么:
print(f"Currently in {os.getcwd()}")
sample_files = os.listdir("./")
这会生成目录中“所有”文件的列表,其中还包括
.txt
文件。然后,这些文件稍后会在代码中复制为图像文件。
仅使用以下方法创建图像列表:
print(f"Currently in {os.getcwd()}")
#create a list of all the images in the folder.
sample_files = natsort.natsorted(img for img in os.listdir("./") if img.endswith(".PNG"))
这将确保循环仅在图像上运行,而不是在
.txt
文件上运行。