Exemplo n.º 1
0
def render_data(render_image=True, render_annotation=True):
    t0 = time.time()
    scene = bpy.data.scenes[0]
    render = scene.render
    t0 = time.time()
    # render = scene
    for hook_name, hook in befor_render_data_hooks.items():
        hook()
    befor_render_data_hooks.clear()
    path = tempfile.NamedTemporaryFile().name
    render_result = {}
    if render_image:
        render_result["image"] = _render_image()
    if render_annotation:
        exr_path = path + ".exr"
        with set_inst_material(), set_annotation_render(), withattr(
            render, "filepath", exr_path
        ):
            bpy.ops.render.render(write_still=True)
        render_result["exr"] = parser_exr(exr_path)
        os.remove(exr_path)
    result = ImageWithAnnotation(**render_result)
    # if "render_6d_pose" and render_annotation:
    #     objs = [obj for obj in bpy.data.objects if "inst_id" in obj]
    #     ycb_6d_pose = get_6d_pose(objs, inst=result["inst"])
    #     result["ycb_6d_pose"] = ycb_6d_pose

    return result
Exemplo n.º 2
0
def render_data(render_image=True, render_annotation=True):
    path = pathjoin(tempfile.gettempdir(), "render_" + str(time.time()))
    render_result = {}
    if render_image:
        png_path = path + ".png"
        with set_image_render(), withattr(render, "filepath", png_path):
            print("Render image using:", render.engine)
            bpy.ops.render.render(write_still=True)
        render_result["image"] = imread(png_path)
        os.remove(png_path)

    if render_annotation:
        exr_path = path + ".exr"
        with set_inst_material(), set_annotation_render(), withattr(
            render, "filepath", exr_path
        ):
            print("Render annotation using:", render.engine)
            bpy.ops.render.render(write_still=True)
        render_result["exr"] = parser_exr(exr_path)
        os.remove(exr_path)
    result = ImageWithAnnotation(**render_result)
    if "render_6dof_pose" and "Camera" in bpy.data.objects:
        objs = [obj for obj in bpy.data.objects if "inst_id" in obj]
        ycb_meta = get_6dof_pose(bpy.data.objects["Camera"], objs, inst=result["inst"])
        result["ycb_meta"] = ycb_meta
    return result
Exemplo n.º 3
0
def render_image():
    render = bpy.data.scenes[0].render
    png_path = tempfile.NamedTemporaryFile().name + ".png"
    with set_image_render(), withattr(render, "filepath", png_path):
        bpy.ops.render.render(write_still=True)
    image = imread(png_path)[..., :3]
    os.remove(png_path)
    return image
Exemplo n.º 4
0
def render_data(render_image=True,
                render_annotation=True,
                render_6d_pose=False):
    scene = bpy.data.scenes[0]
    render = scene.render
    for hook_name, hook in befor_render_data_hooks.items():
        print(f"Run befor_render_data_hooks[{hook_name}]")
        hook()
    befor_render_data_hooks.clear()

    render_result = {}
    if render_image:
        render_result["image"] = _render_image()

    if render_annotation:
        """
        实例分割渲染
        """
        inst_path = tempfile.NamedTemporaryFile().name
        inst_exr_path = inst_path + ".exr"

        with set_inst_material(), set_annotation_render(), withattr(
                render, "filepath", inst_exr_path):
            print("Render annotation using:", render.engine)
            bpy.ops.render.render(write_still=True)
        render_result["exr"] = parser_exr(inst_exr_path)
        os.remove(inst_exr_path)
        """
        语义分割渲染
        """
        sem_path = tempfile.NamedTemporaryFile().name
        sem_path_exr_path = sem_path + ".exr"

        with set_sem_material(), set_annotation_render(), withattr(
                render, "filepath", sem_path_exr_path):
            print("Render annotation using:", render.engine)
            bpy.ops.render.render(write_still=True)
        sem_exr = parser_exr(sem_path_exr_path)
        os.remove(sem_path_exr_path)

    result = ImageWithAnnotation(**render_result)
    if render_annotation:
        result["sem"] = sem_exr.get_inst()

    if render_6d_pose and render_annotation:
        objs = [obj for obj in bpy.data.objects if "inst_id" in obj]
        ycb_6d_pose = get_6d_pose(objs, inst=result["inst"])
        result["ycb_6d_pose"] = ycb_6d_pose
    return result