-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
buffers.py
executable file
·142 lines (105 loc) · 4.68 KB
/
buffers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
#####################################################################
# This script presents different buffers and formats.
#
# Note: OpenCV is used here to display images, install it:
# pip install opencv-python
# or remove any references to cv2.
#
# Configuration is loaded from "../../scenarios/basic.cfg" file.
# <episodes> number of episodes are played.
# Random combination of buttons is chosen for every action.
#
# To see the scenario description go to "../../scenarios/README.md"
#####################################################################
import os
from argparse import ArgumentParser
from random import choice
import cv2
import vizdoom as vzd
DEFAULT_CONFIG = os.path.join(vzd.scenarios_path, "deadly_corridor.cfg")
if __name__ == "__main__":
parser = ArgumentParser(
"ViZDoom example showing different buffers (screen, depth, labels)."
)
parser.add_argument(
dest="config",
default=DEFAULT_CONFIG,
nargs="?",
help="Path to the configuration file of the scenario."
" Please see "
"../../scenarios/*cfg for more scenarios.",
)
args = parser.parse_args()
game = vzd.DoomGame()
# Use other config file if you wish.
game.load_config(args.config)
# Just uncomment desired format for screen buffer (and map buffer).
# The last uncommented will be applied.
# Formats with C (CRCGCB and CBCGCR) were omitted cause they are not cv2 friendly.
# Default format is ScreenFormat.CRCGCB.
# OpenCV uses a BGR colorspace by default.
game.set_screen_format(vzd.ScreenFormat.BGR24)
# game.set_screen_format(ScreenFormat.RGB24)
# game.set_screen_format(ScreenFormat.RGBA32)
# game.set_screen_format(ScreenFormat.ARGB32)
# game.set_screen_format(ScreenFormat.BGRA32)
# game.set_screen_format(ScreenFormat.ABGR32)
# game.set_screen_format(ScreenFormat.GRAY8)
# Raw Doom buffer with palette's values. This one makes no sense in particular
# game.set_screen_format(ScreenFormat.DOOM_256_COLORS)
# Sets resolution for all buffers.
game.set_screen_resolution(vzd.ScreenResolution.RES_640X480)
# Enables depth buffer.
game.set_depth_buffer_enabled(True)
# Enables labeling of in game objects labeling.
game.set_labels_buffer_enabled(True)
# See also labels_buffer.py example for more explanations.
# Enables buffer with top down map of the current episode/level .
game.set_automap_buffer_enabled(True)
game.set_automap_mode(vzd.AutomapMode.OBJECTS)
game.set_automap_rotate(False)
game.set_automap_render_textures(False)
# There is also audio buffer which is not present here.
# See audio_buffer.py example for more explanations.
game.set_render_hud(True)
game.set_render_minimal_hud(False)
game.set_mode(vzd.Mode.SPECTATOR)
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
episodes = 10
sleep_time = 0.028
for i in range(episodes):
print(f"Episode #{i + 1}")
# Not needed for the first episode but the loop is nicer.
game.new_episode()
while not game.is_episode_finished():
# Gets the state and possibly do something with it
state = game.get_state()
# Display all the buffers here!
# Just screen buffer, given in selected format. This buffer is always available.
screen = state.screen_buffer
cv2.imshow("ViZDoom Screen Buffer", screen)
# Depth buffer, always in 8-bit gray channel format.
# This is most fun. It looks best if you inverse colors.
depth = state.depth_buffer
if depth is not None:
cv2.imshow("ViZDoom Depth Buffer", depth)
# Labels buffer, always in 8-bit gray channel format.
# Shows only visible game objects (enemies, pickups, exploding barrels etc.), each with unique label.
# Labels data are available in state.labels, also see labels.py example.
labels = state.labels_buffer
if labels is not None:
cv2.imshow("ViZDoom Labels Buffer", labels)
# Map buffer, in the same format as screen buffer.
# Shows top down map of the current episode/level.
automap = state.automap_buffer
if automap is not None:
cv2.imshow("ViZDoom Map Buffer", automap)
cv2.waitKey(int(sleep_time * 1000))
game.make_action(choice(actions))
print(f"State #{state.number}")
print("=====================")
print("Episode finished!")
print("************************")
cv2.destroyAllWindows()