Skip to content

Commit

Permalink
replace airsim.write_png with cv2.imwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
madratman committed Apr 30, 2019
1 parent 480f65d commit d8a6ee3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
8 changes: 4 additions & 4 deletions PythonClient/airsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def write_pfm(file, image, scale=1):

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 @@ -203,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
15 changes: 7 additions & 8 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,10 +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
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
15 changes: 7 additions & 8 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,10 +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
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
7 changes: 4 additions & 3 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,9 +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_rgb = img1d.reshape(response.height, response.width, 3) #reshape array to 4 channel image array H X W X 3
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 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
8 changes: 4 additions & 4 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,7 +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
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

0 comments on commit d8a6ee3

Please sign in to comment.