处理数据集 一个txt文件有n帧,每帧有25*3个三维坐标 由于使用三维坐标为特征效果不好,提取角度为特征,以下代码为三点法计算角度,后期更换为两点法。
import numpy as np
import math
def get_angles(d_vid):
angles=[]
for i in range(len(d_vid)):
new_d_vid=d_vid[i].reshape(25,3)
leg_right=get_endu(new_d_vid[20][0],new_d_vid[20][1],new_d_vid[20][2],new_d_vid[12][0],new_d_vid[12][1],
new_d_vid[12][2],new_d_vid[13][0],new_d_vid[13][1],new_d_vid[13][2])
leg_left = get_endu(new_d_vid[20][0],new_d_vid[20][1], new_d_vid[20][2], new_d_vid[16][0], new_d_vid[16][1],
new_d_vid[16][2], new_d_vid[17][0], new_d_vid[17][1], new_d_vid[17][2])
arm_right = get_endu(new_d_vid[20][0], new_d_vid[20][1], new_d_vid[20][2], new_d_vid[8][0],
new_d_vid[8][1],new_d_vid[8][2], new_d_vid[9][0], new_d_vid[9][1], new_d_vid[9][2])
arm_left = get_endu(new_d_vid[20][0], new_d_vid[20][1], new_d_vid[20][2], new_d_vid[4][0],
new_d_vid[4][1], new_d_vid[4][2], new_d_vid[5][0], new_d_vid[5][1], new_d_vid[5][2])
hip_right = get_endu(new_d_vid[0][0], new_d_vid[0][1], new_d_vid[0][2], new_d_vid[12][0],
new_d_vid[12][1], new_d_vid[12][2], new_d_vid[13][0], new_d_vid[13][1], new_d_vid[13][2])
hip_left = get_endu(new_d_vid[0][0], new_d_vid[0][1], new_d_vid[0][2], new_d_vid[16][0],
new_d_vid[16][1], new_d_vid[16][2], new_d_vid[17][0], new_d_vid[17][1], new_d_vid[17][2])
elbow_right = get_endu(new_d_vid[8][0], new_d_vid[8][1], new_d_vid[8][2], new_d_vid[9][0],
new_d_vid[9][1], new_d_vid[9][2], new_d_vid[10][0], new_d_vid[10][1], new_d_vid[10][2])
elbow_left = get_endu(new_d_vid[4][0], new_d_vid[4][1], new_d_vid[4][2], new_d_vid[5][0],
new_d_vid[5][1], new_d_vid[5][2], new_d_vid[6][0], new_d_vid[6][1], new_d_vid[6][2])
arm_spine_r=get_endu(new_d_vid[10][0],new_d_vid[10][1],new_d_vid[10][2],new_d_vid[8][0],
new_d_vid[8][1],new_d_vid[8][2],new_d_vid[8][0],new_d_vid[8][1],new_d_vid[1][2])
arm_spine_l = get_endu(new_d_vid[6][0], new_d_vid[6][1], new_d_vid[6][2], new_d_vid[4][0],
new_d_vid[4][1], new_d_vid[4][2], new_d_vid[4][0], new_d_vid[4][1], new_d_vid[1][2])
leg_spine_l = get_endu(new_d_vid[17][0], new_d_vid[17][1], new_d_vid[17][2], new_d_vid[16][0],
new_d_vid[16][1], new_d_vid[16][2], new_d_vid[16][0], new_d_vid[16][1], new_d_vid[17][2])
leg_spine_r = get_endu(new_d_vid[13][0], new_d_vid[13][1], new_d_vid[13][2], new_d_vid[12][0],
new_d_vid[12][1], new_d_vid[12][2], new_d_vid[12][0], new_d_vid[12][1], new_d_vid[13][2])
new_pr = get_endu(new_d_vid[12][0], new_d_vid[12][1], new_d_vid[12][2], new_d_vid[8][0],
new_d_vid[8][1], new_d_vid[8][2], new_d_vid[8][0], new_d_vid[12][1], new_d_vid[12][2])
new_pl = get_endu(new_d_vid[16][0], new_d_vid[16][1], new_d_vid[16][2], new_d_vid[4][0],
new_d_vid[4][1], new_d_vid[4][2], new_d_vid[4][0], new_d_vid[16][1], new_d_vid[4][2])
each_angles=[leg_right,leg_left,arm_right, arm_left,hip_right,hip_left,elbow_right,elbow_left,arm_spine_l,arm_spine_r,leg_spine_l,leg_spine_r,new_pr,new_pl]
angles.append(each_angles)
return np.array(angles)
def get_endu(x1, y1, z1, x2, y2, z2, x3, y3, z3):
dis1 = math.sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2) + pow((z1 - z2), 2))
dis2 = math.sqrt(pow((x2 - x3), 2) + pow((y2 - y3), 2) + pow((z2 - z3), 2))
dis3 = math.sqrt(pow((x1 - x3), 2) + pow((y1 - y3), 2) + pow((z1 - z3), 2))
if dis1 * dis2==0.0:
angle = 180.0
else:
if (dis1 * dis1 + dis2 * dis2 - dis3 * dis3) / (2 * dis1 * dis2)<-1 or(dis1 * dis1 + dis2 * dis2 - dis3 * dis3) / (2 * dis1 * dis2)>1:
angle=0
else:
angle = math.acos((dis1 * dis1 + dis2 * dis2 - dis3 * dis3) / (2 * dis1 * dis2)) * 180.0 / 3.1415926
return angle
#a=np.arange(32).reshape((4,8))
file_path = r'./K-Nearest-Neighbors-with-Dynamic-Time-Warping-master/data/skeletonData/101_18_0_1_1_stand.txt'
a=get_angles(np.loadtxt(file_path,dtype=np.float,encoding='utf-8',delimiter=','))
#a=a.reshape(4,8)
print("原始数据:\n",a)
print("转换后:\n",a.transpose(1,0))
转换后结果: