Giter VIP home page Giter VIP logo

Comments (6)

maximecb avatar maximecb commented on July 24, 2024

Hi @DamienAllonsius. I'm not quite clear on what you want to do. I'm assuming you want to transform the observations the agent gets by processing them as RGB images?

One thing you should be aware of is that the image that you see when running manual_control.py, that is the human-viewable input, is not the same as what the agent sees. The agent gets a partially observable view of the environment that is encoded as a (7,7,3) array, which is not an image, but rather a symbolic encoding of the grid cells in front of the agent. What you see in manual_control.py is a rendering of the whole grid as an RGB image, which is produced by a call to env.render('human').

The easiest way to transform what the agent sees is using a wrapper, see the code in wrappers.py for some examples. There is a wrapper that transforms the input the agent gets into an RGB image, which may be a good starting point for what you want to do.

from minigrid.

DamienAllonsius avatar DamienAllonsius commented on July 24, 2024

Hi @maximecb Thanks a lot for your answer !
No, I really want to transform the rendering, not the observation.
I already give this observation to my agent

env.render(mode='rgb_array', highlight=False)

But then I transform the matrix by applying some downsampling with cv2, and I would like to see the result of it. Is that possible ? Thanks a lot Maxime
(sinon je suis français, on peut aussi communiquer par mail en français si tu préfères)
Damien

from minigrid.

maximecb avatar maximecb commented on July 24, 2024

You would have to modify the renderer class or create your own window using PyQT or Pyglet. The slightly annoying part is that you have to convert the rgba array into a QPixmap object, basically. Then the Window class here has a setPixmap method that you can use: https://github.com/maximecb/gym-minigrid/blob/master/gym_minigrid/rendering.py#L55

It's a bit annoying but definitely doable: https://stackoverflow.com/questions/34232632/convert-python-opencv-image-numpy-array-to-pyqt-qpixmap-image

If you do it, maybe you could add a method to the Window class to set what is being displayed from a numpy rgba array and create a pull request? :)

from minigrid.

DamienAllonsius avatar DamienAllonsius commented on July 24, 2024

Thanks for your answer. Ok I'm on it.

from minigrid.

DamienAllonsius avatar DamienAllonsius commented on July 24, 2024

Following the stackoverflow advices, I made a new function in Renderer class

    def drawArray(self, rgb_array):
        from PyQt5.QtGui import QPixmap

        height, width, _ = rgb_array.shape
        bytes_per_line = 3 * width

        qt_img = QImage(rgb_array, width, height, bytes_per_line, QImage.Format_RGB888)  # .rgbSwapped()
        pixmap = QPixmap(qt_img)

        if self.window.closed:
            self.window = None
        else:
            self.window.setPixmap(pixmap)
            self.app.processEvents()

But I get a segmentation fault when I run it. Any Idea ?

from minigrid.

maximecb avatar maximecb commented on July 24, 2024

I don't think that you can just pass the numpy array directly. You will need to get a pointer to the data it contains using ctypes. Something like:

import ctypes

rgb_array = np.ascontiguousarray(rgb_array)
ptr = rgb_array.ctypes.data_as(ctypes.POINTER(ctypes.c_char))
qt_img = QImage(ptr, ...)

The ascontiguousarray call is to make sure the memory is all in one contiguous block.

References:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ctypes.html
https://docs.python.org/3/library/ctypes.html

from minigrid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.