Skip to content

Commit

Permalink
[simgetimages] fix bgr<->rgb in renderrequest
Browse files Browse the repository at this point in the history
remove old code's np.flipuds
replace airsim.write_png with cv2.imwrite
  • Loading branch information
madratman committed Apr 30, 2019
1 parent aa1bcb5 commit eb115c2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 49 deletions.
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
5 changes: 1 addition & 4 deletions PythonClient/car/drive_straight.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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_rgb = image1d.reshape(image.height, image.width, 3)
image_rgb = np.flipud(image_rgb)
return image_rgb

while (True):
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)
16 changes: 7 additions & 9 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 @@ -68,11 +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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
img_rgb = np.flipud(img_rgb) #original image is flipped vertically
airsim.write_png(os.path.normpath(filename + '.png'), img_rgb) #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
1 change: 0 additions & 1 deletion PythonClient/car/legacy_hello_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
img_rgb = np.flipud(img_rgb) #original image is fliped vertically
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
16 changes: 7 additions & 9 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 @@ -109,11 +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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
img_rgb = np.flipud(img_rgb) #original image is flipped vertically
airsim.write_png(os.path.normpath(filename + '.png'), img_rgb) #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
7 changes: 3 additions & 4 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 @@ -55,8 +55,7 @@
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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
img_rgb = np.flipud(img_rgb) #original image is flipped vertically
#airsim.write_png(os.path.normpath(filename + '.numpy.png'), img_rgb) #write to png
# cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

#find unique colors
print(np.unique(img_rgb[:,:,0], return_counts=True)) #red
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
9 changes: 4 additions & 5 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 @@ -88,8 +88,7 @@
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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 3 channel image array H X W X 3
img_rgb = np.flipud(img_rgb) #original image is flipped vertically
airsim.write_png(os.path.normpath(filename + '.png'), img_rgb) #write to png
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 Unreal/Plugins/AirSim/Source/RenderRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void RenderRequest::getScreenshot(std::shared_ptr<RenderParams> params[], std::v
else {
uint8* ptr = results[i]->image_data_uint8.GetData();
for (const auto& item : results[i]->bmp) {
*ptr++ = item.R;
*ptr++ = item.G;
*ptr++ = item.B;
*ptr++ = item.G;
*ptr++ = item.R;
}
}
}
Expand Down

0 comments on commit eb115c2

Please sign in to comment.