Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageBuffer: prevent additional copy converting from RGBA to RGB #503

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metadrive/component/sensors/base_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def perceive(self, base_object, clip=True) -> np.ndarray:
if self.engine.global_config["rgb_to_grayscale"]:
ret = np.dot(ret[..., :3], [0.299, 0.587, 0.114])
if not clip:
return ret.astype(np.uint8)
return ret.astype(np.uint8, copy=False, order="C")
else:
return ret / 255

Expand Down
8 changes: 5 additions & 3 deletions metadrive/engine/core/image_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Union, List

import numpy as np
from panda3d.core import NodePath, Vec3, Vec4, Camera, PNMImage, Shader, RenderState, ShaderAttrib
from panda3d.core import NodePath, Vec3, Vec4, Camera, PNMImage, Shader, RenderState, ShaderAttrib, FrameBufferProperties

from metadrive.constants import RENDER_MODE_ONSCREEN, BKG_COLOR, RENDER_MODE_NONE

Expand Down Expand Up @@ -52,6 +52,9 @@ def __init__(
self.lens = self.cam.node().getLens()
return

frame_buffer_property = FrameBufferProperties()
frame_buffer_property.set_rgba_bits(8, 8, 8, 0) # disable alpha for RGB camera

# self.texture = Texture()
if frame_buffer_property is None:
self.buffer = self.engine.win.makeTextureBuffer("camera", width, height)
Expand Down Expand Up @@ -102,10 +105,9 @@ def __init__(
def get_rgb_array_cpu(self):
origin_img = self.buffer.getDisplayRegion(1).getScreenshot()
img = np.frombuffer(origin_img.getRamImage().getData(), dtype=np.uint8)
img = img.reshape((origin_img.getYSize(), origin_img.getXSize(), 4))
img = img.reshape((origin_img.getYSize(), origin_img.getXSize(), 3))
# img = np.swapaxes(img, 1, 0)
img = img[::-1]
img = img[..., :-1]
return img

@staticmethod
Expand Down
Loading