Files
Gymnasium/gym/wrappers/resize_observation.py
Xingdong Zuo f380a0e871 [Wrappers]: ResizeObservation (#1487)
* Create resize_observation.py

* Update __init__.py

* Create test_resize_observation.py

* Update resize_observation.py

* Update resize_observation.py

* Update test_resize_observation.py

* Update resize_observation.py

* Update test_resize_observation.py

* Update resize_observation.py

* Update test_resize_observation.py
2019-06-07 16:01:35 -07:00

25 lines
856 B
Python

import numpy as np
from gym.spaces import Box
from gym import ObservationWrapper
class ResizeObservation(ObservationWrapper):
r"""Downsample the image observation to a square image. """
def __init__(self, env, shape):
super(ResizeObservation, self).__init__(env)
if isinstance(shape, int):
shape = (shape, shape)
assert all(x > 0 for x in shape), shape
self.shape = tuple(shape)
obs_shape = shape + self.observation_space.shape[2:]
self.observation_space = Box(low=0, high=255, shape=obs_shape, dtype=np.uint8)
def observation(self, observation):
import cv2
observation = cv2.resize(observation, self.shape[::-1], interpolation=cv2.INTER_AREA)
if observation.ndim == 2:
observation = np.expand_dims(observation, -1)
return observation