"""Camera overlay and fixed oblique 3D comparison; identical view for both conditions.""" import sys,json,subprocess from pathlib import Path ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'third_party/FoundationPose')) import cv2,numpy as np,torch,trimesh,nvdiffrast.torch as dr from Utils import nvdiffrast_render,make_mesh_tensors from scipy.spatial.transform import Rotation out=ROOT/'output/depth_ablation_red_20260916';a=np.load(out/'foreground_left_depth_comparison.npz');N=len(a['verts_before']);K=a['K'];P=a['object_pose'];mesh=trimesh.load(out/'red_box_closed.ply');ctx=dr.RasterizeCudaContext();eye=torch.eye(4,device='cuda')[None] center=np.median(np.einsum('tij,j->ti',P[:,:3,:3],mesh.bounds.mean(0))+P[:,:3,3],axis=0);R=Rotation.from_euler('y',55,degrees=True).as_matrix();virtualK=np.array([[700.,0,424],[0,700,240],[0,0,1.]]) def render(v,f,color,k): m=trimesh.Trimesh(vertices=v,faces=f,process=False);m.visual.vertex_colors=np.tile([*color,255],(len(v),1));mt=make_mesh_tensors(m) with torch.inference_mode():c,d,_=nvdiffrast_render(K=k,H=480,W=848,ob_in_cams=eye,glctx=ctx,mesh_tensors=mt,use_light=True) return (c[0].cpu().numpy()[:,:,::-1]*255).astype('uint8'),d[0].cpu().numpy() def scene(b,hand,box,k): colors=[];depth=[] for v,f,c in [(box,mesh.faces,[60,185,95]),(hand,a['faces'],[80,145,235])]: cc,d=render(v,f,c,k);colors.append(cc);depth.append(d) masks=[d>0 for d in depth];front=masks[1]&((~masks[0])|(depth[1]