import os
import shutil
input_dir = "dataset"
output_dir = "output"
matching_folder = "matched_files"
unmatching_folder = "unmatched_files"
file_names_file = os.path.join(input_dir, "file_names.txt")
# 创建输出文件夹
output_matching_dir = os.path.join(output_dir, matching_folder)
output_unmatching_dir = os.path.join(output_dir, unmatching_folder)
os.makedirs(output_matching_dir, exist_ok=True)
os.makedirs(output_unmatching_dir, exist_ok=True)
# 读取文件名列表
with open(file_names_file, "r") as f:
file_names = [line.strip() for line in f]
# 遍历文件名列表
for file_name in file_names:
image_path = os.path.join(input_dir, "images", file_name + ".jpg")
label_path = os.path.join(input_dir, "labels", file_name + ".txt")
# 如果图片和标签文件都存在,将它们移动到匹配文件夹
if os.path.exists(image_path) and os.path.exists(label_path):
shutil.copyfile(image_path, os.path.join(output_matching_dir, file_name + ".jpg"))
shutil.copyfile(label_path, os.path.join(output_matching_dir, file_name + ".txt"))
else:
print(f"未找到匹配文件: {file_name}")
print("文件移动完成!")
根据.txt文件读取文件名(无后缀)相同的图片(.jpg)和标签(.txt)并保存到相应的文件夹里
最新推荐文章于 2024-09-09 23:28:21 发布