Upload plot_3d_global.py
Browse files- visualization/plot_3d_global.py +129 -0
visualization/plot_3d_global.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import matplotlib
|
| 6 |
+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
| 7 |
+
import mpl_toolkits.mplot3d.axes3d as p3
|
| 8 |
+
from textwrap import wrap
|
| 9 |
+
import imageio
|
| 10 |
+
|
| 11 |
+
def plot_3d_motion(args, figsize=(10, 10), fps=120, radius=4):
|
| 12 |
+
matplotlib.use('Agg')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
joints, out_name, title = args
|
| 16 |
+
|
| 17 |
+
data = joints.copy().reshape(len(joints), -1, 3)
|
| 18 |
+
|
| 19 |
+
nb_joints = joints.shape[1]
|
| 20 |
+
smpl_kinetic_chain = [[0, 11, 12, 13, 14, 15], [0, 16, 17, 18, 19, 20], [0, 1, 2, 3, 4], [3, 5, 6, 7], [3, 8, 9, 10]] if nb_joints == 21 else [[0, 2, 5, 8, 11], [0, 1, 4, 7, 10], [0, 3, 6, 9, 12, 15], [9, 14, 17, 19, 21], [9, 13, 16, 18, 20]]
|
| 21 |
+
limits = 1000 if nb_joints == 21 else 2
|
| 22 |
+
MINS = data.min(axis=0).min(axis=0)
|
| 23 |
+
MAXS = data.max(axis=0).max(axis=0)
|
| 24 |
+
colors = ['red', 'blue', 'black', 'red', 'blue',
|
| 25 |
+
'darkblue', 'darkblue', 'darkblue', 'darkblue', 'darkblue',
|
| 26 |
+
'darkred', 'darkred', 'darkred', 'darkred', 'darkred']
|
| 27 |
+
frame_number = data.shape[0]
|
| 28 |
+
# print(data.shape)
|
| 29 |
+
|
| 30 |
+
height_offset = MINS[1]
|
| 31 |
+
data[:, :, 1] -= height_offset
|
| 32 |
+
trajec = data[:, 0, [0, 2]]
|
| 33 |
+
|
| 34 |
+
data[..., 0] -= data[:, 0:1, 0]
|
| 35 |
+
data[..., 2] -= data[:, 0:1, 2]
|
| 36 |
+
|
| 37 |
+
def update(index):
|
| 38 |
+
|
| 39 |
+
def init():
|
| 40 |
+
ax.set_xlim(-limits, limits)
|
| 41 |
+
ax.set_ylim(-limits, limits)
|
| 42 |
+
ax.set_zlim(0, limits)
|
| 43 |
+
ax.grid(b=False)
|
| 44 |
+
def plot_xzPlane(minx, maxx, miny, minz, maxz):
|
| 45 |
+
## Plot a plane XZ
|
| 46 |
+
verts = [
|
| 47 |
+
[minx, miny, minz],
|
| 48 |
+
[minx, miny, maxz],
|
| 49 |
+
[maxx, miny, maxz],
|
| 50 |
+
[maxx, miny, minz]
|
| 51 |
+
]
|
| 52 |
+
xz_plane = Poly3DCollection([verts])
|
| 53 |
+
xz_plane.set_facecolor((0.5, 0.5, 0.5, 0.5))
|
| 54 |
+
ax.add_collection3d(xz_plane)
|
| 55 |
+
fig = plt.figure(figsize=(480/96., 320/96.), dpi=96) if nb_joints == 21 else plt.figure(figsize=(10, 10), dpi=96)
|
| 56 |
+
if title is not None :
|
| 57 |
+
wraped_title = '\n'.join(wrap(title, 40))
|
| 58 |
+
fig.suptitle(wraped_title, fontsize=16)
|
| 59 |
+
ax = p3.Axes3D(fig)
|
| 60 |
+
|
| 61 |
+
init()
|
| 62 |
+
|
| 63 |
+
ax.lines = []
|
| 64 |
+
ax.collections = []
|
| 65 |
+
ax.view_init(elev=110, azim=-90)
|
| 66 |
+
ax.dist = 7.5
|
| 67 |
+
# ax =
|
| 68 |
+
plot_xzPlane(MINS[0] - trajec[index, 0], MAXS[0] - trajec[index, 0], 0, MINS[2] - trajec[index, 1],
|
| 69 |
+
MAXS[2] - trajec[index, 1])
|
| 70 |
+
# ax.scatter(data[index, :22, 0], data[index, :22, 1], data[index, :22, 2], color='black', s=3)
|
| 71 |
+
|
| 72 |
+
if index > 1:
|
| 73 |
+
ax.plot3D(trajec[:index, 0] - trajec[index, 0], np.zeros_like(trajec[:index, 0]),
|
| 74 |
+
trajec[:index, 1] - trajec[index, 1], linewidth=1.0,
|
| 75 |
+
color='blue')
|
| 76 |
+
# ax = plot_xzPlane(ax, MINS[0], MAXS[0], 0, MINS[2], MAXS[2])
|
| 77 |
+
|
| 78 |
+
for i, (chain, color) in enumerate(zip(smpl_kinetic_chain, colors)):
|
| 79 |
+
# print(color)
|
| 80 |
+
if i < 5:
|
| 81 |
+
linewidth = 4.0
|
| 82 |
+
else:
|
| 83 |
+
linewidth = 2.0
|
| 84 |
+
ax.plot3D(data[index, chain, 0], data[index, chain, 1], data[index, chain, 2], linewidth=linewidth,
|
| 85 |
+
color=color)
|
| 86 |
+
# print(trajec[:index, 0].shape)
|
| 87 |
+
|
| 88 |
+
plt.axis('off')
|
| 89 |
+
ax.set_xticklabels([])
|
| 90 |
+
ax.set_yticklabels([])
|
| 91 |
+
ax.set_zticklabels([])
|
| 92 |
+
|
| 93 |
+
if out_name is not None :
|
| 94 |
+
plt.savefig(out_name, dpi=96)
|
| 95 |
+
plt.close()
|
| 96 |
+
|
| 97 |
+
else :
|
| 98 |
+
io_buf = io.BytesIO()
|
| 99 |
+
fig.savefig(io_buf, format='raw', dpi=96)
|
| 100 |
+
io_buf.seek(0)
|
| 101 |
+
# print(fig.bbox.bounds)
|
| 102 |
+
arr = np.reshape(np.frombuffer(io_buf.getvalue(), dtype=np.uint8),
|
| 103 |
+
newshape=(int(fig.bbox.bounds[3]), int(fig.bbox.bounds[2]), -1))
|
| 104 |
+
io_buf.close()
|
| 105 |
+
plt.close()
|
| 106 |
+
return arr
|
| 107 |
+
|
| 108 |
+
out = []
|
| 109 |
+
for i in range(frame_number) :
|
| 110 |
+
out.append(update(i))
|
| 111 |
+
out = np.stack(out, axis=0)
|
| 112 |
+
return torch.from_numpy(out)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def draw_to_batch(smpl_joints_batch, title_batch=None, outname=None) :
|
| 116 |
+
|
| 117 |
+
batch_size = len(smpl_joints_batch)
|
| 118 |
+
out = []
|
| 119 |
+
for i in range(batch_size) :
|
| 120 |
+
out.append(plot_3d_motion([smpl_joints_batch[i], None, title_batch[i] if title_batch is not None else None]))
|
| 121 |
+
if outname is not None:
|
| 122 |
+
imageio.mimsave(outname[i], np.array(out[-1]), fps=20)
|
| 123 |
+
out = torch.stack(out, axis=0)
|
| 124 |
+
return out
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|