在Python中批量复制图像会创建空文件

问题描述 投票:0回答:1

我正在为 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

我使用了以下方法:

  1. os.system("cp src dest")
  2. shutil.copy(" src dest")

我的期望:

  • 所有图像和各自的注释文件都复制到各自的文件夹中。

发生了什么:

  • 仅复制一些尺寸合适且可以使用图像查看器应用程序查看的图像。大多数图像文件大小只有 38 kb 并且是空的。
python linux shutil os.system
1个回答
0
投票

代码中的错误在这里突出显示:

    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
文件上运行。

© www.soinside.com 2019 - 2024. All rights reserved.