Skip to content

Commit 1b29f67

Browse files
committed
add video recording
1 parent 92ba0be commit 1b29f67

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

MalmoEnv/malmoenv/core.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class Env:
8484
def __init__(self, reshape=False):
8585
self.action_space = None
8686
self.observation_space = None
87+
self.metadata = {'render.modes': ['rgb_array']}
8788
self.xml = None
8889
self.integratedServerPort = 0
8990
self.role = 0
@@ -104,11 +105,12 @@ def __init__(self, reshape=False):
104105
self.height = 0
105106
self.depth = 0
106107
self.reshape = reshape
108+
self.last_obs = None
107109

108110
def init(self, xml, port, server=None,
109111
server2=None, port2=None,
110112
role=0, exp_uid=None, episode=0,
111-
action_filter=None, resync=0, step_options=0, action_space=None):
113+
action_filter=None, resync=0, step_options=0, action_space=None, reshape=False):
112114
""""Initialize a Malmo environment.
113115
xml - the mission xml.
114116
port - the MalmoEnv service's port.
@@ -206,6 +208,7 @@ def init(self, xml, port, server=None,
206208
})
207209
self.xml.insert(2, e)
208210

211+
self.reshape = reshape
209212
video_producers = self.xml.findall('.//' + self.ns + 'VideoProducer')
210213
assert len(video_producers) == self.agent_count
211214
video_producer = video_producers[self.role]
@@ -236,6 +239,7 @@ def reset(self):
236239

237240
@retry
238241
def _start_up(self):
242+
self.last_obs = None
239243
self.resets += 1
240244
if self.role != 0:
241245
self._find_server()
@@ -275,7 +279,7 @@ def _peek_obs(self):
275279
obs = np.zeros(self.height * self.width * self.depth, dtype=np.uint8)
276280
elif self.reshape:
277281
obs = obs.reshape((self.height, self.width, self.depth)).astype(np.uint8)
278-
282+
self.last_obs = obs
279283
return obs
280284

281285
def _quit_episode(self):
@@ -284,9 +288,15 @@ def _quit_episode(self):
284288
ok, = struct.unpack('!I', reply)
285289
return ok != 0
286290

287-
def render(self):
291+
def render(self, mode=None):
288292
"""gym api render"""
289-
pass
293+
if self.last_obs is None:
294+
if self.reshape:
295+
self.last_obs = np.zeros((self.height, self.width, self.depth), dtype=np.uint8)
296+
else:
297+
self.last_obs = np.zeros(self.height * self.width * self.depth, dtype=np.uint8)
298+
299+
return np.flipud(self.last_obs)
290300

291301
def seed(self):
292302
pass
@@ -336,6 +346,7 @@ def step(self, action):
336346
obs = np.zeros((self.height, self.width, self.depth), dtype=np.uint8)
337347
else:
338348
obs = obs.reshape((self.height, self.width, self.depth)).astype(np.uint8)
349+
self.last_obs = obs
339350

340351
return obs, reward, self.done, info
341352

MalmoEnv/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
setuptools.setup(
2626
name="malmoenv",
27-
version="0.0.6",
27+
version="0.0.7",
2828
author="Andre Kramer",
2929
author_email="[email protected]",
3030
description="A gym environemnt for Malmo",

MalmoEnv/video_run.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# ------------------------------------------------------------------------------------------------
2+
# Copyright (c) 2018 Microsoft Corporation
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
# associated documentation files (the "Software"), to deal in the Software without restriction,
6+
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all copies or
11+
# substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
# ------------------------------------------------------------------------------------------------
19+
20+
import malmoenv
21+
import argparse
22+
from pathlib import Path
23+
import time
24+
import gym
25+
from gym.wrappers.monitoring.video_recorder import VideoRecorder
26+
27+
import logging
28+
logging.basicConfig(level=logging.DEBUG)
29+
30+
if __name__ == '__main__':
31+
32+
parser = argparse.ArgumentParser(description='malmovnv test')
33+
parser.add_argument('--mission', type=str, default='missions/mobchase_single_agent.xml', help='the mission xml')
34+
parser.add_argument('--port', type=int, default=9000, help='the mission server port')
35+
parser.add_argument('--server', type=str, default='127.0.0.1', help='the mission server DNS or IP address')
36+
parser.add_argument('--port2', type=int, default=None, help="(Multi-agent) role N's mission port. Defaults to server port.")
37+
parser.add_argument('--server2', type=str, default=None, help="(Multi-agent) role N's server DNS or IP")
38+
parser.add_argument('--episodes', type=int, default=1, help='the number of resets to perform - default is 1')
39+
parser.add_argument('--episode', type=int, default=0, help='the start episode - default is 0')
40+
parser.add_argument('--role', type=int, default=0, help='the agent role - defaults to 0')
41+
parser.add_argument('--episodemaxsteps', type=int, default=0, help='max number of steps per episode')
42+
parser.add_argument('--resync', type=int, default=0, help='exit and re-sync every N resets'
43+
' - default is 0 meaning never.')
44+
parser.add_argument('--experimentUniqueId', type=str, default='test1', help="the experiment's unique id.")
45+
parser.add_argument('--video_path', type=str, default="video.mp4", help="Optional video path.")
46+
args = parser.parse_args()
47+
if args.server2 is None:
48+
args.server2 = args.server
49+
50+
xml = Path(args.mission).read_text()
51+
env = malmoenv.make()
52+
53+
env.init(xml, args.port,
54+
server=args.server,
55+
server2=args.server2, port2=args.port2,
56+
role=args.role,
57+
exp_uid=args.experimentUniqueId,
58+
episode=args.episode,
59+
resync=args.resync,
60+
reshape=True)
61+
62+
rec = VideoRecorder(env, args.video_path)
63+
64+
for i in range(args.episodes):
65+
print("reset " + str(i))
66+
obs = env.reset()
67+
rec.capture_frame()
68+
69+
steps = 0
70+
done = False
71+
while not done and (args.episodemaxsteps <= 0 or steps < args.episodemaxsteps):
72+
action = env.action_space.sample()
73+
rec.capture_frame()
74+
obs, reward, done, info = env.step(action)
75+
steps += 1
76+
print("reward: " + str(reward))
77+
# print("done: " + str(done))
78+
print("obs: " + str(obs))
79+
# print("info" + info)
80+
81+
time.sleep(.05)
82+
83+
rec.close()
84+
env.close()

0 commit comments

Comments
 (0)