一 、显示效果
动画链接:3D骨骼关节的动画显示
二、3D骨骼关节点坐标的文件说明
skeleton_pos.txt: Skeleton position data
Skeleton data consists of 15 joints per person. Each row follows the following format.
Frame#,PA(1),PA(2),...,PA(15),PB(1),PB(2),...,PB(15)
PA(i) => position of ith joint (x,y,z) for the subject A located at left
PB(i) => position of ith joint (x,y,z) for the subject B located at right
x and y are normalized as [0,1] while z is normalized as [0,7.8125]
Joint number -> Joint name
1 -> HEAD
2 -> NECK
3 -> TORSO
4 -> LEFT_SHOULDER
5 -> LEFT_ELBOW
6 -> LEFT_HAND
7 -> RIGHT_SHOULDER
8 -> RIGHT_ELBOW
9 -> RIGHT_HAND
10 -> LEFT_HIP
11 -> LEFT_KNEE
12 -> LEFT_FOOT
13 -> RIGHT_HIP
14 -> RIGHT_KNEE
15 -> RIGHT_FOOT
The skeleton data was normalized. In order to extract the original position of the joints, the following equations are needed:
original_X = 1280 - (normalized_X .* 2560);
original_Y = 960 - (normalized_Y .* 1920);
original_Z = normalized_Z .* 10000 ./ 7.8125;
三、源代码
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
a = pd.read_csv("skeleton_pos.txt",header=None)
b = a.values[:,1:]
b = b.reshape((-1,30,3))
length = b.shape[0]
b[:,:,0] = 1280 - b[:,:,0] * 2560
b[:,:,1] = 960 - b[:,:,1] * 1920
b[:,:,2] = b[:,:,2] * 10000 / 7.8125
Person1,Person2 = np.split(b,2,axis = 1)
print Person1[1]
print Person1.shape
fig = plt.figure()
ax = Axes3D(fig)
stick_defines = [
(0, 1),
(1, 2),
(1, 3),
(1, 6),
(3, 4),
(4, 5),
(6, 7),
(7, 8),
(2, 9),
(2, 12),
(9, 10),
(10, 11),
(12, 13),
(13, 14)
]
Person1_lines = [ax.plot(Person1[0,i,0], Person1[0,i,2], Person1[0,i,1], 'k-')[0] for i in stick_defines]
Person2_lines = [ax.plot(Person2[0,i,0], Person2[0,i,2], Person2[0,i,1], 'r-')[0] for i in stick_defines]
def init():
for stick_line, i in zip(Person1_lines, stick_defines):
stick_line._verts3d = Person1[0,i,0], Person1[0,i,2], Person1[0,i,1]
for stick_line, i in zip(Person2_lines, stick_defines):
stick_line._verts3d = Person2[0,i,0], Person2[0,i,2], Person2[0,i,1]
return Person1_lines+Person2_lines
def animate(j):
for stick_line, i in zip(Person1_lines, stick_defines):
stick_line._verts3d = Person1[j,i,0], Person1[j,i,2], Person1[j,i,1]
for stick_line, i in zip(Person2_lines, stick_defines):
stick_line._verts3d = Person2[j,i,0], Person2[j,i,2], Person2[j,i,1]
# stick_lines1 = [ax.plot(FirPerson[j,i,0], FirPerson[j,i,2], FirPerson[j,i,1], 'k-')[0] for i in stick_defines]
# stick_lines2 = [ax.plot(SecPerson[j,i,0], SecPerson[j,i,2], SecPerson[j,i,1], 'r-')[0] for i in stick_defines]
return Person1_lines+Person2_lines
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=length, interval=100, blit=True)
anim.save('result.mp4',fps = 20)
#plt.show()