-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Hi, thank you for great works for SofaGym!
I currently study SofaGym and make my own environment.
In my reinforcement learning architecture, I want to define image state(or observation) which is related to GUI screen image.
For this, I defined additional camera in Scene python file as follows:
(I thought that InteractiveCamera can realize my wish :))
source = config["source"]
target = config["target"]
rootNode.addObject("InteractiveCamera", name='camera2', position=source, lookAt=target, zFar=500)Next, I refer to viewer.py and find the code which can make image object as follows:
glViewport(0, 0, self.surface_size[0], self.surface_size[1])
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
if self.root:
Sofa.SofaGL.glewInit()
Sofa.Simulation.initVisual(self.root)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (self.surface_size[0] / self.surface_size[1]), 0.1, self.zFar)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
cameraMVM = self.root.camera.getOpenGLModelViewMatrix()
glMultMatrixd(cameraMVM)
Sofa.SofaGL.draw(self.root)
else:
print("===============> ERROR")
try:
x, y, width, height = glGetIntegerv(GL_VIEWPORT)
except:
width, height = self.surface_size[0], self.surface_size[1]
buff = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
image_array = np.fromstring(buff, np.uint8)
if image_array.shape != (0,):
image = image_array.reshape(self.surface_size[1], self.surface_size[0], 3)
else:
image = np.zeros((self.surface_size[1], self.surface_size[0], 3))
image = np.flipud(image)
image = np.moveaxis(image, 0, 1)I checked that above image is same as GUI screen image.
By using above code and Interactive camera(defined as camera2), I implemented additional function that returns image in Toolbox python file.
However, there is some multi-thread error(I guess that problem is due to reinitialization such as Sofa.SofaGL.glewInit() and Sofa.SofaGL.draw(self.root)).
So I conclude that this is not good approach.
Is there any method to get screen image in Toolbox and Scene code python without GUI?