From 55eb9c515dcec4dce64e4a64222efbbd12494a79 Mon Sep 17 00:00:00 2001 From: hagrid67 <jdhwatson@gmail.com> Date: Wed, 21 Aug 2019 14:07:10 +0100 Subject: [PATCH] added render show_debug to show agent index - issue 146 --- docs/flatland.rst | 20 ++++++++++---------- flatland/utils/graphics_pil.py | 26 +++++++++++++++++++++----- flatland/utils/rendertools.py | 14 +++++++++----- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/docs/flatland.rst b/docs/flatland.rst index 88e8ec93..e09087a4 100644 --- a/docs/flatland.rst +++ b/docs/flatland.rst @@ -6,10 +6,10 @@ Subpackages .. toctree:: - flatland.core - flatland.envs - flatland.evaluators - flatland.utils + flatland.core + flatland.envs + flatland.evaluators + flatland.utils Submodules ---------- @@ -18,15 +18,15 @@ flatland.cli module ------------------- .. automodule:: flatland.cli - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: Module contents --------------- .. automodule:: flatland - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index f86f301c..42cbfeaa 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -4,7 +4,7 @@ import time import tkinter as tk import numpy as np -from PIL import Image, ImageDraw, ImageTk # , ImageFont +from PIL import Image, ImageDraw, ImageTk, ImageFont from numpy import array from pkg_resources import resource_string as resource_bytes @@ -90,6 +90,8 @@ class PILGL(GraphicsLayer): self.old_background_image = (None, None, None) self.create_layers() + self.font = ImageFont.load_default() + def build_background_map(self, dTargets): x = self.old_background_image rebuild = False @@ -167,8 +169,14 @@ class PILGL(GraphicsLayer): # quit but not destroy! self.__class__.window.quit() - def text(self, *args, **kwargs): - pass + def text(self, xPx, yPx, strText, layer=RAIL_LAYER): + xyPixLeftTop = (xPx, yPx) + self.draws[layer].text(xyPixLeftTop, strText, font=self.font, fill=(0, 0, 0, 255)) + + def text_rowcol(self, rcTopLeft, strText, layer=AGENT_LAYER): + print("Text:", "rc:", rcTopLeft, "text:", strText, "layer:", layer) + xyPixLeftTop = tuple((array(rcTopLeft) * self.nPixCell)[[1, 0]]) + self.text(*xyPixLeftTop, strText, layer) def prettify(self, *args, **kwargs): pass @@ -492,13 +500,17 @@ class PILSVG(PILGL): False)[0] self.draw_image_row_col(colored_rail, (row, col), layer=PILGL.PREDICTION_PATH_LAYER) - def set_rail_at(self, row, col, binary_trans, target=None, is_selected=False, rail_grid=None): + def set_rail_at(self, row, col, binary_trans, target=None, is_selected=False, rail_grid=None, + show_debug=True): + if binary_trans in self.pil_rail: pil_track = self.pil_rail[binary_trans] if target is not None: target_img = self.station_colors[target % len(self.station_colors)] target_img = Image.alpha_composite(pil_track, target_img) self.draw_image_row_col(target_img, (row, col), layer=PILGL.TARGET_LAYER) + if show_debug: + self.text_rowcol((row+0.8, col+0.0), strText=str(target), layer=PILGL.TARGET_LAYER) if binary_trans == 0: if self.background_grid[col][row] <= 4: @@ -579,7 +591,7 @@ class PILSVG(PILGL): for color_idx, pil_zug_3 in enumerate(pils): self.pil_zug[(in_direction_2, out_direction_2, color_idx)] = pils[color_idx] - def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected): + def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected, show_debug=False): delta_dir = (out_direction - in_direction) % 4 color_idx = agent_idx % self.n_agent_colors # when flipping direction at a dead end, use the "out_direction" direction. @@ -593,6 +605,10 @@ class PILSVG(PILGL): self.clear_layer(PILGL.SELECTED_AGENT_LAYER, 0) self.draw_image_row_col(bg_svg, (row, col), layer=PILGL.SELECTED_AGENT_LAYER) + if show_debug: + print("Call text:") + self.text_rowcol((row+0.2, col+0.2,), str(agent_idx)) + def set_cell_occupied(self, agent_idx, row, col): occupied_im = self.cell_occupied[agent_idx % len(self.cell_occupied)] self.draw_image_row_col(occupied_im, (row, col), 1) diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py index 8974126a..99d30166 100644 --- a/flatland/utils/rendertools.py +++ b/flatland/utils/rendertools.py @@ -39,7 +39,9 @@ class RenderTool(object): theta = np.linspace(0, np.pi / 2, 5) arc = array([np.cos(theta), np.sin(theta)]).T # from [1,0] to [0,1] - def __init__(self, env, gl="PILSVG", jupyter=False, agent_render_variant=AgentRenderVariant.ONE_STEP_BEHIND): + def __init__(self, env, gl="PILSVG", jupyter=False, + agent_render_variant=AgentRenderVariant.ONE_STEP_BEHIND, + show_debug=True): self.env = env self.frame_nr = 0 self.start_time = time.time() @@ -56,6 +58,7 @@ class RenderTool(object): self.gl = PILSVG(env.width, env.height, jupyter) self.new_rail = True + self.show_debug = show_debug self.update_background() def reset(self): @@ -282,7 +285,7 @@ class RenderTool(object): if len(observation_dict) < 1: warnings.warn( "Predictor did not provide any predicted cells to render. \ - Observaiton builder needs to populate: env.dev_obs_dict") + Observation builder needs to populate: env.dev_obs_dict") else: for agent in agent_handles: color = self.gl.get_agent_color(agent) @@ -525,7 +528,7 @@ class RenderTool(object): is_selected = False self.gl.set_rail_at(r, c, transitions, target=target, is_selected=is_selected, - rail_grid=env.rail.grid) + rail_grid=env.rail.grid, show_debug=self.show_debug) self.gl.build_background_map(targets) @@ -550,7 +553,8 @@ class RenderTool(object): # set_agent_at uses the agent index for the color if self.agent_render_variant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX: self.gl.set_cell_occupied(agent_idx, *(agent.position)) - self.gl.set_agent_at(agent_idx, *position, old_direction, direction, selected_agent == agent_idx) + self.gl.set_agent_at(agent_idx, *position, old_direction, direction, + selected_agent == agent_idx, show_debug=self.show_debug) else: position = agent.position direction = agent.direction @@ -562,7 +566,7 @@ class RenderTool(object): # set_agent_at uses the agent index for the color self.gl.set_agent_at(agent_idx, *position, agent.direction, direction, - selected_agent == agent_idx) + selected_agent == agent_idx, show_debug=self.show_debug) # set_agent_at uses the agent index for the color if self.agent_render_variant == AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX: -- GitLab