Skip to content

Commit

Permalink
semantic camera test
Browse files Browse the repository at this point in the history
  • Loading branch information
QuanyiLi committed Oct 24, 2023
1 parent f456171 commit 90115e0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
3 changes: 1 addition & 2 deletions metadrive/component/sensors/depth_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __init__(self, width, height, engine, *, cuda=False):
# vert_path = AssetLoader.file_path("shaders", "depth_cam_gles.vert.glsl")
# frag_path = AssetLoader.file_path("shaders", "depth_cam_gles.frag.glsl")
# else:
self.setup_effect()
if self.VIEW_GROUND:
ground = PNMImage(513, 513, 4)
ground.fill(1., 1., 1.)
Expand All @@ -64,7 +63,7 @@ def track(self, base_object):
# self.GROUND_MODEL.setR(-base_object.origin.getR())
return super(DepthCamera, self).track(base_object)

def setup_effect(self):
def _setup_effect(self):
"""
Setup Camera Effect enabling depth calculation
Expand Down
2 changes: 1 addition & 1 deletion metadrive/component/sensors/rgb_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, width, height, engine, *, cuda=False):
cam.lookAt(0, 10.4, 1.6)
lens.setFov(60)

def setup_effect(self):
def _setup_effect(self):
"""
Setup simple PBR effect
Returns: None
Expand Down
4 changes: 2 additions & 2 deletions metadrive/component/sensors/semantic_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def track(self, base_object):
# self.GROUND_MODEL.setR(-base_object.origin.getR())
return super(SemanticCamera, self).track(base_object)

def setup_effect(self):
def _setup_effect(self):
"""
Use tag to apply color to different object class
Returns: None
Expand All @@ -82,7 +82,7 @@ def setup_effect(self):
label, c = getattr(Semantics, t)
cam.setTagState(label, RenderState.make(ColorAttrib.makeFlat((c[0] / 255, c[1] / 255, c[2] / 255, 1)), 1))

def create_buffer(self, width, height, frame_buffer_property):
def _create_buffer(self, width, height, frame_buffer_property):
"""
The buffer should be created without frame_buffer_property
Args:
Expand Down
8 changes: 4 additions & 4 deletions metadrive/engine/core/image_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
return

# self.texture = Texture()
self.buffer = self.create_buffer(width, height, frame_buffer_property)
self.buffer = self._create_buffer(width, height, frame_buffer_property)
self.origin = NodePath("new render")

# this takes care of setting up their camera properly
Expand All @@ -67,10 +67,10 @@ def __init__(
self.cam.node().setCameraMask(self.CAM_MASK)
if parent_node is not None:
self.origin.reparentTo(parent_node)
self.setup_effect()
self._setup_effect()
self.logger.debug("Load Image Buffer: {}".format(self.__class__.__name__))

def create_buffer(self, width, height, frame_buffer_property):
def _create_buffer(self, width, height, frame_buffer_property):
"""
Create the buffer object to render the scene into it
Args:
Expand All @@ -86,7 +86,7 @@ def create_buffer(self, width, height, frame_buffer_property):
frame_buffer_property.set_rgba_bits(*self.frame_buffer_rgb_bits) # disable alpha for RGB camera
return self.engine.win.makeTextureBuffer("camera", width, height, fbp=frame_buffer_property)

def setup_effect(self):
def _setup_effect(self):
"""
Apply effect to the render the scene. Usually setup shader here
Returns: None
Expand Down
3 changes: 2 additions & 1 deletion metadrive/tests/test_sensors/test_depth_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@pytest.mark.parametrize("config", list(blackbox_test_configs.values()), ids=list(blackbox_test_configs.keys()))
def test_depth_cam(config, render=False):
"""
Test the output shape of rgb camera. This can not make sure the correctness of rendered image
Test the output shape of Depth camera. This can not make sure the correctness of rendered image but only for
checking the shape of image output and image retrieve pipeline
Args:
config: test parameter
render: render with cv2
Expand Down
3 changes: 2 additions & 1 deletion metadrive/tests/test_sensors/test_rgb_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@pytest.mark.parametrize("config", list(blackbox_test_configs.values()), ids=list(blackbox_test_configs.keys()))
def test_rgb_cam(config, render=False):
"""
Test the output shape of rgb camera. This can not make sure the correctness of rendered image
Test the output shape of rgb camera. This can not make sure the correctness of rendered image but only for
checking the shape of image output and image retrieve pipeline
Args:
config: test parameter
render: render with cv2
Expand Down
58 changes: 58 additions & 0 deletions metadrive/tests/test_sensors/test_semantic_cam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest

from metadrive.component.sensors.semantic_camera import SemanticCamera
from metadrive.envs.metadrive_env import MetaDriveEnv

blackbox_test_configs = dict(
standard=dict(stack_size=3, width=256, height=128, rgb_clip=True),
small=dict(stack_size=1, width=64, height=32, rgb_clip=False),
large=dict(stack_size=5, width=800, height=600, rgb_clip=True),
no_clip=dict(stack_size=3, width=800, height=600, rgb_clip=False),
)


@pytest.mark.parametrize("config", list(blackbox_test_configs.values()), ids=list(blackbox_test_configs.keys()))
def test_semantic_cam(config, render=False):
"""
Test the output shape of Semantic camera. This can NOT make sure the correctness of rendered image but only for
checking the shape of image output and image retrieve pipeline
Args:
config: test parameter
render: render with cv2
Returns: None
"""
env = MetaDriveEnv(
{
"num_scenarios": 1,
"traffic_density": 0.1,
"map": "S",
"start_seed": 4,
"stack_size": config["stack_size"],
"vehicle_config": dict(image_source="camera"),
"sensors": {
"camera": (SemanticCamera, config["width"], config["height"])
},
"interface_panel": ["dashboard", "camera"],
"image_observation": True, # it is a switch telling metadrive to use rgb as observation
"rgb_clip": config["rgb_clip"], # clip rgb to range(0,1) instead of (0, 255)
}
)
env.reset()
try:
import cv2
for i in range(1, 10):
o, r, tm, tc, info = env.step([0, 1])
assert env.observation_space.contains(o)
# Reverse
assert o["image"].shape == (config["height"], config["width"], 4, config["stack_size"])
if render:
cv2.imshow('img', o["image"][..., -1])
cv2.waitKey(1)
finally:
env.close()


if __name__ == '__main__':
test_semantic_cam(config=blackbox_test_configs["small"], render=True)

0 comments on commit 90115e0

Please sign in to comment.