首页 > 解决方案 > 生成具有不同大小数组的 4D 数组

问题描述

我有一个 .skeleton 图像,它们是 3D 图像。一个 .skeleton 文件包含可变数量的帧。每个框架有 50 个关节,所有 50 个关节都包含它们的 x、y、z 坐标。

例如,考虑一个包含 150 帧的 .skeleton 文件,其中每帧包含 50 个关节,每个关节包含 x、y、z 坐标。所以我创建了将值放入一个 4D 数组 [1,150,50,3] 中。

另一个文件包含 148 帧、50 个关节,每个关节都有 x、y、z 坐标。[1,140,​​50,3] 因此,对于每个 .skeleton 文件,帧数各不相同,但另一个是固定的。例如:[1,200,50,3]

.skeleton 文件的总数为 2500。所以我需要将它们组合成一个 ndarray。例如,[2500,XXX,50,3]

但我无法做到这一点,因为 .skeleton 文件中的帧数不同。有人可以告诉我如何实现这一目标吗?我如何确定常见的帧数。例如将其转换为 [2500,300,50,3]。请帮忙。

这是我到目前为止所做的

import glob
import os
import numpy as np
import csv

skeleton_files_mask = os.path.join(SKELETON_DIR, '*.skeleton')
skeleton_files = glob.glob(skeleton_files_mask) #glob is a general term used to define techniques to match specified pattern according to rules related Unix shell
#skeleton_files is a list of path names of the file

print ('Number of .skeleton files ',len(skeleton_files))

max_frame_count = 300
max_joints = 50

full_ds = []
total = []

#for idx, file_name in enumerate(skeleton_files[:568]):
for idx, file_name in enumerate(skeleton_files): 
    if idx%100 == 0:
        print(idx)
    basename = os.path.basename(file_name)
    #os.path.basename(path) : It is used to return the basename of the file . This function basically return the file name from the path given
    print (basename)
    name = os.path.splitext(basename)[0]
    label = name.split('A')[1]
    with open(file_name) as f:
        framecount = int(f.readline())

        print ('Frame count.....',framecount)
        #Frame count..... 158
        #Frame count..... 102
        sequence_frames = []

        for frame in range(framecount): # Looping across 158
            body_count = int(f.readline()) #1/2
            #print ('body_count.....',body_count)
            if body_count <= 0 or body_count>2:
                print('continue, no body')
                break
            joints_xyz = []
            for body in range(body_count): #1/2
                skeleton_info = f.readline()
                joint_counts = int(f.readline()) 
                #print ('joint_counts.....',joint_counts)
                for joint in range(joint_counts): #25/50
                    joint_info = f.readline() #loop goes for 25 times
                    #print ('joint_info.....',joint_info)
                    joint_info_array = joint_info.split()
                    #print (joint_info_array)
                    x, y, z = joint_info_array[:3]
                    #print (x,y,z)
                    joint_info_xyz = np.array([float(x), float(y), float(z)])
                    #print('joint_info_xyz', joint_info_xyz.shape)
                    joints_xyz.append(joint_info_xyz)
            pad_joints = max_joints - len(joints_xyz)
            joints_xyz = np.array(joints_xyz)
            joints_xyz = np.pad(joints_xyz, ((0, pad_joints), (0, 0)), mode='constant')
            frame_xyz = np.stack(joints_xyz)
            #print('frame_xyz', frame_xyz.shape)
            sequence_frames.append(frame_xyz)
            #print (sequence_frames)
            #X = np.array(sequence_frames)
        total.append(sequence_frames)
            #print (total)
        if len(sequence_frames) > 0:
            file_name = os.path.join(NPY_DIR, name+ '.npy')
            sample = [name+'.npy', int(label)-1]
            full_ds.append(sample)
            #print(full_ds)
            #print (X.shape)
            np.save(file_name, np.array(sequence_frames))
print ('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
R=np.array(total)

print (R.shape)
#train_ds = full_ds[:380]
#test_ds = full_ds[380:]

train_ds = full_ds[:40320]
test_ds = full_ds[40320:]

with open(os.path.join(NPY_DIR, TRAIN_DS), 'w') as train_ds_file:
    writer = csv.writer(train_ds_file, lineterminator='\n')
    writer.writerows(train_ds)

with open(os.path.join(NPY_DIR, TEST_DS), 'w') as test_ds_file:
    writer = csv.writer(test_ds_file, lineterminator='\n')
    writer.writerows(test_ds)

我可以为一个 .skeleton 文件成功运行它。它的输出是

Number of .skeleton files  1
0
S001C001P001R001A002.skeleton
Frame count..... 158
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
(1, 158, 50, 3)

标签: pythonarrays

解决方案


推荐阅读