Пример #1
0
def draw_map_gymd(map_name: str, output: AbsDirPath, style: str):
    try:
        from gym_duckietown.simulator import Simulator
    except ImportError:
        return

    sim = Simulator(
        map_name,
        enable_leds=True,
        domain_rand=False,
        num_tris_distractors=0,
        camera_width=640,
        camera_height=480,
        # distortion=True,
        color_ground=[0, 0.3, 0],  # green
        style=style,
    )

    sim.reset()

    logger.info("rendering obs")
    img = sim.render_obs()

    out = os.path.join(output, "cam.jpg")
    save_rgb_to_jpg(img, out)

    sim.cur_pos = [-100.0, -100.0, -100.0]
    from gym_duckietown.simulator import FrameBufferMemory

    td = FrameBufferMemory(width=1024, height=1024)
    # noinspection PyProtectedMember
    horiz = sim._render_img(
        width=td.width,
        height=td.height,
        multi_fbo=td.multi_fbo,
        final_fbo=td.final_fbo,
        img_array=td.img_array,
        top_down=True,
    )
    # img = sim.render("top_down")
    out = cast(FilePath, os.path.join(output, "top_down.jpg"))
    save_rgb_to_jpg(horiz, out)
Пример #2
0
def make_scenario_main(args=None):
    setup_logging()
    parser = argparse.ArgumentParser()

    # parser.add_argument("--config", help="Configuration", required=True)
    parser.add_argument("-o",
                        "--output",
                        help="Destination directory",
                        required=True)
    parser.add_argument("-n",
                        "--num",
                        type=int,
                        help="Number of scenarios to generate",
                        required=True)
    parser.add_argument(
        "--styles",
        default="synthetic-F",
        help=
        "Draw preview in various styles, comma separated. (needs gym duckietown)",
    )

    parsed, rest = parser.parse_known_args(args=args)
    if parsed.styles == "all":
        styles = ["synthetic", "synthetic-F", "photos", "smooth"]
    else:
        styles = parsed.styles.split(",")
    for config in rest:
        basename = os.path.basename(config).split(".")[0]
        data = read_ustring_from_utf8_file(config)
        interpreted = yaml.load(data, Loader=yaml.Loader)
        n: int = parsed.num
        output: str = parsed.output
        params: ScenarioGenerationParam = object_from_ipce(
            interpreted, ScenarioGenerationParam, iedo=iedo)
        for i in range(n):
            scenario_name = f"{basename}-{i:03d}"
            yaml_str = _get_map_yaml(params.map_name)
            scenario = make_scenario(
                yaml_str=yaml_str,
                scenario_name=scenario_name,
                only_straight=params.only_straight,
                min_dist=params.min_dist,
                delta_y_m=params.delta_y_m,
                robots_npcs=params.robots_npcs,
                robots_parked=params.robots_parked,
                robots_pcs=params.robots_pcs,
                nduckies=params.nduckies,
                duckie_min_dist_from_other_duckie=params.
                duckie_min_dist_from_other_duckie,
                duckie_min_dist_from_robot=params.duckie_min_dist_from_robot,
                duckie_y_bounds=params.duckie_y_bounds,
                delta_theta_rad=np.deg2rad(params.theta_tol_deg),
                pc_robot_protocol=params.pc_robot_protocol,
                npc_robot_protocol=params.npc_robot_protocol,
                tree_density=params.tree_density,
                tree_min_dist=params.tree_min_dist,
            )

            # styles = ['smooth']
            for style in styles:
                try:
                    from gym_duckietown.simulator import Simulator
                except ImportError:
                    logger.warning(traceback.format_exc())
                    # noinspection PyUnusedLocal
                    Simulator = None
                else:
                    sim = Simulator(
                        "4way",
                        enable_leds=True,
                        domain_rand=False,
                        num_tris_distractors=0,
                        camera_width=640,
                        camera_height=480,
                        # distortion=True,
                        color_ground=[0, 0.3, 0],  # green
                        style=style,
                    )
                    logger.info("resetting")
                    sim.reset()
                    m = cast(
                        dw.MapFormat1,
                        yaml.load(scenario.environment, Loader=yaml.Loader))

                    if "objects" not in m:
                        m["objects"] = {}
                    obs: Dict[str, object] = m["objects"]

                    for robot_name, srobot in scenario.robots.items():
                        st = dw.SE2Transform.from_SE2(
                            pose_from_friendly(srobot.configuration.pose))

                        obs[robot_name] = dict(kind="duckiebot",
                                               pose=st.as_json_dict(),
                                               height=0.12,
                                               color=srobot.color)

                    for duckie_name, duckie in scenario.duckies.items():
                        st = dw.SE2Transform.from_SE2(
                            pose_from_friendly(duckie.pose))
                        obs[duckie_name] = dict(kind="duckie",
                                                pose=st.as_json_dict(),
                                                height=0.08,
                                                color=duckie.color)

                    sim._interpret_map(m)
                    sim.reset()
                    logger.info("rendering obs")
                    img = sim.render_obs()
                    out = os.path.join(output, scenario_name, style, "cam.png")
                    save_rgb_to_png(img, out)
                    out = os.path.join(output, scenario_name, style, "cam.jpg")
                    save_rgb_to_jpg(img, out)

                    sim.cur_pos = [-100.0, -100.0, -100.0]
                    from gym_duckietown.simulator import FrameBufferMemory

                    td = FrameBufferMemory(width=1024, height=1024)
                    horiz = sim._render_img(
                        width=td.width,
                        height=td.height,
                        multi_fbo=td.multi_fbo,
                        final_fbo=td.final_fbo,
                        img_array=td.img_array,
                        top_down=True,
                    )
                    # img = sim.render("top_down")
                    out = cast(
                        FilePath,
                        os.path.join(output, scenario_name, style,
                                     "top_down.jpg"))
                    save_rgb_to_jpg(horiz, out)
                    out = cast(
                        FilePath,
                        os.path.join(output, scenario_name, style,
                                     "top_down.png"))
                    save_rgb_to_png(horiz, out)

                    dw.Tile.style = style
                    dm = interpret_scenario(scenario)
                    output_dir = os.path.join(output, scenario_name, style)
                    dw.draw_static(dm, output_dir=output_dir)
                    export_gltf(dm, output_dir, background=False)

            scenario_struct = ipce_from_object(scenario, Scenario, ieso=ieso)
            scenario_yaml = yaml.dump(scenario_struct)
            filename = os.path.join(output, scenario_name, f"scenario.yaml")
            write_ustring_to_utf8_file(scenario_yaml, filename)