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

Improve simgetimages speed #1910

Merged
merged 2 commits into from
Apr 30, 2019
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
3 changes: 0 additions & 3 deletions PythonClient/airsim/pfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def read_pfm(file):

data = np.reshape(data, shape)
# DEY: I don't know why this was there.
#data = np.flipud(data)
file.close()

return data, scale
Expand All @@ -64,8 +63,6 @@ def write_pfm(file, image, scale=1):
if image.dtype.name != 'float32':
raise Exception('Image dtype must be float32.')

image = np.flipud(image)

if len(image.shape) == 3 and image.shape[2] == 3: # color image
color = True
elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # greyscale
Expand Down
11 changes: 4 additions & 7 deletions PythonClient/airsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def read_pfm(file):

data = np.reshape(data, shape)
# DEY: I don't know why this was there.
#data = np.flipud(data)
file.close()

return data, scale
Expand All @@ -172,11 +171,9 @@ def write_pfm(file, image, scale=1):
if image.dtype.name != 'float32':
raise Exception('Image dtype must be float32.')

image = np.flipud(image)

if len(image.shape) == 3 and image.shape[2] == 3: # color image
color = True
elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # greyscale
elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # grayscale
color = False
else:
raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.')
Expand Down Expand Up @@ -206,9 +203,9 @@ def write_png(filename, image):
height = image.shape[0]

# reverse the vertical line order and add null bytes at the start
width_byte_4 = width * 4
raw_data = b''.join(b'\x00' + buf[span:span + width_byte_4]
for span in range((height - 1) * width_byte_4, -1, - width_byte_4))
width_byte_3 = width * 3
raw_data = b''.join(b'\x00' + buf[span:span + width_byte_3]
for span in range((height - 1) * width_byte_3, -1, - width_byte_3))

def png_pack(png_tag, data):
chunk_head = png_tag + data
Expand Down
9 changes: 3 additions & 6 deletions PythonClient/car/drive_straight.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
def get_image():
image = client.simGetImages([airsim.ImageRequest("0", airsim.ImageType.Scene, False, False)])[0]
image1d = np.fromstring(image.image_data_uint8, dtype=np.uint8)
image_rgba = image1d.reshape(image.height, image.width, 4)
image_rgba = np.flipud(image_rgba)
return image_rgba[:, :, 0:3]
image_rgb = image1d.reshape(image.height, image.width, 3)
return image_rgb

while (True):
car_state = client.getCarState()
Expand All @@ -42,13 +41,11 @@ def get_image():
else:
car_controls.throttle = 0.0

#image_buf[0] = get_image()
#state_buf[0] = np.array([car_controls.steering, car_controls.throttle, car_controls.brake, car_state.speed])
#model_output = model.predict([image_buf, state_buf])
#car_controls.steering = float(model_output[0][0])
car_controls.steering = 0

print('Sending steering = {0}, throttle = {1}'.format(car_controls.steering, car_controls.throttle))

client.setCarControls(car_controls)

client.setCarControls(car_controls)
19 changes: 8 additions & 11 deletions PythonClient/car/hello_car.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import setup_path
import airsim

import time
import os
import cv2
import numpy as np
import os
import setup_path
import time

# connect to the AirSim simulator
client = airsim.CarClient()
Expand Down Expand Up @@ -53,7 +53,7 @@
airsim.ImageRequest("0", airsim.ImageType.DepthVis), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.DepthPerspective, True), #depth in perspective projection
airsim.ImageRequest("1", airsim.ImageType.Scene), #scene vision image in png format
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGB array
print('Retrieved images: %d', len(responses))

for response in responses:
Expand All @@ -68,12 +68,9 @@
airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png

img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) # get numpy array
img_rgb = img1d.reshape(response.height, response.width, 3) # reshape array to 3 channel image array H X W X 3
cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

#restore to original state
client.reset()
Expand Down
8 changes: 3 additions & 5 deletions PythonClient/car/legacy_hello_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
ImageRequest(0, airsim.AirSimImageType.DepthVis), #depth visualiztion image
ImageRequest(1, airsim.AirSimImageType.DepthPerspective, True), #depth in perspective projection
ImageRequest(1, airsim.AirSimImageType.Scene), #scene vision image in png format
ImageRequest(1, airsim.AirSimImageType.Scene, False, False)]) #scene vision image in uncompressed RGBA array
ImageRequest(1, airsim.AirSimImageType.Scene, False, False)]) #scene vision image in uncompressed RGB array
print('Retrieved images: %d' % len(responses))

tmp_dir = os.path.join(tempfile.gettempdir(), "airsim_drone")
Expand All @@ -64,10 +64,8 @@
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is fliped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
AirSimClientBase.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png
img_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
AirSimClientBase.write_png(os.path.normpath(filename + '.png'), img_rgb) #write to png

AirSimClientBase.wait_key('Press any key to reset to original state')

Expand Down
21 changes: 9 additions & 12 deletions PythonClient/car/multi_agent_car.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import setup_path
import airsim

import time
import os
import cv2
import numpy as np
import os
import setup_path
import time

# Use below in settings.json with blocks environment
"""
Expand Down Expand Up @@ -91,11 +91,11 @@
# get camera images from the car
responses1 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car1") #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car1") #scene vision image in uncompressed RGB array
print('Car1: Retrieved images: %d' % (len(responses1)))
responses2 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.Segmentation), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car2") #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car2") #scene vision image in uncompressed RGB array
print('Car2: Retrieved images: %d' % (len(responses2)))

for response in responses1 + responses2:
Expand All @@ -109,12 +109,9 @@
airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png

img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) # get numpy array
img_rgb = img1d.reshape(response.height, response.width, 3) # reshape array to 3 channel image array H X W X 3
cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

#restore to original state
client.reset()
Expand Down
24 changes: 7 additions & 17 deletions PythonClient/computer_vision/segmentation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# In settings.json first activate computer vision mode:
# https://github.com/Microsoft/AirSim/blob/master/docs/image_apis.md#computer-vision-mode

import setup_path
import airsim

import cv2
import numpy as np
import setup_path

client = airsim.VehicleClient()
client.confirmConnection()
Expand Down Expand Up @@ -54,20 +54,10 @@
else: #uncompressed array - numpy demo
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
#airsim.write_png(os.path.normpath(filename + '.numpy.png'), img_rgba) #write to png
img_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
# cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

#find unique colors
print(np.unique(img_rgba[:,:,0], return_counts=True)) #red
print(np.unique(img_rgba[:,:,1], return_counts=True)) #green
print(np.unique(img_rgba[:,:,2], return_counts=True)) #blue
print(np.unique(img_rgba[:,:,3], return_counts=True)) #blue








print(np.unique(img_rgb[:,:,0], return_counts=True)) #red
print(np.unique(img_rgb[:,:,1], return_counts=True)) #green
print(np.unique(img_rgb[:,:,2], return_counts=True)) #blue
4 changes: 2 additions & 2 deletions PythonClient/imitation_learning/drive_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def get_image():
"""
image_response = client.simGetImages([airsim.ImageRequest("0", airsim.ImageType.Scene, False, False)])[0]
image1d = np.fromstring(image_response.image_data_uint8, dtype=np.uint8)
image_rgba = image1d.reshape(image_response.height, image_response.width, 4)
return image_rgba[78:144,27:227,0:3].astype(float)
image_rgb = image1d.reshape(image_response.height, image_response.width, 3)
return image_rgb[78:144,27:227,0:2].astype(float)

while True:
# Update throttle value according to steering angle
Expand Down
9 changes: 4 additions & 5 deletions PythonClient/multirotor/hello_drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import tempfile
import pprint
import cv2

# connect to the AirSim simulator
client = airsim.MultirotorClient()
Expand Down Expand Up @@ -59,11 +60,9 @@
airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) # get numpy array
img_rgb = img1d.reshape(response.height, response.width, 3) # reshape array to 4 channel image array H X W X 3
cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

airsim.wait_key('Press any key to reset to original state')

Expand Down
4 changes: 2 additions & 2 deletions PythonClient/multirotor/kinect_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def getDepthImage(self,response_d):

def getRGBImage(self,response_rgb):
img1d = np.fromstring(response_rgb.image_data_uint8, dtype=np.uint8)
img_rgba = img1d.reshape(response_rgb.height, response_rgb.width, 4)
img_rgb = img_rgba[..., :3][..., ::-1]
img_rgb = img1d.reshape(response_rgb.height, response_rgb.width, 3)
img_rgb = img_rgb[..., :3][..., ::-1]
return img_rgb

def enhanceRGB(self,img_rgb):
Expand Down
16 changes: 7 additions & 9 deletions PythonClient/multirotor/multi_agent_drone.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import setup_path
import airsim

import cv2
import numpy as np
import os
import tempfile
import pprint
import setup_path
import tempfile

# Use below in settings.json with Blocks environment
"""
Expand Down Expand Up @@ -59,11 +59,11 @@
# get camera images from the car
responses1 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], vehicle_name="Drone1") #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], vehicle_name="Drone1") #scene vision image in uncompressed RGB array
print('Drone1: Retrieved images: %d' % len(responses1))
responses2 = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis), #depth visualization image
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], vehicle_name="Drone2") #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], vehicle_name="Drone2") #scene vision image in uncompressed RGB array
print('Drone2: Retrieved images: %d' % len(responses2))

tmp_dir = os.path.join(tempfile.gettempdir(), "airsim_drone")
Expand All @@ -87,10 +87,8 @@
else: #uncompressed array
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
img_rgba = np.flipud(img_rgba) #original image is flipped vertically
img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png
img_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

airsim.wait_key('Press any key to reset to original state')

Expand Down
10 changes: 5 additions & 5 deletions PythonClient/ros/car_image_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ def airpub():
while not rospy.is_shutdown():
# get camera images from the car
responses = client.simGetImages([
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGB array

for response in responses:
img_rgba_string = response.image_data_uint8
img_rgb_string = response.image_data_uint8

# Populate image message
msg=Image()
msg.header.stamp = rospy.Time.now()
msg.header.frame_id = "frameId"
msg.encoding = "rgba8"
msg.encoding = "rgb8"
msg.height = 360 # resolution should match values in settings.json
msg.width = 640
msg.data = img_rgba_string
msg.data = img_rgb_string
msg.is_bigendian = 0
msg.step = msg.width * 4
msg.step = msg.width * 3

# log time and size of published image
rospy.loginfo(len(response.image_data_uint8))
Expand Down
10 changes: 5 additions & 5 deletions PythonClient/ros/drone_image_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ def airpub():
while not rospy.is_shutdown():
# get camera images from the car
responses = client.simGetImages([
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGBA array
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)]) #scene vision image in uncompressed RGB array

for response in responses:
img_rgba_string = response.image_data_uint8
img_rgb_string = response.image_data_uint8

# Populate image message
msg=Image()
msg.header.stamp = rospy.Time.now()
msg.header.frame_id = "frameId"
msg.encoding = "rgba8"
msg.encoding = "rgb8"
msg.height = 360 # resolution should match values in settings.json
msg.width = 640
msg.data = img_rgba_string
msg.data = img_rgb_string
msg.is_bigendian = 0
msg.step = msg.width * 4
msg.step = msg.width * 3

# log time and size of published image
rospy.loginfo(len(response.image_data_uint8))
Expand Down
Loading