def render_lsd(seq): plypath = os.path.join(seq, 'render_me.ply') verts, edges = IO.read_ply(plypath) lsd_mesh = Mesh() lsd_mesh.verts = verts.astype(np.float32) campath = os.path.join(seq, 'render.py') Rs, ts = IO.import_blender_cam(campath) W, H = 1920, 1080 im = empty((H, W, 3), np.uint8) cam = Geom.cam_params(29.97, W, H, 35) # Blender defaults cam = cam.astype(np.float32) render_out = os.path.join(seq, 'render') Util.try_mkdir(render_out) im[:] = 255 pts = lsd_mesh.project_verts(Rs[0].astype(np.float32), ts[0].astype(np.float32), cam) pt_buf = buffer_points(pts, np.r_[255, 82, 82], alpha=0.2, size=2) render_buffer(im, pt_buf, bg=255) out = os.path.join(render_out, 'frame_0.png') IO.imwrite(out, im)
def show_edges(seq, cloud, thresh, min_score, imtype='png'): edge_dir = os.path.join(seq, 'edges', '*.' + imtype) paths = glob.glob(edge_dir) segout = os.path.join(seq, 'segment_stats.npz') # --- Load data --- npz = np.load(segout) score = npz['score'] # total = npz['total'] # curv_mean = npz['curv_mean'] # curv_var = npz['curv_var'] # --- Prepare data --- F = len(paths) fx, fy, cx, cy = cloud.cam labels = cloud.labels_frame focal = cloud.cam[:2] center = cloud.cam[2:] outfolder = os.path.join(seq, 'hard_edges') Util.try_mkdir(outfolder) # --- Do it --- print("Writing out frames...") j = 0 for f in range(F): if f % 5: continue print("Frame {}".format(f)) path = paths[f] im = IO.imread(path) col = color.gray2rgb(255 - im) s, e = cloud.frame_range[f] for i in range(s, e): if labels[i] == 0 or score[i] < min_score: continue line = empty(4) line[:2] = cloud.local_rays[i, :2] * focal + center line[2:] = cloud.local_rays[i + 1, :2] * focal + center np.round(line, out=line) I, J = Drawing.line(*(line.astype(int))) Drawing.draw_points(col, I, J, color=(255, 0, 0)) Drawing.draw_points(col, I + 1, J, color=(255, 0, 0)) Drawing.draw_points(col, I, J + 1, color=(255, 0, 0)) # col[I, J] = 255, 0, 0 out = os.path.join(outfolder, '{:04d}.png'.format(f)) j += 1 IO.imwrite(out, col)
def draw_normal_map(path, size): ''' Draw a spherical normal map for visualizations ''' # White backdrop im = np.full((size, size, 3), 255, np.uint8) center = size//2 light = np.r_[0,0,1.0] ng = np.r_[0,0,1.0] for i in range(size): di = i - center for j in range(size): dj = j - center r = sqrt(di*di + dj*dj) if r > center: continue r_norm = r/center phi = np.arccos(r_norm) theta = -np.arctan2(dj, di) nx = cos(theta)*cos(phi) ny = sin(theta)*cos(phi) nz = sin(phi) mag = sqrt(nx*nx + ny*ny + nz*nz) normal = np.r_[nx/mag, -ny/mag, nz/mag] color = normal_shade(light, ng, normal) im[i,j] = color IO.imshow(im) pynutmeg.wait_for_nutmeg() IO.imwrite(path, im) return im
def render_animation(seq, sub, dopoly): import pynutmeg import time import os import RayCloud fig = pynutmeg.figure('cube', 'figs/imshow.qml') W, H = 1920, 1080 im = empty((H, W, 3), np.uint8) cam = Geom.cam_params(29.97, W, H, 35) # Blender defaults cam = cam.astype(np.float32) print("Loading data") cloud = RayCloud.load(os.path.join(seq, sub)) datapath = os.path.join(seq, 'occlusion_data.npz') data = np.load(datapath) mesh_path = os.path.join(seq, 'occlusion_mesh.npz') occl_mesh = load_mesh(mesh_path) # occl_mesh = Mesh() # occl_mesh.verts = data['verts'].astype(np.float32) edges = data['edges'].astype(np.uint32) ply_path = os.path.join(seq, 'model.ply') if os.path.exists(ply_path): obj_mesh = from_ply(ply_path) else: obj_mesh = Mesh() if dopoly: poly_data = os.path.join(seq, sub, 'poly_data.npz') polynpz = np.load(poly_data) poly_mesh = Mesh() poly_mesh.verts = polynpz['verts'].astype(np.float32) poly_mesh.edges = polynpz['edges'].astype(np.uint32) else: poly_mesh = None # pers_mesh_path = os.path.join(seq, 'persistent_mesh.npz') # pers_mesh = load_mesh(pers_mesh_path) campath = os.path.join(seq, 'animation.py') Rs, ts = IO.import_blender_cam(campath) # Grab frame info inds = data['inds'] frames = cloud.frames[inds] render_out = os.path.join(seq, 'animate') Util.try_mkdir(render_out) F = len(Rs) N = len(frames) print("Loaded", frames.max()) end_frame = 0 for f in range(0, F, 10): print("Frame:", f) # R = cloud.Rs[f] # t = cloud.ts[f] R = Rs[f].astype(np.float32) t = ts[f].astype(np.float32) while end_frame < N and frames[end_frame] < f: end_frame += 1 occl_mesh.edges = edges[:end_frame] im[:] = 255 if len(occl_mesh.edges) > 0: t0 = time.time() render_frame(im, obj_mesh, occl_mesh, None, R, t, cam) print("Render time: {} ms".format( int((time.time() - t0)*1000) )) # time.sleep(0.005) fig.set('ax.im', binary=im) out = os.path.join(render_out, 'frame_{:05d}.png'.format(f)) IO.imwrite(out, im)
def test_render_seq(seq, sub, dopoly): import pynutmeg import time import os import RayCloud fig = pynutmeg.figure('cube', 'figs/imshow.qml') W, H = 1920, 1080 im = empty((H, W, 3), np.uint8) cam = Geom.cam_params(29.97, W, H, 35) # Blender defaults cam = cam.astype(np.float32) print("Loading data") cloud = RayCloud.load(os.path.join(seq, sub)) mesh_path = os.path.join(seq, 'occlusion_mesh.npz') occl_mesh = load_mesh(mesh_path) ply_path = os.path.join(seq, 'model.ply') if os.path.exists(ply_path): obj_mesh = from_ply(ply_path) else: obj_mesh = Mesh() if dopoly: poly_data = os.path.join(seq, sub, 'poly_data.npz') polynpz = np.load(poly_data) poly_mesh = Mesh() poly_mesh.verts = polynpz['verts'].astype(np.float32) poly_mesh.edges = polynpz['edges'].astype(np.uint32) else: poly_mesh = None pers_mesh_path = os.path.join(seq, 'persistent_mesh.npz') pers_mesh = load_mesh(pers_mesh_path) campath = os.path.join(seq, 'render.py') Rs, ts = IO.import_blender_cam(campath) render_out = os.path.join(seq, 'render') Util.try_mkdir(render_out) F = min(3, len(Rs)) print("Loaded") for f in range(F): print("Frame:", f) # R = cloud.Rs[f] # t = cloud.ts[f] R = Rs[f].astype(np.float32) t = ts[f].astype(np.float32) im[:] = 255 t0 = time.time() render_frame(im, obj_mesh, occl_mesh, None, R, t, cam) print("dt:", (time.time() - t0)*1000) # time.sleep(0.005) fig.set('ax.im', binary=im) out = os.path.join(render_out, 'frame_{}.png'.format(f)) IO.imwrite(out, im) im[:] = 255 t0 = time.time() render_frame(im, obj_mesh, pers_mesh, poly_mesh, R, t, cam, color=np.r_[255, 82, 82], poly_color=np.r_[0,0,0]) print("dt:", (time.time() - t0)*1000) fig.set('ax.im', binary=im) out = os.path.join(render_out, 'frame_pers_{}.png'.format(f)) IO.imwrite(out, im)