Skip to content

Commit

Permalink
Fix bug in Instance Cam
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhenghao committed Dec 3, 2024
1 parent a179976 commit 492a3fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions metadrive/engine/base_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@


def generate_distinct_rgb_values():
r = np.linspace(0, 256, 16)
g = np.linspace(0, 256, 16)
b = np.linspace(0, 256, 16)
# Try to avoid (0,0,0) and (255,255,255) to avoid confusion with the background and other objects.
r = np.linspace(16, 256 - 16, 16).astype(int)
g = np.linspace(16, 256 - 16, 16).astype(int)
b = np.linspace(16, 256 - 16, 16).astype(int)

# Create a meshgrid and reshape to get all combinations of r, g, b
rgbs = np.array(np.meshgrid(r, g, b)).T.reshape(-1, 3)

# Normalize the values to be between 0 and 1
rgbs = rgbs / 255.0

# Round to 5 decimal places to avoid floating point errors
rgbs = np.round(rgbs, 5)

return tuple(tuple(v) for v in rgbs.tolist())
return tuple(tuple(round(vv, 5) for vv in v) for v in rgbs)


COLOR_SPACE = generate_distinct_rgb_values()
Expand Down
4 changes: 2 additions & 2 deletions metadrive/tests/test_sensors/test_instance_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_instance_cam(config, render=False):
env.reset()
base_free = len(env.engine.COLORS_FREE)
base_occupied = len(env.engine.COLORS_OCCUPIED)
assert base_free + base_occupied == 4096
assert base_free + base_occupied == env.engine.MAX_COLOR
import cv2
import time
start = time.time()
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_instance_cam(config, render=False):
assert env.engine.id_c[env.engine.c_id[color]] == color #Making sure the color-id is a bijection
assert len(env.engine.c_id.keys()) == len(env.engine.COLORS_OCCUPIED)
assert len(env.engine.id_c.keys()) == len(env.engine.COLORS_OCCUPIED)
assert len(env.engine.COLORS_FREE) + len(env.engine.COLORS_OCCUPIED) == 4096
assert len(env.engine.COLORS_FREE) + len(env.engine.COLORS_OCCUPIED) == env.engine.MAX_COLOR
#Making sure every object in the engine(not necessarily observable) have corresponding color
for id, object in env.engine.get_objects().items():
assert id in env.engine.id_c.keys()
Expand Down

0 comments on commit 492a3fa

Please sign in to comment.