Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def take_snapshot(self):
# first hold our current position so drone doesn't try and keep flying while we take the picture.
pos = self.client.getMultirotorState().kinematics_estimated.position
self.client.moveToPositionAsync(pos.x_val, pos.y_val, self.z, 0.5, 10, airsim.DrivetrainType.MaxDegreeOfFreedom,
airsim.YawMode(False, self.camera_heading)).join()
responses = self.client.simGetImages([airsim.ImageRequest(1, airsim.ImageType.Scene)]) #scene vision image in png format
response = responses[0]
filename = "photo_" + str(self.snapshot_index)
self.snapshot_index += 1
airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
print("Saved snapshot: {}".format(filename))
self.start_time = time.time() # cause smooth ramp up to happen again after photo is taken.
def take_snapshot(self):
if not os.path.exists(self.image_dir):
os.makedirs(self.image_dir)
# first hold our current position so drone doesn't try and keep flying while we take the picture.
pos = self.client.getMultirotorState().kinematics_estimated.position
self.client.moveToPositionAsync(pos.x_val, pos.y_val, self.z, 0.25, 3, airsim.DrivetrainType.MaxDegreeOfFreedom,
airsim.YawMode(False, self.camera_heading))
responses = self.client.simGetImages([airsim.ImageRequest(
0, airsim.ImageType.Scene)]) # scene vision image in png format
response = responses[0]
filename = self.photo_prefix + \
str(self.snapshot_index) + "_" + str(int(time.time()))
self.snapshot_index += 1
airsim.write_file(os.path.normpath(
self.image_dir + filename + '.png'), response.image_data_uint8)
print("Saved snapshot: {}".format(filename))
# cause smooth ramp up to happen again after photo is taken.
self.start_time = time.time()
airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car1") #scene vision image in uncompressed RGBA 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
print('Car2: Retrieved images: %d' % (len(responses2)))
for response in responses1 + responses2:
filename = 'c:/temp/car_multi_py' + str(idx)
if response.pixels_as_float:
print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))
elif response.compress: #png format
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
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
#restore to original state
client.reset()
client.enableApiControl(False)
responses = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis),
airsim.ImageRequest("1", airsim.ImageType.DepthPerspective, True),
airsim.ImageRequest("2", airsim.ImageType.Segmentation),
airsim.ImageRequest("3", airsim.ImageType.Scene),
airsim.ImageRequest("4", airsim.ImageType.DisparityNormalized),
airsim.ImageRequest("4", airsim.ImageType.SurfaceNormals)])
for i, response in enumerate(responses):
if response.pixels_as_float:
print("Type %d, size %d, pos %s" % (response.image_type, len(response.image_data_float), pprint.pformat(response.camera_position)))
airsim.write_pfm(os.path.normpath('/temp/cv_mode_' + str(x) + "_" + str(i) + '.pfm'), airsim.get_pfm_array(response))
else:
print("Type %d, size %d, pos %s" % (response.image_type, len(response.image_data_uint8), pprint.pformat(response.camera_position)))
airsim.write_file(os.path.normpath('/temp/cv_mode_' + str(x) + "_" + str(i) + '.png'), response.image_data_uint8)
pose = client.simGetVehiclePose()
pp.pprint(pose)
time.sleep(3)
# currently reset() doesn't work in CV mode. Below is the workaround
client.simSetPose(airsim.Pose(airsim.Vector3r(0, 0, 0), airsim.to_quaternion(0, 0, 0)), True)
try:
os.makedirs(tmp_dir)
except OSError:
if not os.path.isdir(tmp_dir):
raise
for idx, response in enumerate(responses1 + responses2):
filename = os.path.join(tmp_dir, str(idx))
if response.pixels_as_float:
print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))
elif response.compress: #png format
print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
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
airsim.wait_key('Press any key to reset to original state')
client.armDisarm(False, "Drone1")
client.armDisarm(False, "Drone2")
client.reset()
# that's enough fun for now. let's quit cleanly
client.enableApiControl(False, "Drone1")
airsim.wait_key('Press any key to get images')
for x in range(3): # do few times
responses = client.simGetImages([
airsim.ImageRequest("0", airsim.ImageType.DepthVis),
airsim.ImageRequest("0", airsim.ImageType.Segmentation),
airsim.ImageRequest("0", airsim.ImageType.Scene),
airsim.ImageRequest("0", airsim.ImageType.SurfaceNormals)])
for i, response in enumerate(responses):
if response.pixels_as_float:
print("Type %d, size %d, pos %s" % (response.image_type, len(response.image_data_float), pprint.pformat(response.camera_position)))
airsim.write_pfm(os.path.normpath('/temp/cv_mode_' + str(x) + "_" + str(i) + '.pfm'), airsim.get_pfm_array(response))
else:
print("Type %d, size %d, pos %s" % (response.image_type, len(response.image_data_uint8), pprint.pformat(response.camera_position)))
airsim.write_file(os.path.normpath('/temp/cv_mode_' + str(x) + "_" + str(i) + '.png'), response.image_data_uint8)
pose = client.simGetVehiclePose()
pose.position.x_val = pose.position.x_val + 1
pose.position.y_val = pose.position.y_val - 0.5
pose.position.z_val = pose.position.z_val - 0.5
client.simSetVehiclePose(pose, True)
time.sleep(3)
client.reset()