Skip to content

Commit

Permalink
support no numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
jjshoots committed Jul 25, 2023
1 parent 204e61e commit bcf519d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
3 changes: 2 additions & 1 deletion PyFlyt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

# Throw error if pybullet is not installed with numpy support.
if not pybullet.isNumpyEnabled():
raise RuntimeError(
raise RuntimeWarning(
"PyBullet is not installed properly with Numpy functionality,\n"
"This will result in a significant performance hit when using vision.\n'"
"Please fix this by installing Numpy again, then rebuilding PyBullet:\n"
"\tpip3 uninstall pybullet -y\n"
"\tpip3 install numpy\n"
Expand Down
10 changes: 10 additions & 0 deletions PyFlyt/core/abstractions/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,14 @@ def capture_image(self) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
projectionMatrix=self.proj_mat,
)

rgbaImg = np.asarray(rgbaImg).reshape(
self.camera_resolution[0], self.camera_resolution[1], -1
)
depthImg = np.asarray(depthImg).reshape(
self.camera_resolution[0], self.camera_resolution[1], -1
)
segImg = np.asarray(segImg).reshape(
self.camera_resolution[0], self.camera_resolution[1], -1
)

return rgbaImg, depthImg, segImg
4 changes: 4 additions & 0 deletions PyFlyt/gym_envs/fixedwing_envs/fixedwing_base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,8 @@ def render(self):
projectionMatrix=self.env.drones[0].camera.proj_mat,
)

rgbaImg = np.asarray(rgbaImg).reshape(
self.render_resolution[0], self.render_resolution[1], -1
)

return rgbaImg
4 changes: 4 additions & 0 deletions PyFlyt/gym_envs/quadx_envs/quadx_base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ def render(self):
projectionMatrix=self.camera_parameters[3],
)

rgbaImg = np.asarray(rgbaImg).reshape(
self.render_resolution[0], self.render_resolution[1], -1
)

return rgbaImg
5 changes: 5 additions & 0 deletions PyFlyt/gym_envs/rocket_envs/rocket_base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,9 @@ def render(self):
viewMatrix=self.env.drones[0].camera.view_mat,
projectionMatrix=self.env.drones[0].camera.proj_mat,
)

rgbaImg = np.asarray(rgbaImg).reshape(
self.render_resolution[0], self.render_resolution[1], -1
)

return rgbaImg
24 changes: 18 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,24 @@ def test_camera():
env.step()

# check the camera image
assert isinstance(env.drones[0].rgbaImg, np.ndarray), f"Expected camera image to be of type `np.ndarray`, got {type(env.drones[0].rgbaImg)}."
assert isinstance(env.drones[0].depthImg, np.ndarray), f"Expected depth image to be of type `np.ndarray`, got {type(env.drones[0].depthImg)}."
assert isinstance(env.drones[0].segImg, np.ndarray), f"Expected segmented image to be of type `np.ndarray`, got {type(env.drones[0].segImg)}."
assert env.drones[0].rgbaImg.shape[-1] == 4, f"Expected 4 channels in the rendered image, got {env.drones[0].rgbaImg.shape[-1]}."
assert len(env.drones[0].depthImg.shape) == 2, f"Expected depth image to only have 2 dimensions, got {len(env.drones[0].depthImg.shape)}."
assert len(env.drones[0].segImg.shape) == 2, f"Expected segmented image to have only 2 dimensions, got {len(env.drones[0].segImg.shape)}"
assert isinstance(
env.drones[0].rgbaImg, np.ndarray
), f"Expected camera image to be of type `np.ndarray`, got {type(env.drones[0].rgbaImg)}."
assert isinstance(
env.drones[0].depthImg, np.ndarray
), f"Expected depth image to be of type `np.ndarray`, got {type(env.drones[0].depthImg)}."
assert isinstance(
env.drones[0].segImg, np.ndarray
), f"Expected segmented image to be of type `np.ndarray`, got {type(env.drones[0].segImg)}."
assert (
env.drones[0].rgbaImg.shape[-1] == 4
), f"Expected 4 channels in the rendered image, got {env.drones[0].rgbaImg.shape[-1]}."
assert (
env.drones[0].depthImg.shape[-1] == 1
), f"Expected 1 channel in the depth image, got {env.drones[0].depthImg.shape[-1]}."
assert (
env.drones[0].segImg.shape[-1] == 1
), f"Expected 1 channel in the segmentated image, got {env.drones[0].segImg.shape[-1]}"

env.disconnect()

Expand Down
10 changes: 7 additions & 3 deletions tests/test_gym_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import warnings

import gymnasium as gym
import numpy as np
import pytest
from gymnasium.error import Error
from gymnasium.utils.env_checker import check_env, data_equivalence
import numpy as np

import PyFlyt.gym_envs

Expand Down Expand Up @@ -163,7 +163,11 @@ def test_render(env_config):
env.step(env.action_space.sample())

for frame in frames:
assert isinstance(frame, np.ndarray), f"Expected render frames to be of type `np.ndarray`, got {type(frame)}."
assert frame.shape[-1] == 4, f"Expected 4 channels in the rendered image, got {frame.shape[-1]}."
assert isinstance(
frame, np.ndarray
), f"Expected render frames to be of type `np.ndarray`, got {type(frame)}."
assert (
frame.shape[-1] == 4
), f"Expected 4 channels in the rendered image, got {frame.shape[-1]}."

env.close()

0 comments on commit bcf519d

Please sign in to comment.