2019-06-08 01:01:35 +02:00
|
|
|
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)
|
|
|
|
|
2019-07-13 06:10:11 +08:00
|
|
|
obs_shape = self.shape + self.observation_space.shape[2:]
|
2019-06-08 01:01:35 +02:00
|
|
|
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
|