-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworld.py
More file actions
executable file
·45 lines (41 loc) · 1.26 KB
/
world.py
File metadata and controls
executable file
·45 lines (41 loc) · 1.26 KB
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
#!/usr/bin/env python
# license removed for brevity
import rospy
import gym
import cv2
import numpy as np
import imutils
from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage
#check world debug
env = gym.make('SpaceInvaders-v0')
image_pub = rospy.Publisher('space_invader/image_raw', CompressedImage, queue_size=10)
def pub_image(env):
#GYM RENDER AS IMAGE
img = env.render(mode='rgb_array')
# ROTATE THE IMAGE THE MATRIX IS 90 grates and mirror
img = np.flipud(np.rot90(img))
image_np = imutils.resize(img, width=500)
# Publish new image
msg = CompressedImage()
msg.header.stamp = rospy.Time.now()
msg.format = "jpeg"
compressed_images = cv2.imencode('.jpg', image_np)
msg.data = np.array(compressed_images[1]).tostring()
image_pub.publish(msg)
def open_world(vel_msg):
action = int(vel_msg.data)
obs, rew, done, info = env.step(action)
print("Reward ",rew)
pub_image(env)
if done is True:
env.reset()
if __name__ == '__main__':
rospy.init_node('space_invader_world', anonymous=True)
try:
rospy.Subscriber("space_invader/move", String, open_world)
env.reset()
pub_image(env)
rospy.spin()
except rospy.ROSInterruptException:
pass