【AirSim+Python】image API和无人机获取图像

没错!这个还是b站【皮卡丘上大学啦】 up主学习的代码。我就是懒!今天下午敲得每一行代码都不能白敲,放在这方便我以后复制!【AirSim+Python】image API和无人机获取图像_第1张图片up主原代码分享链接:在这!!!

1.image API获取相机图像

使用的时候根据自己需求进行注释:

import airsim
import numpy as np
import cv2

# 与airsim建立连接
client = airsim.MultirotorClient()
client.confirmConnection()

# 1.直接使用simGetImage获取PNG格式的彩色图,并保存成.png格式
# response = client .simGetImage('0', airsim .ImageType .Scene, vehicle_name='Drone')
# f = open('D:/Revive_Python/Airsim python/screen/scene.png', 'wb')
# f.write(response)
# f.close()

# 2.使用simGetImages 获取PNG格式分割图,并保存成.png格式的图片文件
# responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Segmentation,
#                                                          pixels_as_float= False, compress= True)])
# f = open('D:/Revive_Python/Airsim python/screen/seg.png', 'wb')
# f.write(responses[0].image_data_uint8 )
# f.close()

# 3.使用simGetImages 获取PNG格式红外图和表面法向图,并保存成2个.png格式的图片文件
# responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Infrared,
#                                                           pixels_as_float= False, compress= True),
#                                   airsim .ImageRequest(0, airsim .ImageType .SurfaceNormals,
#                                                        pixels_as_float= False, compress= True)])
# print(responses)
# 保存红外图
# f = open('D:/Revive_Python/Airsim python/screen/infrared.png', 'wb')
# f.write(responses[0].image_data_uint8)
# f.close()
# # 保存表面法线图
# f = open('D:/Revive_Python/Airsim python/screen/surface.png', 'wb')
# f.write(responses[1].image_data_uint8)
# f.close()

# 4.保存图像Array格式图像

# 读取图像数据,Array格式
responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Scene,
                                                         pixels_as_float= False, compress= False)])
# 将bytes格式转化为 array格式,fromstring→frombuffer
img_ld = np.frombuffer(responses[0].image_data_uint8, dtype=np.uint8)
img_bgr = img_ld.reshape(responses[0].height, responses[0].width, 3)  # 3表示3通道

# 保存为图片文件
cv2 .imwrite('scene.png', img_bgr)  # 保存为.png格式的图像文件
cv2 .imwrite('scene.jpg', img_bgr)  # 保存为.jpg格式的图像文件
cv2 .imwrite('scene.tif', img_bgr)  # 保存为.tif格式的图像文件
cv2 .imwrite('scene.bmp', img_bgr)  # 保存为.bmp格式的图像文件

2.无人机随机位置和获取照片并保存位置

import airsim
import os
import numpy as np
import pandas as pd

# 连接到AirSim模拟器
client = airsim.MultirotorClient()
client.confirmConnection()

# 获取图像路径
folder_path = "screen"

# 保存位姿信息的空DataFrame
poses_df = pd.DataFrame(columns=['index', 'x', 'y', 'z', 'yaw', 'pitch', 'roll'])

# 设置随机采样的范围和数量
num_samples = 50  # 需要采样的数量
x_min, x_max, y_min, y_max, z_min, z_max = -4, 4, -4, 4, -5, -2  # 位置范围
yaw_min, yaw_max, pitch_min, pitch_max, roll_min, roll_max = -90, 90, -45, 45, -45, 45  # 姿态范围

# 相机列表
camera_list = ["0", "1", "2", "3", "4"]

# 随机采样并保存图像和位姿信息
poses_list = []
for i in range(num_samples):
   #  \随机生成目标位置,并设置姿态朝向
   x = np.random.uniform(x_min, x_max)
   y = np.random.uniform(y_min, y_max)
   z = np.random.uniform(z_min, z_max)
   yaw = np.random.uniform(yaw_min, yaw_max)
   pitch = np.random.uniform(pitch_min, pitch_max)
   roll = np.random.uniform(roll_min, roll_max)
   pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))
   poses_list.append({'index': i, 'x': x, 'y': y, 'z': z, 'yaw': yaw, 'pitch': pitch, 'roll': roll})
   # 移动到目标位置
   client.simSetVehiclePose(pose, True)

   #  获取相机图像
   # responses = client.simGetImages([airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)])
   # img_raw = responses[0]

   # 遍历相机列表,获取每个相机的图像
   for j, camera_name in enumerate(camera_list):
       # 获取相机图像
       responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False,
                                                            False)])
       img_raw = responses[0]

       # 将字节流转换为PIL的Image对象
       img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
       img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)

       # 保存PNG格式的图像
       img_filename = "pose_{0}_x_{1:.2f}_y_{2:.2f}_z_{3:.2f}_yaw_{4:.2f}_pitch_{5:.2f}_roll_{6:.2f}_camera_{4}.png".format(
           i, x, y, z, yaw, pitch, roll, j)
       img_filepath = os.path.join(folder_path, img_filename)
       airsim.write_png(os.path.normpath(img_filepath), img_rgb)

print("全部图像和位姿信息均已保存到文件夹:", folder_path)

# 将位姿信息保存到csv文件中
poses_df = pd.DataFrame(poses_list)
poses_df.to_csv(os.path.join(folder_path, 'poses.csv'), index=False)

3.根据2中获取的无人机位置进行拍照

import airsim
import os
import csv
import numpy as np


client = airsim.MultirotorClient()
client.confirmConnection()

# 设置相机和文件路径
camera_list = ["0", "1", "2", "3", "4"]
folder_path = "D:/Revive_Python/Airsim python/square"

# 读取位姿信息文件(csv格式)
poses_csv_file = open("D:/Revive_Python/Airsim python/screen/poses.csv", "r")
pos_reader = csv.DictReader(poses_csv_file)

# 循环采样并保存图像和位姿信息
for i, row in enumerate(pos_reader):
         x, y, z = float(row['x']), float(row['y']), float(row['z'])
         yaw, pitch, roll = float(row['yaw']), float(row['pitch']), float(row['roll'])
         pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))

         # 移动到目标位置
         client.simSetVehiclePose(pose, True)

         # 遍历相机列表,获取每个相机的图像
         for j, camera_name in enumerate(camera_list):
                    responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False, False)])
                    img_raw = responses[0]

                    # 将字节流转换为PIL的Image对象
                    img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
                    img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)

                    # 保存PNG格式的图像
                    img_filename = "pose_{0}_x_{1:.2f}_y_{2:.2f}_z_{3:.2f}_yaw_{4:.2f}_pitch_{5:.2f}_roll_{6:.2f}_camera_{7}.png".format(i, x, y, z, yaw, pitch, roll, j)
                    img_filepath = os.path.join(folder_path, img_filename)
                    airsim.write_png(os.path.normpath(img_filepath), img_rgb)

print("图像和位姿信息均已保存到文件夹:", folder_path)

你可能感兴趣的:(python,无人机,开发语言)