예제 #1
0
def default_scene(obj_file,
                  width=640,
                  height=480,
                  use_distortion=True,
                  integer_pixel_centers=True):

    mesh_trimesh = trimesh.load(obj_file)

    mesh = ColoredTriMesh.from_trimesh(mesh_trimesh)

    # rot = Rotation.from_euler("xyz", [180, 0, 0], degrees=True).as_matrix()
    rot = Rotation.from_euler("xyz", [180, 0, 0], degrees=True).as_matrix()

    camera = differentiable_renderer.default_camera(width, height, 80,
                                                    mesh.vertices, rot)
    if use_distortion:
        camera.distortion = np.array([-0.5, 0.5, 0, 0, 0])

    bg_color = np.array((0.8, 0.8, 0.8))
    scene = differentiable_renderer.Scene3D(
        integer_pixel_centers=integer_pixel_centers)
    light_ambient = 0
    light_directional = 0.3 * np.array([1, -1, 0])
    scene.set_light(light_directional=light_directional,
                    light_ambient=light_ambient)
    scene.set_mesh(mesh)
    scene.set_background_color(bg_color)
    return scene, camera
예제 #2
0
    def __init__(
        self,
        file_or_mesh,
        display_texture_map=True,
        width=640,
        height=480,
        display_fps=True,
        title=None,
        use_moderngl=False,
        light_directional=(0, 0, -0.5),
        light_ambient=0.5,
        background_color=(1, 1, 1),
        use_antialiasing=True,
        use_light=True,
        fps_exp_average_decay=0.1,
        horizontal_fov=60,
        video_pattern="deodr_viewer_recording{id}.avi",
        video_format="MJPG",
    ):
        self.title = title
        self.scene = differentiable_renderer.Scene3D(sigma=1)
        self.set_mesh(file_or_mesh)
        self.windowname = f"DEODR mesh viewer:{self.title}"

        self.width = width
        self.height = height
        self.display_fps = display_fps
        self.use_moderngl = use_moderngl
        self.use_antialiasing = use_antialiasing
        self.use_light = use_light
        self.fps_exp_average_decay = fps_exp_average_decay
        self.last_time = None
        self.horizontal_fov = horizontal_fov
        self.video_writer = None
        self.recording = False
        self.video_pattern = video_pattern
        self.video_format = video_format

        if display_texture_map:
            self.display_texture_map()

        self.set_background_color(background_color)
        self.set_light(light_directional, light_ambient)
        self.recenter_camera()

        if use_moderngl:
            self.setup_moderngl()
        else:
            self.offscreen_renderer = None

        self.register_keys()
예제 #3
0
파일: mesh_viewer.py 프로젝트: WYK96/DEODR
def mesh_viewer(
        file_or_mesh,
        display_texture_map=True,
        width=640,
        height=480,
        display_fps=True,
        title=None,
        use_moderngl=False,
        light_directional=(0, 0, -0.5),
        light_ambient=0.5,
):
    if isinstance(file_or_mesh, str):
        if title is None:
            title = file_or_mesh
        mesh_trimesh = trimesh.load(file_or_mesh)
        mesh = ColoredTriMesh.from_trimesh(mesh_trimesh)
    elif isinstance(file_or_mesh, trimesh.base.Trimesh):
        mesh_trimesh = file_or_mesh
        mesh = ColoredTriMesh.from_trimesh(mesh_trimesh)
        if title is None:
            title = "unknown"
    elif isinstance(file_or_mesh, ColoredTriMesh):
        mesh = file_or_mesh
        if title is None:
            title = "unknown"
    else:
        raise (BaseException(
            f"unknown type {type(file_or_mesh)}for input obj_file_or_trimesh,"
            " can be string or trimesh.base.Trimesh"))

    if display_texture_map:
        ax = plt.subplot(111)
        if mesh.textured:
            mesh.plot_uv_map(ax)

    object_center = 0.5 * (mesh.vertices.max(axis=0) +
                           mesh.vertices.min(axis=0))
    object_radius = np.max(
        mesh.vertices.max(axis=0) - mesh.vertices.min(axis=0))

    camera_center = object_center + np.array([0, 0, 3]) * object_radius

    rotation = np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]])
    translation = -rotation.T.dot(camera_center)
    extrinsic = np.column_stack((rotation, translation))

    focal = 2 * width
    intrinsic = np.array([[focal, 0, width / 2], [0, focal, height / 2],
                          [0, 0, 1]])

    distortion = [0, 0, 0, 0, 0]
    camera = differentiable_renderer.Camera(
        extrinsic=extrinsic,
        intrinsic=intrinsic,
        width=width,
        height=height,
        distortion=distortion,
    )
    use_antiliazing = True
    use_light = True

    scene = differentiable_renderer.Scene3D(sigma=1)

    scene.set_light(light_directional=np.array(light_directional),
                    light_ambient=light_ambient)
    scene.set_mesh(mesh)

    scene.set_background_color([1, 1, 1])

    if mesh.texture is not None:
        mesh.texture = mesh.texture[:, :, ::
                                    -1]  # convert texture to GBR to avoid future conversion when ploting in Opencv

    fps = 0
    fps_decay = 0.1
    windowname = f"DEODR mesh viewer:{title}"

    interactor = Interactor(
        camera=camera,
        object_center=object_center,
        z_translation_speed=0.01 * object_radius,
        xy_translation_speed=3e-4,
    )

    cv2.namedWindow(windowname, cv2.WINDOW_NORMAL)
    cv2.resizeWindow(windowname, width, height)
    cv2.setMouseCallback(windowname, interactor.mouse_callback)

    if use_moderngl:
        import deodr.opengl.moderngl

        offscreen_renderer = deodr.opengl.moderngl.OffscreenRenderer()
        scene.mesh.compute_vertex_normals()
        offscreen_renderer.set_scene(scene)
    else:
        offscreen_renderer = None
    while cv2.getWindowProperty(windowname, 0) >= 0:

        # mesh.set_vertices(mesh.vertices+np.random.randn(*mesh.vertices.shape)*0.001)
        width, height = cv2.getWindowImageRect(windowname)[2:]
        focal = 2 * width
        intrinsic = np.array([[focal, 0, width / 2], [0, focal, height / 2],
                              [0, 0, 1]])
        camera.intrinsic = intrinsic
        camera.width = width
        camera.height = height

        start = time.clock()
        if use_moderngl:
            image = offscreen_renderer.render(camera)
        else:
            image = scene.render(interactor.camera)

        if display_fps:
            font = cv2.FONT_HERSHEY_SIMPLEX
            bottom_left_corner_of_text = (20, height - 20)
            font_scale = 1
            font_color = (0, 0, 255)
            thickness = 2
            cv2.putText(
                image,
                "fps:%0.1f" % fps,
                bottom_left_corner_of_text,
                font,
                font_scale,
                font_color,
                thickness,
            )
        cv2.imshow(windowname, image)
        stop = time.clock()
        fps = (1 - fps_decay) * fps + fps_decay * (1 / (stop - start))
        key = cv2.waitKey(1)
        if key >= 0:
            if key == ord("r"):
                # change renderer between DEODR cpu rendering and moderngl
                use_moderngl = not (use_moderngl)
                print(f"use_moderngl = {use_moderngl}")

                if offscreen_renderer is None:
                    offscreen_renderer = deodr.opengl.moderngl.OffscreenRenderer(
                    )
                    scene.mesh.compute_vertex_normals()
                    offscreen_renderer.set_scene(scene)

            if key == ord("p"):
                if use_moderngl:
                    print(
                        "can only use perspective corect mapping  when using moderngl"
                    )
                else:
                    # toggle perspective correct mapping (texture or interpolation)
                    scene.perspective_correct = not (scene.perspective_correct)
                    print(f"perspective_correct = {scene.perspective_correct}")

            if key == ord("l"):
                # toggle directional light + ambient vs ambient = 1
                use_light = not (use_light)
                print(f"use_light = {use_light}")
                if use_light:
                    if use_moderngl:
                        offscreen_renderer.set_light(
                            light_directional=np.array(light_directional),
                            light_ambient=light_ambient,
                        )
                    else:
                        scene.set_light(
                            light_directional=np.array(light_directional),
                            light_ambient=light_ambient,
                        )
                else:
                    if use_moderngl:
                        offscreen_renderer.set_light(
                            light_directional=(0, 0, 0),
                            light_ambient=1.0,
                        )
                    else:
                        scene.set_light(light_directional=None,
                                        light_ambient=1.0)

            if key == ord("a"):
                # toggle edge overdraw anti-aliasing
                if use_moderngl:
                    print("no anti-alizaing available when using moderngl")
                else:
                    use_antiliazing = not (use_antiliazing)
                    print(f"use_antialiazing = {use_antiliazing}")
                    if use_antiliazing:
                        scene.sigma = 1.0
                    else:
                        scene.sigma = 0.0

            if key == ord("s"):
                filename = os.path.abspath("scene.pickle")
                # save scene and camera in pickle file
                with open(filename, "wb") as file:
                    # dump information to the file
                    pickle.dump(scene, file)
                print(f"saved scene in {filename}")

                filename = os.path.abspath("camera.pickle")
                print(f"save scene in {filename}")
                with open(filename, "wb") as file:
                    # dump information to the file
                    pickle.dump(camera, file)
                print(f"saved camera in {filename}")
예제 #4
0
def mesh_viewer(
        obj_file_or_trimesh,
        display_texture_map=True,
        width=640,
        height=480,
        display_fps=True,
        title=None,
        use_moderngl=False,
        light_directional=(-0.5, 0, -0.5),
        light_ambient=0.3,
):
    if type(obj_file_or_trimesh) == str:
        if title is None:
            title = obj_file_or_trimesh
        mesh_trimesh = trimesh.load(obj_file_or_trimesh)
    elif type(obj_file_or_trimesh) == trimesh.base.Trimesh:
        mesh_trimesh = obj_file_or_trimesh
        if title is None:
            title = "unknown"
    else:
        raise (BaseException(
            f"unknown type {type(obj_file_or_trimesh)}for input obj_file_or_trimesh,"
            " can be string or trimesh.base.Trimesh"))

    mesh = ColoredTriMesh.from_trimesh(mesh_trimesh)
    if display_texture_map:
        ax = plt.subplot(111)
        if mesh.textured:
            mesh.plot_uv_map(ax)

    object_center = 0.5 * (mesh.vertices.max(axis=0) +
                           mesh.vertices.min(axis=0))
    object_radius = np.max(
        mesh.vertices.max(axis=0) - mesh.vertices.min(axis=0))

    camera_center = object_center + np.array([0, 0, 3]) * object_radius
    focal = 2 * width

    rotation = np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]])
    translation = -rotation.T.dot(camera_center)
    extrinsic = np.column_stack((rotation, translation))
    intrinsic = np.array([[focal, 0, width / 2], [0, focal, height / 2],
                          [0, 0, 1]])

    distortion = [0, 0, 0, 0, 0]
    camera = differentiable_renderer.Camera(
        extrinsic=extrinsic,
        intrinsic=intrinsic,
        width=width,
        height=height,
        distortion=distortion,
    )
    use_antiliazing = True
    use_light = True

    scene = differentiable_renderer.Scene3D(sigma=1)

    scene.set_light(light_directional=np.array(light_directional),
                    light_ambient=light_ambient)
    scene.set_mesh(mesh)
    background_image = np.ones((height, width, 3))
    scene.set_background(background_image)

    if mesh.texture is not None:
        mesh.texture = mesh.texture[:, :, ::
                                    -1]  # convert texture to GBR to avoid future conversion when ploting in Opencv

    fps = 0
    fps_decay = 0.1
    windowname = f"DEODR mesh viewer:{title}"

    interactor = Interactor(
        camera=camera,
        object_center=object_center,
        z_translation_speed=0.01 * object_radius,
        xy_translation_speed=3e-4,
    )

    cv2.namedWindow(windowname)
    cv2.setMouseCallback(windowname, interactor.mouse_callback)

    if use_moderngl:
        import deodr.opengl.moderngl

        offscreen_renderer = deodr.opengl.moderngl.OffscreenRenderer()
        scene.mesh.compute_vertex_normals()
        offscreen_renderer.set_scene(scene)

    while cv2.getWindowProperty(windowname, 0) >= 0:
        # mesh.set_vertices(mesh.vertices+np.random.randn(*mesh.vertices.shape)*0.001)
        start = time.clock()
        if use_moderngl:
            image = offscreen_renderer.render(camera)
        else:
            image = scene.render(interactor.camera)

        if display_fps:
            font = cv2.FONT_HERSHEY_SIMPLEX
            bottom_left_corner_of_text = (20, height - 20)
            font_scale = 1
            font_color = (0, 0, 255)
            thickness = 2
            cv2.putText(
                image,
                "fps:%0.1f" % fps,
                bottom_left_corner_of_text,
                font,
                font_scale,
                font_color,
                thickness,
            )

        cv2.imshow(windowname, image)
        stop = time.clock()
        fps = (1 - fps_decay) * fps + fps_decay * (1 / (stop - start))
        key = cv2.waitKey(1)
        if key >= 0:
            if key == ord("p"):
                # toggle perspective correct mapping (texture or interpolation)
                scene.perspective_correct = not (scene.perspective_correct)
                print(f"perspective_correct = {scene.perspective_correct}")

            if key == ord("l"):
                # toggle directional light + ambient vs ambient = 1
                use_light = not (use_light)
                if use_light:
                    scene.set_light(
                        light_directional=np.array(light_directional),
                        light_ambient=light_ambient,
                    )
                else:
                    scene.set_light(light_directional=None, light_ambient=1.0)

            if key == ord("a"):
                # toggle edge overdraw anti-aliasing
                use_antiliazing = not (use_antiliazing)
                if use_antiliazing:
                    scene.sigma = 1.0
                else:
                    scene.sigma = 0.0