Esempio n. 1
0
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)
Esempio n. 2
0
def test_render_cube():
    import pynutmeg
    import time

    fig = pynutmeg.figure('cube', 'figs/imshow.qml')

    im = empty((720, 1280, 3), np.uint8)

    azi = 0
    alt = np.deg2rad(40)
    dist = 10
    cam = Geom.cam_params(29.97, 1280, 720, 35)  # Blender defaults
    cam = cam.astype(np.float32)

    mesh = get_cube_mesh(1)
    obj_mesh = Mesh()

    n = 0
    while True:
        n += 1
        azi += np.deg2rad(2)

        tw = dist * np.array([cos(alt)*cos(azi), cos(alt)*sin(azi), sin(alt)], np.float32)

        alpha = -np.pi/2 - alt
        beta = np.pi/2 + azi
        Rx = np.array([
            [1, 0, 0],
            [0, cos(alpha), -sin(alpha)],
            [0, sin(alpha), cos(alpha)]
        ], np.float32)
        Rz = np.array([
            [cos(beta), -sin(beta), 0],
            [sin(beta), cos(beta), 0],
            [0, 0, 1]
        ], np.float32)
        Rw = dot(Rz, Rx)

        t = -dot(Rw.T, tw)
        R = Rw.T

        im[:] = 255
        render_frame(im, obj_mesh, mesh, R, t, cam)

        time.sleep(0.005)

        fig.set('ax.im', binary=im)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
def test_contours_rgb():
    import Edge_Tracker

    seq = 'data/synth/plant_out'
    out_d = os.path.join(seq, 'outlines_out')
    IO.imshow(zeros((3, 3, 3), np.uint8))

    try:
        os.makedirs(out_d)
    except FileExistsError:
        pass

    h, w = 360, 640
    cam = Geom.cam_params(f=29.9, sw=35.0, w=w, h=h)
    fx, fy, cx, cy = cam

    def to_homogenious(pts):
        N = pts.shape[0]
        out = empty((N, 3))
        out[:, 2] = 1
        out[:, 0] = (pts[:, 0] - cx) * (1 / fx)
        out[:, 1] = (pts[:, 1] - cy) * (1 / fy)
        return out

    def from_homogenious(pts):
        x = (fx * pts[:, 0] + cx).astype(int)
        y = (fy * pts[:, 1] + cy).astype(int)
        return x, y

    i = 0
    contour_set = []
    normal_set = []
    P_last = None
    pose = zeros(6)
    try:
        for im, D in load_datas(seq, imext='png'):
            print("Frame {}".format(i))
            P, normals, edge_map = find_contours_rgb(im, D)
            P_hom = to_homogenious(P)

            H, W = im.shape[:2]

            if P_last is not None:
                P_tr, pose, tree = Edge_Tracker.align(P_last, P_hom, pose=pose)

                P_tr = from_homogenious(P_tr)
                np.clip(P_tr[0], 0, W - 1, out=P_tr[0])
                np.clip(P_tr[1], 0, H - 1, out=P_tr[1])

                align = np.zeros_like(im)

                last_u, last_v = from_homogenious(P_last)

                align[P[:, 1], P[:, 0], 2] = 255
                align[last_v, last_u, 1] = 255
                align[P_tr[1], P_tr[0], 0] = 255

                IO.imshow(align, "align")
            # outlines, contours, normals = find_contours_rgb(im, D, snakes=False)
            # imio.imsave(os.path.join(out_d, "{:04d}.png".format(i)), outlines.astype(np.uint16)*2**3)
            # contour_set.append(contours)
            # normal_set.append(normals)
            # time.sleep(1)

            P_last = P_hom

            i += 1
    except Exception as e:
        print(e)
        raise

    np.savez(os.path.join(seq, 'contours.npz'),
             contours=contour_set,
             normals=normal_set)