生成具有不同大小的数组的4D数组

时间:2019-05-21 09:02:46

标签: python arrays

我有3D图像的.skeleton图像。一个.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)

0 个答案:

没有答案