Human3.6m数据集预处理的一个小步骤
2023-12-26 18:12:52
根据论文提供的源码learnable-triangulation-pytorch
After that, you should have images unpacked as e.g.?$THIS_REPOSITORY/data/human36m/processed/S1/Phoning-1/imageSequence/54138969/img_000001.jpg
.
步骤二需要的是将图片按照Subject、camera、action这几个标签放到文件夹下。
而从网上下载的Human3.6m图片数据集是这样的,就是给出了7个Subject,并且用以下三个作为图片名称的代表。
S1_Directions.54138969_000001.jpg
S1_SittingDown_2.55011271_001511.jpg
S1_Directions_1.54138969_000001.jpg
subject+action+占位符+相机名称+帧数
为了实现论文Readme的要求撰写以下代码用于文件的分类复制
import os
import shutil
import numpy as np
import pandas as pd
#存放方式:S1...这些文件夹下有一个总的图片存放文件
#任务1:获取所有文件名,并将文件名拆分,并按动作和分隔符+相机名称进行分类,并存放到对应文件夹下
#例如S1_Directions_1.54138969_000001.jpg,将被存到 ..//S1//Directions_1//imageSequence//54138969
#任务2:对每个动作中的文件进行重命名img_+“编号”的形式
retval = {
'subject_names': ['S1', 'S5', 'S6', 'S7', 'S8', 'S9', 'S11'],
'camera_names': ['54138969', '55011271', '58860488', '60457274'],
'action_names': [
'Directions-1', 'Directions-2',
'Discussion-1', 'Discussion-2',
'Eating-1', 'Eating-2',
'Greeting-1', 'Greeting-2',
'Phoning-1', 'Phoning-2',
'Posing-1', 'Posing-2',
'Purchases-1', 'Purchases-2',
'Sitting-1', 'Sitting-2',
'SittingDown-1', 'SittingDown-2',
'Smoking-1', 'Smoking-2',
'TakingPhoto-1', 'TakingPhoto-2',
'Waiting-1', 'Waiting-2',
'Walking-1', 'Walking-2',
'WalkingDog-1', 'WalkingDog-2',
'WalkingTogether-1', 'WalkingTogether-2']
}
#S1_Directions.54138969_000001
#子集+动作+数据编号+图片
root_path = "E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed"
#起始地址
float_path = "E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\{}\\all".format(retval['subject_names'][6])
## 目标地址
DEST_path = "E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\{}\\{}\\imageSequence\\{}"
# float_path_1 = "E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\{}\\{}\\imageSequence\\{}".format(retval['action_names'][0], retval['camera_names'][0])
# float_path_2 = "E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\S1\\{}\\imageSequence\\{}".format(retval['action_names'][2*action_number+1])
# print(float_path_1)
#公共地址
new_name_prefix = "img_"
img_name = []
#把S1中的4个相机里面的图片挨个读一遍
# def path_read(i):
# data_path = os.path.join(float_path, retval['camera_names'][i])
# #很神奇,加上这句折后返回值直接变成文件了
# # data_path = os.listdir(data_path)
# return data_path
#
###############获取多个文件的文件名
# def get_image_filenames(folder_paths):
# #放整个名称
# df_name = pd.DataFrame(columns=['ImageName'])
# #放细节
# df_detail = pd.DataFrame(columns=['Subject', 'Action_', '_', 'Camera', 'FrameNumber'])
# # 获取文件夹下的所有图片文件名
# image_names = [name for name in os.listdir(folder_paths) if name.lower().endswith('.jpg')]
# # 将数据添加到 DataFrame
# data = {'ImageName': image_names}
# df_name = df_name.append(pd.DataFrame(data), ignore_index=True)
# for index, row in df_name.iterrows():
# image_name = row['ImageName']
# if image_name.endswith('.jpg'):
# #计算下划线的个数区分是1还是0号数据集中的数据(3是1,2是0)
# # 去除尾缀
# image_name = image_name.rstrip('.jpg')
# underscore_count = image_name.count('_')
# if underscore_count == 2:
# #此时占位符为0
# _ = '0'
# # 按点进行分割
# segments = image_name.split('.')
# # 提取前两个片段
# subject_action, camera_frame = segments[:2]
# # 再按下划线进行分割,提取相机和帧编号
# subject, action = subject_action.split('_')
# # 再按点进行分割,提取相机和帧编号
# camera, frame_number = camera_frame.split('_')
# Action_ = '-'.join([action, _])
# # 将结果添加到 DataFrame
# df_detail = df_detail.append({'Subject': subject, 'Action_': Action_, '_': _, 'Camera': camera, 'FrameNumber': frame_number},
# ignore_index=True)
# else:
# #下划线和点来分割文件名
# segments = image_name.split('_')
# # 提取前四个片段
# subject, action, camera_, frame_number = segments[:4]
# _, camera = camera_.split('.')
# Action_ = '-'.join([action, _])
# # 将结果添加到 DataFrame
# df_detail = df_detail.append({'Subject': subject, 'Action_': Action_, '_': _, 'Camera': camera, 'FrameNumber': frame_number},
# ignore_index=True)
# Action, Camera = df_detail['Action_'], df_detail['Camera']
# return Action, Camera, _
####获取单个文件的文件名
def get_image_filename(folder_paths):
#basename就是获取'E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\S1\\all\\11\\S1_Directions.54138969_000001.jpg'中的S1_Directions.54138969_000001.jpg
image_name = os.path.basename(folder_paths)
# 将数据添加到 DataFrame
#计算下划线的个数区分是1还是0号数据集中的数据(3是1,2是0)
# 去除尾缀
image_name = image_name.rstrip('.jpg')
underscore_count = image_name.count('_')
if underscore_count == 2:
#此时占位符为0
_ = '0'
# 按点进行分割
segments = image_name.split('.')
# 提取前两个片段
subject_action, camera_frame = segments[:2]
# 再按下划线进行分割,提取相机和帧编号
subject, action = subject_action.split('_')
# 再按点进行分割,提取相机和帧编号
camera, frame_number = camera_frame.split('_')
Action_ = '-'.join([action, _])
# 将结果添加到 DataFrame
else:
#下划线和点来分割文件名
segments = image_name.split('_')
# 提取前四个片段
subject, action, camera_, frame_number = segments[:4]
_, camera = camera_.split('.')
Action_ = '-'.join([action, _])
# 将结果添加到 DataFrame
return Action_, camera, _, subject
#单个动作的移动
# def copy_images(source_root, dest_root, camera_folder,action_name):
def copy_images(source_root, dest_root,img_names, num):
#当源路径是多个相机的根时候这样搞,还不行~
# source_folder = [os.path.join(source_root, folder) for folder in camera_folder]
# dest_folder = [os.path.join(dest_root, folder) for folder in camera_folder]
# source_folder = source_root
# dest_folder = dest_root
# img_name_S1, img_detail_S1 = get_image_filenames(source_folder)
# image_files = [f for f in os.listdir(source_folder) if f.endswith('.jpg')]
# # # 创建目标文件夹
# # os.makedirs(dest_folder, exist_ok=True)
#
# # # 获取源文件夹下所有的图片文件名
# # image_files = [f for f in os.listdir(source_folder) if f.endswith('.jpg')]
#
# # 复制对应动作图片到目标文件夹
# source_path = os.path.join(source_folder, image_file)
# dest_path = os.path.join(dest_folder, image_file)
for time in range(len(num)):
source_path = os.path.join(source_root, img_names["ImageName"][num[time]])
# dest_path = os.path.join(dest_root[0], img_names["ImageName"][num[time]])
dest_path = os.path.join(dest_root, img_names["ImageName"][num[time]])
shutil.copyfile(source_path, dest_path)
# for index in img_detail:
# if img_detail['Action_'] == retval['action_names'][0]:
# i = img_detail.index[img_detail['Action_'] == retval['action_names'][0]].tolist()
# dest_path = DEST_path.format(retval['subject_names'][0], retval['action_names'][0],
# retval['camera_names'][0])
#
# source_path = os.path.join(source_root, img_names[i])
# dest_path = os.path.join(dest_root[0], img_names[i])
# dest_paths.append(dest_path)
# source_paths.append(source_path)
# print(i)
# print(index)
# print(source_paths)
# print(dest_paths)
# shutil.copyfile(source_root, dest_root)
# def rename(folder_path, prefix_length):
# folder_path = os.path.join(base_folder, folder)
def classify_and_pack(folder_path, prefix_length):
for filename in os.listdir(folder_path):
if filename.endswith('.jpg'):
# 获取前缀部分
prefix = filename[:prefix_length]
# 创建目标文件夹
target_folder = os.path.join(folder_path, prefix)
os.makedirs(target_folder, exist_ok=True)
# 移动文件
source_path = os.path.join(folder_path, filename)
target_path = os.path.join(target_folder, filename)
shutil.move(source_path, target_path)
# def rename_file(folder_path, prefix_length, suffix_length):
# for filename in os.listdir(folder_path):
# #获取文件命中的编号部分
if __name__ == '__main__':
dest_paths = []
source_paths = []
inx = 0
# camera_folder = np.array([path_read(0), path_read(1), path_read(2), path_read(3)])
# img_name_S1, img_detail_S1 = get_image_filenames(camera_folder)
# img_name_S1, img_detail_S1 = get_image_filenames(float_path)
# img_name, img_detail = get_image_filenames(float_path)
#提取动作和相机标签
for filename in os.listdir(float_path):
source_path = os.path.join(float_path, filename)
# Action, Camera, _ = get_image_filenames(float_path)
if filename.endswith('.jpg'):
Action, Camera, _, Subject = get_image_filename(source_path)
#知识点1,itertuples()这个是dataframe的,要让它变成在in之后的循环常量
#知识点2,zip能让series类型变成in之后的循环常量
#知识点3,zip(str1,str2),其中str1和str2是两个字符串数组,需要保证两者的元素个数一样才能打包
if _ != '0':
#知识点3,"E:\\project\\learnable-triangulation-pytorch-master\\data\\human36m\\processed\\{}\\{}\\imageSequence\\{}".format(1,2,3)可以实现地址的补全
#source_path = os.path.join(folder_path, filename)这也是一种补全方法
dest_folder = DEST_path.format(Subject, Action, Camera)
#创建目标文件夹!可以一次创建多级目录
os.makedirs(dest_folder, exist_ok=True)
dest_path = os.path.join(dest_folder, filename)
shutil.copyfile(source_path, dest_path)
else:
# 跳过不符合条件的文件
print(f"Skipping: {filename} (not a .jpg file)")
# for i, filename in len(img_name), os.listdir(float_path):
# if img_detail['Action_'][i] == retval['action_names'][0]:
# #代表满足当前动作的图片总数
# k = img_detail.index[img_detail['Action_'] == retval['action_names'][0]].tolist()
# if img_detail['Camera'][i] == retval['camera_names'][0]:
# #代表即满足当前图片又满足相机编号的图片总数
# j = img_detail.index[img_detail['Camera'] == retval['camera_names'][0]].tolist()
# dest_path = DEST_path.format(retval['subject_names'][0], retval['action_names'][0],
# img_detail['Camera'][i])
#
# dest_paths.append(dest_path)
# inx = inx + 1
# copy_images(float_path, dest_paths[0], img_name, i)
# elif img_detail['Camera'][i] == retval['camera_names'][1]:
# print("良好")
# elif img_detail['Camera'][i] == retval['camera_names'][2]:
# print("cha")
# else:
# print("s")
# copy_images(float_path, dest_paths)
# dest_path = DEST_path.format(retval['subject_names'][0], retval['action_names'][0],
# retval['camera_names'][0])
# print(final_path)
# copy_images(dest_paths[0], dest_root)
# if img_detail['Action_'].astype(str) == retval['action_names'][0]:
# dest_path = DEST_path.format(retval['subject_names'][0],retval['action_names'][0], retval['camera_names'][0])
# print(s)
# for i in path_S1:
# temporary = get_image_filenames(i)
# img_name = img_name.append(temporary)
# print(img_name_S1)
文章来源:https://blog.csdn.net/weixin_44680341/article/details/135225663
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!