import os import shutil import pandas as pd

读取train.csv文件

train_df = pd.read_csv('train.csv')

遍历train文件夹中的所有图片

for image_name in os.listdir('train'): # 获取图片id image_id = image_name.split('.')[0]

# 根据图片id的前三个字符创建子文件夹路径
subfolder_path = os.path.join('train', image_id[0], image_id[1], image_id[2])

# 创建子文件夹路径(如果不存在)
os.makedirs(subfolder_path, exist_ok=True)

# 移动图片到对应的子文件夹中
shutil.move(os.path.join('train', image_name), os.path.join(subfolder_path, image_name))

# 获取对应图片的标签
label = train_df[train_df['id'] == image_id]['landmark_id'].values[0]

# 将图片标签写入对应的子文件夹中的label.txt文件
with open(os.path.join(subfolder_path, 'label.txt'), 'w') as f:
    f.write(str(label)
kaggle竞赛的数据组织形式描述如下你能写个python帮我把我的imagenet格式的数据修改为下面这种格式吗?In this competition you are asked to take test images and recognize which landmarks if any are depicted in them The training set is available

原文地址: https://www.cveoy.top/t/topic/iTm8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录