Shortcuts

Source code for openrl.envs.wrappers.image_wrappers

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2023 The OpenRL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""""""

from gymnasium.spaces.box import Box

from openrl.envs.wrappers.base_wrapper import BaseObservationWrapper


[docs]class TransposeImage(BaseObservationWrapper): def __init__(self, env=None, op=[2, 0, 1]): """ Transpose observation space for images """ super(TransposeImage, self).__init__(env) assert len(op) == 3, "Error: Operation, " + str(op) + ", must be dim3" self.op = op obs_shape = self.observation_space.shape self.observation_space = Box( self.observation_space.low[0, 0, 0], self.observation_space.high[0, 0, 0], [obs_shape[self.op[0]], obs_shape[self.op[1]], obs_shape[self.op[2]]], dtype=self.observation_space.dtype, )
[docs] def observation(self, ob): return ob.transpose(self.op[0], self.op[1], self.op[2])