File size: 1,460 Bytes
a1da68b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import imageio.v2 as imageio
npz_path, mp4_path = sys.argv[1], sys.argv[2]
title = sys.argv[3] if len(sys.argv) > 3 else ""
d = np.load(npz_path)
pj = d["posed_joints"]
T = pj.shape[0]
stride = max(1, T // 240)
idx = list(range(0, T, stride))
lo = pj[idx].reshape(-1, 3).min(0); hi = pj[idx].reshape(-1, 3).max(0)
c = (lo + hi) / 2; r = max((hi - lo).max() / 2, 0.5)
pairs = []
for key in ("parents", "parent", "joint_parents", "bones", "edges"):
    if key in d.files:
        par = np.asarray(d[key]).ravel()
        if len(par) == pj.shape[1]:
            pairs = [(int(p), j) for j, p in enumerate(par) if int(p) >= 0]
        break
frames = []
fig = plt.figure(figsize=(5, 5))
for t in idx:
    plt.clf()
    ax = fig.add_subplot(111, projection="3d")
    p3 = pj[t]
    for a, b in pairs:
        ax.plot([p3[a,0], p3[b,0]], [p3[a,1], p3[b,1]], [p3[a,2], p3[b,2]], lw=1.5)
    ax.scatter(p3[:,0], p3[:,1], p3[:,2], s=8)
    ax.set_xlim(c[0]-r, c[0]+r); ax.set_ylim(c[1]-r, c[1]+r); ax.set_zlim(c[2]-r, c[2]+r)
    ax.view_init(elev=12, azim=-60)
    ax.set_title((title[:60] + f"  f{t}/{T}") if title else f"f{t}/{T}")
    fig.canvas.draw()
    frames.append(np.asarray(fig.canvas.buffer_rgba())[..., :3].copy())
plt.close(fig)
imageio.mimsave(mp4_path, frames, fps=30, codec="libx264", quality=7)
print("preview OK:", mp4_path, len(frames), "frames")