|
import math |
|
import numpy as np |
|
import matplotlib |
|
import matplotlib.pyplot as plt |
|
from mpl_toolkits.mplot3d import Axes3D |
|
from matplotlib.animation import FuncAnimation, FFMpegFileWriter |
|
from mpl_toolkits.mplot3d.art3d import Poly3DCollection |
|
import mpl_toolkits.mplot3d.axes3d as p3 |
|
|
|
|
|
|
|
def list_cut_average(ll, intervals): |
|
if intervals == 1: |
|
return ll |
|
|
|
bins = math.ceil(len(ll) * 1.0 / intervals) |
|
ll_new = [] |
|
for i in range(bins): |
|
l_low = intervals * i |
|
l_high = l_low + intervals |
|
l_high = l_high if l_high < len(ll) else len(ll) |
|
ll_new.append(np.mean(ll[l_low:l_high])) |
|
return ll_new |
|
|
|
|
|
def plot_3d_motion(save_path, kinematic_tree, joints, title, figsize=(10, 10), fps=120, radius=4): |
|
matplotlib.use('Agg') |
|
|
|
title_sp = title.split(' ') |
|
if len(title_sp) > 20: |
|
title = '\n'.join([' '.join(title_sp[:10]), ' '.join(title_sp[10:20]), ' '.join(title_sp[20:])]) |
|
elif len(title_sp) > 10: |
|
title = '\n'.join([' '.join(title_sp[:10]), ' '.join(title_sp[10:])]) |
|
|
|
def init(): |
|
ax.set_xlim3d([-radius / 4, radius / 4]) |
|
ax.set_ylim3d([0, radius / 2]) |
|
ax.set_zlim3d([0, radius / 2]) |
|
|
|
fig.suptitle(title, fontsize=20) |
|
ax.grid(b=False) |
|
|
|
def plot_xzPlane(minx, maxx, miny, minz, maxz): |
|
|
|
verts = [ |
|
[minx, miny, minz], |
|
[minx, miny, maxz], |
|
[maxx, miny, maxz], |
|
[maxx, miny, minz] |
|
] |
|
xz_plane = Poly3DCollection([verts]) |
|
xz_plane.set_facecolor((0.5, 0.5, 0.5, 0.5)) |
|
ax.add_collection3d(xz_plane) |
|
|
|
|
|
|
|
|
|
data = joints.copy().reshape(len(joints), -1, 3) |
|
fig = plt.figure(figsize=figsize) |
|
ax = p3.Axes3D(fig) |
|
init() |
|
MINS = data.min(axis=0).min(axis=0) |
|
MAXS = data.max(axis=0).max(axis=0) |
|
colors = ['red', 'blue', 'black', 'red', 'blue', |
|
'darkblue', 'darkblue', 'darkblue', 'darkblue', 'darkblue', |
|
'darkred', 'darkred', 'darkred', 'darkred', 'darkred'] |
|
frame_number = data.shape[0] |
|
|
|
|
|
height_offset = MINS[1] |
|
data[:, :, 1] -= height_offset |
|
trajec = data[:, 0, [0, 2]] |
|
|
|
data[..., 0] -= data[:, 0:1, 0] |
|
data[..., 2] -= data[:, 0:1, 2] |
|
|
|
|
|
|
|
def update(index): |
|
|
|
ax.lines = [] |
|
ax.collections = [] |
|
ax.view_init(elev=120, azim=-90) |
|
ax.dist = 7.5 |
|
|
|
plot_xzPlane(MINS[0] - trajec[index, 0], MAXS[0] - trajec[index, 0], 0, MINS[2] - trajec[index, 1], |
|
MAXS[2] - trajec[index, 1]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i, (chain, color) in enumerate(zip(kinematic_tree, colors)): |
|
|
|
if i < 5: |
|
linewidth = 4.0 |
|
else: |
|
linewidth = 2.0 |
|
ax.plot3D(data[index, chain, 0], data[index, chain, 1], data[index, chain, 2], linewidth=linewidth, |
|
color=color) |
|
|
|
|
|
plt.axis('off') |
|
ax.set_xticklabels([]) |
|
ax.set_yticklabels([]) |
|
ax.set_zticklabels([]) |
|
|
|
ani = FuncAnimation(fig, update, frames=frame_number, interval=1000 / fps, repeat=False) |
|
|
|
from matplotlib import rcParams |
|
rcParams['animation.ffmpeg_path'] = '/home/chenlinghao/software/miniconda3/envs/py10/bin/ffmpeg' |
|
writer = FFMpegFileWriter(fps=fps) |
|
ani.save(save_path, writer=writer) |
|
plt.close() |
|
|