AirSim图像获取

图像获取API

import airsim #pip install airsim

# for car use CarClient() 
client = airsim.MultirotorClient()

png_image = client.simGetImage("0", airsim.ImageType.Scene)
# do something with image

参数反别是“0” , 0
ImageType一共8种类型
simGetImage获得单个图片

    def simGetImage(self, camera_name, image_type, vehicle_name = '', external = False):
        """
        Get a single image

        Returns bytes of png format image which can be dumped into abinary file to create .png image
        `string_to_uint8_array()` can be used to convert into Numpy unit8 array
        See https://microsoft.github.io/AirSim/image_apis/ for details

        Args:
            camera_name (str): Name of the camera, for backwards compatibility, ID numbers such as 0,1,etc. can also be used
            image_type (ImageType): Type of image required
            vehicle_name (str, optional): Name of the vehicle with the camera
            external (bool, optional): Whether the camera is an External Camera

        Returns:
            Binary string literal of compressed png image
        """
#todo : in future remove below, it's only for compatibility to pre v1.2
        camera_name = str(camera_name)

#because this method returns std::vector < uint8>, msgpack decides to encode it as a string unfortunately.
        result = self.client.call('simGetImage', camera_name, image_type, vehicle_name, external)
        if (result == "" or result == "\0"):
            return None
        return result
class ImageType(metaclass=_ImageType):
    Scene = 0				#景象
    DepthPlanar = 1			#深度平面
    DepthPerspective = 2	#深度透视
    DepthVis = 3			#深度Vis(可见光?)
    DisparityNormalized = 4	#
    Segmentation = 5
    SurfaceNormals = 6
    Infrared = 7

simGetImages获得多个图片,它一共三个参数:需求,名字,是否有外部相机?

    def simGetImages(self, requests, vehicle_name = '', external = False):
        """
        Get multiple images

        See https://microsoft.github.io/AirSim/image_apis/ for details and examples

        Args:
            requests (list[ImageRequest]): Images required
            vehicle_name (str, optional): Name of vehicle associated with the camera
            external (bool, optional): Whether the camera is an External Camera

        Returns:
            list[ImageResponse]:
        """
        responses_raw = self.client.call('simGetImages', requests, vehicle_name, external)
        return [ImageResponse.from_msgpack(response_raw) for response_raw in responses_raw]

截取两张图片。分别是类型0和类型1
保存在D:\ZYG\Atemp文件夹下,
os.path.normpath是规范path字符串形式,指定文件路径

# ready to run example: PythonClient/multirotor/hello_drone.py
import airsim
import os

# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)

# Async methods returns Future. Call join() to wait for task to complete.
client.takeoffAsync().join()
client.moveToPositionAsync(-10, 10, -10, 5).join()

# take images
responses = client.simGetImages([
    airsim.ImageRequest("0", airsim.ImageType.DepthVis),
    airsim.ImageRequest("1", airsim.ImageType.DepthPlanar, True)])
print('Retrieved images: %d' % (len(responses)))

# do something with the images
for response in responses:
    if response.pixels_as_float:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
        airsim.write_pfm(os.path.normpath('D:\ZYG\Atemp/py1.pfm'), airsim.get_pfm_array(response))
    else:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
        airsim.write_file(os.path.normpath('D:\ZYG\Atemp/py1.png'), response.image_data_uint8)

hello_drone代码部分卡在airsim.wait_key(‘Press any key to takeoff’)

一直输入 不会结束

import setup_path
import airsim

import numpy as np
import os
import tempfile
import pprint
import cv2

# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)

state = client.getMultirotorState()
s = pprint.pformat(state)
print("state: %s" % s)

imu_data = client.getImuData()
s = pprint.pformat(imu_data)
print("imu_data: %s" % s)

barometer_data = client.getBarometerData()
s = pprint.pformat(barometer_data)
print("barometer_data: %s" % s)

magnetometer_data = client.getMagnetometerData()
s = pprint.pformat(magnetometer_data)
print("magnetometer_data: %s" % s)

gps_data = client.getGpsData()
s = pprint.pformat(gps_data)
print("gps_data: %s" % s)

airsim.wait_key('Press any key to takeoff')

解决方法是在菜单栏Run->Edit Configurations->选中文件勾选下方Emulate terminal in output console
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用 AirSim 拍摄图像获取拍摄定位的 Python 代码: ```python import airsim import cv2 import os import numpy as np # connect to the AirSim simulator client = airsim.MultirotorClient() client.confirmConnection() client.enableApiControl(True) client.armDisarm(True) # set camera parameters camera_name = "0" camera_pos = airsim.Vector3r(0.0, 0.0, 0.0) camera_dir = airsim.Vector3r(0.0, 0.0, 0.0) fov = airsim.to_quaternion(0.0, 0.0, 0.0) camera_info = client.simGetCameraInfo(camera_name) # set image path image_dir = "./images/" if not os.path.exists(image_dir): os.makedirs(image_dir) # take images for i in range(10): # set camera pose client.simSetCameraPose(camera_name, airsim.Pose(camera_pos, fov)) client.simSetCameraOrientation(camera_name, airsim.to_quaternion(camera_dir.x_val, camera_dir.y_val, camera_dir.z_val)) # take image responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene)]) response = responses[0] img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) img_rgb = img1d.reshape(response.height, response.width, 3) # save image filename = "image" + str(i) + ".png" cv2.imwrite(os.path.join(image_dir, filename), img_rgb) # get camera position and orientation pose = client.simGetVehiclePose() pos = pose.position ori = airsim.to_eularian_angles(pose.orientation) print("Image", i, "taken at:", pos.x_val, pos.y_val, pos.z_val, "with orientation:", ori.x_val, ori.y_val, ori.z_val) # disarm the drone and close the connection client.armDisarm(False) client.enableApiControl(False) ``` 在这个例子中,我们首先连接到 AirSim 仿真器,然后设置相机参数并拍摄图像。我们将图像保存到指定的目录中,并在控制台输出每张图像的拍摄位置和方向。 注意:在运行此代码之前,请确保已经启动了 AirSim 仿真器,并且已经加载了适当的场景和无人机模型。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Karsowe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值