import syssys.path.extend(['../'])from data_gen.rotation import*from tqdm import tqdmdefpre_normalization(data, zaxis=[0,1], xaxis=[8,4]):N, C, T, V, M = data.shapes = np.transpose(data,[0,4,2,3,1])# N, C, T, V, M to N, M, T, V, Cprint('pad the null frames with the previous frames')for i_s, skeleton inenumerate(tqdm(s)):# 選中一個sampleif skeleton.sum()==0:print(i_s,' has no skeleton')for i_p, person inenumerate(skeleton):#在sample中選中一個personif person.sum()==0:#對當前矩陣所有內(nèi)容求和continueif person[0].sum()==0:#如果這個person的第0幀對應的所有內(nèi)容=0index =(person.sum(-1).sum(-1)!=0)#如果最后一幀的最后一個關節(jié)存在,index=1,否則為0tmp = person[index].copy()#復制這一幀的內(nèi)容person *=0#清空person[:len(tmp)]= tmp#全部賦值為相同的內(nèi)容for i_f, frame inenumerate(person):#選中一幀if frame.sum()==0:if person[i_f:].sum()==0:#如果當前這個人對應的這個幀之后的內(nèi)容都為0rest =len(person)- i_fnum =int(np.ceil(rest / i_f))#使用有意義的數(shù)據(jù)循環(huán)填充pad = np.concatenate([person[0:i_f]for _ inrange(num)],0)[:rest]s[i_s, i_p, i_f:]= padbreakprint('sub the center joint #1 (spine joint in ntu and neck joint in kinetics)')for i_s, skeleton inenumerate(tqdm(s)):if skeleton.sum()==0:continuemain_body_center = skeleton[0][:,1:2,:].copy()#選取一個sample下的一個person,它的第1個節(jié)點定義為main_body_centerfor i_p, person inenumerate(skeleton):if person.sum()==0:continuemask =(person.sum(-1)!=0).reshape(T, V,1)s[i_s, i_p]=(s[i_s, i_p]- main_body_center)* mask#減去main_body_center的內(nèi)容print('parallel the bone between hip(jpt 0) and spine(jpt 1) of the first person to the z axis')#將第一個人的髖部(jpt 0)與脊柱(jpt 1)之間的骨與z軸平行for i_s, skeleton inenumerate(tqdm(s)):if skeleton.sum()==0:continuejoint_bottom = skeleton[0,0, zaxis[0]]joint_top = skeleton[0,0, zaxis[1]]axis = np.cross(joint_top - joint_bottom,[0,0,1])
angle = angle_between(joint_top - joint_bottom,[0,0,1])matrix_z = rotation_matrix(axis, angle)for i_p, person inenumerate(skeleton):if person.sum()==0:continuefor i_f, frame inenumerate(person):if frame.sum()==0:continuefor i_j, joint inenumerate(frame):s[i_s, i_p, i_f, i_j]= np.dot(matrix_z, joint)print('parallel the bone between right shoulder(jpt 8) and left shoulder(jpt 4) of the first person to the x axis')#將第一個人的右肩(jpt 8)和左肩(jpt 4)之間的骨與x軸平行for i_s, skeleton inenumerate(tqdm(s)):if skeleton.sum()==0:continuejoint_rshoulder = skeleton[0,0, xaxis[0]]joint_lshoulder = skeleton[0,0, xaxis[1]]axis = np.cross(joint_rshoulder - joint_lshoulder,[1,0,0])angle = angle_between(joint_rshoulder - joint_lshoulder,[1,0,0])matrix_x = rotation_matrix(axis, angle)for i_p, person inenumerate(skeleton):if person.sum()==0:continuefor i_f, frame inenumerate(person):if frame.sum()==0:continuefor i_j, joint inenumerate(frame):s[i_s, i_p, i_f, i_j]= np.dot(matrix_x, joint)data = np.transpose(s,[0,4,2,3,1])return dataif __name__ =='__main__':data = np.load('../data/ntu/xview/val_data.npy')pre_normalization(data)np.save('../data/ntu/xview/data_val_pre.npy', data)