trick-prediction-public / tools /render_numeric_previews.py
diffusion-gif's picture
data: privacy-minimized licensed numerical trajectories and GT-only diagrams
dde1cc3 verified
Raw History Blame Contribute Delete
3.14 kB
#!/usr/bin/env python3
"""Draw trajectory GT animations from already-sanitized numerical values only.
No source RGB/NPZ/video/audio is read. Animations are diagrams, not model outputs
or actual footage. MP4s contain one newly drawn video stream and no audio.
"""
import json,math
from pathlib import Path
from PIL import Image,ImageDraw
ROOT=Path(__file__).resolve().parents[1]
COLORS=[(37,112,218),(233,132,28)]
def frame(row, step):
size=512;image=Image.new('RGB',(size,size),(248,250,253));draw=ImageDraw.Draw(image)
pad=42;extent=size-2*pad
def xy(point):return (pad+point[0]/1000*extent,pad+point[1]/1000*extent)
for n in range(6):
p=pad+n*extent/5
draw.line((p,pad,p,size-pad),fill=(224,229,237));draw.line((pad,p,size-pad,p),fill=(224,229,237))
draw.text((pad,10),'NUMERIC TRAJECTORY DIAGRAM - NOT RGB FOOTAGE',fill=(25,34,49))
draw.text((pad,size-28),'Observed history / Future GT; blue=left, orange=right',fill=(25,34,49))
for arm in range(2):
history=row['history'][arm]
future=[[row['anchor'][arm][i]+d[i] for i in range(2)] for d in row['future_delta'][arm]]
total=history+future
visible=total[:step+1]
if len(visible)>1:draw.line([xy(p) for p in visible],fill=COLORS[arm],width=3)
for j,p in enumerate(visible):
x,y=xy(p);radius=4 if j<8 else 3
if j>=8:draw.rectangle((x-radius,y-radius,x+radius,y+radius),outline=COLORS[arm],width=2)
else:draw.ellipse((x-radius,y-radius,x+radius,y+radius),fill=COLORS[arm])
x,y=xy(visible[-1]);draw.ellipse((x-6,y-6,x+6,y+6),outline=COLORS[arm],width=2)
return image
def main():
import cv2,numpy as np
cv2.setNumThreads(1)
selected={}
for split in ('train','heldout'):
with (ROOT/'data'/(split+'.jsonl')).open() as f:
for line in f:
row=json.loads(line);selected.setdefault(row['task'],row)
out=ROOT/'previews';out.mkdir(exist_ok=True);records=[]
for i,(task,row) in enumerate(sorted(selected.items()),1):
name=f'preview_{i:02d}'
images=[frame(row,j) for j in range(16)]
images[0].save(out/(name+'.gif'),save_all=True,append_images=images[1:],duration=180,loop=0,disposal=2)
mp4=out/(name+'.mp4');writer=cv2.VideoWriter(str(mp4),cv2.VideoWriter_fourcc(*'mp4v'),6.0,(512,512))
if not writer.isOpened():raise RuntimeError('CPU MP4 encoder unavailable')
for image in images:writer.write(cv2.cvtColor(np.asarray(image),cv2.COLOR_RGB2BGR))
writer.release()
reader=cv2.VideoCapture(str(mp4))
if not reader.isOpened() or int(reader.get(cv2.CAP_PROP_FRAME_COUNT))!=16:raise ValueError('Preview validation failed')
reader.release()
records.append({'gif':name+'.gif','video':name+'.mp4','sample_id':row['sample_id'],'task':task,'frames':16,'source_pixels_used':False,'audio_used':False,'model_prediction':False})
(out/'manifest.json').write_text(json.dumps(records,indent=2)+'\n')
print('Rendered',len(records),'GT-only diagram GIFs/MP4s; no raw source footage/audio.')
if __name__=='__main__':main()