Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing render for Win/MacOS #41

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions diambra/arena/arena_gym.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import os
import sys
import cv2
import gym
import logging
Expand Down Expand Up @@ -118,17 +119,20 @@ def reset(self):
# Rendering the environment
def render(self, mode='human', wait_key=1):

if mode == "human" and 'DISPLAY' in os.environ:
if (self.render_gui_started is False):
self.window_name = "[{}] DIAMBRA Arena - {} - ({})".format(
os.getpid(), self.env_settings["game_id"], self.env_settings["rank"])
cv2.namedWindow(self.window_name, cv2.WINDOW_GUI_NORMAL)
self.render_gui_started = True
wait_key = 100

cv2.imshow(self.window_name, self.frame[:, :, ::-1])
cv2.waitKey(wait_key)
return True
if mode == "human" and (sys.platform.startswith('linux') is False or 'DISPLAY' in os.environ):
try:
if (self.render_gui_started is False):
self.window_name = "[{}] DIAMBRA Arena - {} - ({})".format(
os.getpid(), self.env_settings["game_id"], self.env_settings["rank"])
cv2.namedWindow(self.window_name, cv2.WINDOW_GUI_NORMAL)
self.render_gui_started = True
wait_key = 100

cv2.imshow(self.window_name, self.frame[:, :, ::-1])
cv2.waitKey(wait_key)
return True
except:
return False
elif mode == "rgb_array":
return self.frame

Expand Down
23 changes: 15 additions & 8 deletions diambra/arena/utils/gym_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gym
import os
import sys
from gym import spaces
import numpy as np
import pickle
Expand Down Expand Up @@ -151,9 +152,12 @@ def show_gym_obs(observation, char_list, wait_key=1, viz=True):
if viz:
obs = observation / 255.0

if viz and 'DISPLAY' in os.environ:
cv2.imshow("[{}] Frame".format(os.getpid()), obs[:, :, ::-1]) # rgb2bgr
cv2.waitKey(wait_key)
if viz is True and (sys.platform.startswith('linux') is False or 'DISPLAY' in os.environ):
try:
cv2.imshow("[{}] Frame".format(os.getpid()), obs[:, :, ::-1]) # rgb2bgr
cv2.waitKey(wait_key)
except:
pass

# Visualize Obs content
def show_wrap_obs(observation, n_actions_stack, char_list, wait_key=1, viz=True):
Expand Down Expand Up @@ -184,12 +188,15 @@ def show_wrap_obs(observation, n_actions_stack, char_list, wait_key=1, viz=True)
if viz:
obs = observation

if viz and 'DISPLAY' in os.environ:
norm_factor = 255 if np.amax(obs) > 1.0 else 1.0
for idx in range(obs.shape[2]):
cv2.imshow("[{}] Frame-{}".format(os.getpid(), idx), obs[:, :, idx] / norm_factor)
if viz is True and (sys.platform.startswith('linux') is False or 'DISPLAY' in os.environ):
try:
norm_factor = 255 if np.amax(obs) > 1.0 else 1.0
for idx in range(obs.shape[2]):
cv2.imshow("[{}] Frame-{}".format(os.getpid(), idx), obs[:, :, idx] / norm_factor)

cv2.waitKey(wait_key)
cv2.waitKey(wait_key)
except:
pass

# List all available games
def available_games(print_out=True, details=False):
Expand Down