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 airpub():
pub = rospy.Publisher("airsim/image_raw", Image, queue_size=1)
rospy.init_node('image_raw', anonymous=True)
rate = rospy.Rate(10) # 10hz
# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
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 RGB array
for response in responses:
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 = "rgb8"
msg.height = 360 # resolution should match values in settings.json
msg.width = 640
msg.data = img_rgb_string
msg.is_bigendian = 0
msg.step = msg.width * 3
# log time and size of published image
def get_image():
"""
Get image from AirSim client
"""
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_rgb = image1d.reshape(image_response.height, image_response.width, 3)
return image_rgb[78:144,27:227,0:2].astype(float)
# apply breaks
car_controls1.brake = 1
client.setCarControls(car_controls1, "Car1")
print("Car1: Apply break")
car_controls1.brake = 0 #remove break
car_controls2.brake = 1
client.setCarControls(car_controls2, "Car2")
print("Car2: Apply break")
car_controls2.brake = 0 #remove break
time.sleep(3) # let car drive a bit
# 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
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
if __name__ == "__main__":
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)
rospy.init_node('airsim_publisher', anonymous=True)
publisher_d = rospy.Publisher('/camera/depth_registered/image_raw', Image, queue_size=1)
publisher_rgb = rospy.Publisher('/camera/rgb/image_rect_color', Image, queue_size=1)
publisher_info = rospy.Publisher('/camera/rgb/camera_info', CameraInfo, queue_size=1)
publisher_tf = rospy.Publisher('/tf', TFMessage, queue_size=1)
rate = rospy.Rate(30) # 30hz
pub = KinectPublisher()
while not rospy.is_shutdown():
responses = client.simGetImages([airsim.ImageRequest(0, airsim.ImageType.DepthPlanner, True, False),
airsim.ImageRequest(0, airsim.ImageType.Scene, False, False)])
img_depth = pub.getDepthImage(responses[0])
img_rgb = pub.getRGBImage(responses[1])
if CLAHE_ENABLED:
img_rgb = pub.enhanceRGB(img_rgb)
pub.GetCurrentTime()
msg_rgb = pub.CreateRGBMessage(img_rgb)
msg_d = pub.CreateDMessage(img_depth)
msg_info = pub.CreateInfoMessage()
msg_tf = pub.CreateTFMessage()
publisher_rgb.publish(msg_rgb)
publisher_d.publish(msg_d)
publisher_info.publish(msg_info)
publisher_tf.publish(msg_tf)
airsim.wait_key('Press any key to get camera parameters')
for camera_name in range(5):
camera_info = client.simGetCameraInfo(str(camera_name))
print("CameraInfo %d: %s" % (camera_name, pp.pprint(camera_info)))
airsim.wait_key('Press any key to get images')
for x in range(3): # do few times
z = x * -20 - 5 # some random number
client.simSetVehiclePose(airsim.Pose(airsim.Vector3r(z, z, z), airsim.to_quaternion(x / 3.0, 0, x / 3.0)), True)
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)
def process_image(client, img_res):
image_response = client.simGetImages([airsim.ImageRequest('0', airsim.ImageType.Scene, False, False)])[0]
img_1d = np.fromstring(image_response.image_data_uint8, dtype=np.uint8) # get numpy array
img_bgr = img_1d.reshape(image_response.height, image_response.width, 3) # reshape array to 4 channel image array H X W X 3
img_resized = cv2.resize(img_bgr, (img_res, img_res)).astype(np.float32)
img_batch_1 = np.array([img_resized])
cam_pos = image_response.camera_position
cam_orientation = image_response.camera_orientation
return img_batch_1, cam_pos, cam_orientation
def get_image_and_publish(self):
image = self.client.simGetImage("0", airsim.ImageType.Scene)
image = cv2.imdecode(
airsim.string_to_uint8_array(image),
cv2.IMREAD_UNCHANGED)[:, :, :3]
self.image_pub.publish(self.bridge.cv2_to_imgmsg(image, 'bgr8'))
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]
airsim.wait_key('Press any key to move vehicles')
f1 = client.moveToPositionAsync(-5, 5, -10, 5, vehicle_name="Drone1")
f2 = client.moveToPositionAsync(5, -5, -10, 5, vehicle_name="Drone2")
f1.join()
f2.join()
airsim.wait_key('Press any key to take images')
# 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
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
print('Drone2: Retrieved images: %d' % len(responses2))
tmp_dir = os.path.join(tempfile.gettempdir(), "airsim_drone")
print ("Saving images to %s" % tmp_dir)
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)))