File size: 1,871 Bytes
ea1b5d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fast orthographic rendering of actual HumanML3D XYZ joints."""
from pathlib import Path
import numpy as np
from PIL import Image,ImageDraw

CHAINS=[[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]]
COLORS=['#d95f02','#1b9e77','#666666','#7570b3','#e7298a']

def motion_frame(joints,frame,width=640):
    p=np.asarray(joints,dtype=float)
    if p.ndim!=3 or p.shape[1:]!=(22,3) or not np.isfinite(p).all():raise ValueError('Expected finite [T,22,3] joints')
    # XYZ -> two coordinates, y remains vertical. Camera follows horizontal root.
    data=p[frame].copy();data[:,[0,2]]-=data[0,[0,2]]
    floor=float(p[:,:,1].min());data[:,1]-=floor
    projected=np.stack([.866*data[:,0]-.5*data[:,2],-.25*data[:,0]-.433*data[:,2]-data[:,1]],axis=-1)
    height=max(float(p[:,:,1].max()-floor),1.);scale=width*.65/max(height,1.5)
    projected=projected*scale+np.array([width/2,width*.85])
    image=Image.new('RGB',(width,width),'#f7f7f7');draw=ImageDraw.Draw(image)
    draw.line([(width*.1,width*.85),(width*.9,width*.85)],fill='#bbbbbb',width=2)
    for chain,color in zip(CHAINS,COLORS):draw.line([tuple(projected[i]) for i in chain],fill=color,width=5)
    for x,y in projected:draw.ellipse((x-3,y-3,x+3,y+3),fill='#222222')
    draw.text((16,16),f'HumanML3D XYZ | frame {frame+1}/{len(p)} | 20 fps',fill='#222222')
    return image

def render_motion(path,joints,fps=20):
    import imageio.v2 as iio
    path=Path(path)
    with iio.get_writer(path,fps=fps,codec='libx264',quality=8,macro_block_size=1) as writer:
        for frame in range(len(joints)):writer.append_data(np.asarray(motion_frame(joints,frame)))
    indices=np.linspace(0,len(joints)-1,6).astype(int);strip=Image.new('RGB',(640*6,640))
    for column,index in enumerate(indices):strip.paste(motion_frame(joints,index),(column*640,0))
    strip.save(path.with_suffix('.png'))