|
4 | 4 | import gym |
5 | 5 | import numpy as np |
6 | 6 | from PIL import Image |
| 7 | +from scipy import ndimage |
7 | 8 |
|
8 | 9 | from balrog.environments import Strings |
9 | 10 |
|
@@ -99,6 +100,13 @@ def distange_to_string(distance, direction): |
99 | 100 | return " and ".join(desc) if desc else "at your location" |
100 | 101 |
|
101 | 102 |
|
| 103 | +def get_edge_items(semantic, item_idx): |
| 104 | + item_mask = semantic == item_idx |
| 105 | + not_item_mask = semantic != item_idx |
| 106 | + item_edge = ndimage.binary_dilation(not_item_mask) & item_mask |
| 107 | + return item_edge |
| 108 | + |
| 109 | + |
102 | 110 | def describe_env(info): |
103 | 111 | assert info["semantic"][info["player_pos"][0], info["player_pos"][1]] == player_idx |
104 | 112 | semantic = info["semantic"][ |
@@ -126,12 +134,24 @@ def describe_env(info): |
126 | 134 | else: |
127 | 135 | obs = "You face nothing at your front." |
128 | 136 |
|
| 137 | + # Edge detection |
| 138 | + edge_only_items = ["water"] |
| 139 | + edge_masks = {} |
| 140 | + |
| 141 | + for item_name in edge_only_items: |
| 142 | + item_idx = id_to_item.index(item_name) |
| 143 | + edge_masks[item_idx] = get_edge_items(semantic, item_idx) |
| 144 | + |
129 | 145 | for i in range(semantic.shape[0]): |
130 | 146 | for j in range(semantic.shape[1]): |
131 | 147 | idx = semantic[i, j] |
132 | 148 | if idx == player_idx: |
133 | 149 | continue |
134 | 150 |
|
| 151 | + # only display the edge of items that are in edge_only_items |
| 152 | + if idx in edge_masks and not edge_masks[idx][i, j]: |
| 153 | + continue |
| 154 | + |
135 | 155 | # skip grass, sand or path so obs is not too long |
136 | 156 | if idx in [id_to_item.index(o) for o in ["grass", "sand", "path"]]: |
137 | 157 | continue |
|
0 commit comments