AirSim icon indicating copy to clipboard operation
AirSim copied to clipboard

Get location and bounding box of objects in environment

Open MudeLin opened this issue 9 years ago • 16 comments

How can I get the 2D location and 3D location of objects in the scene? For example, when the drone is flying on the road, can I get the 3D box of the cars or pedestrains on the road?

MudeLin avatar May 24 '17 01:05 MudeLin

Yes, you can but you will need to dig in to Unreal Engine programming. Basically environment has all these meshes so you can iterate through meshes that interests you and then there are direct methods to get its position and size in world coordinates. If you have never done any Unreal engine programming, its non-trivial, however. I would suggest looking at Unreal Engine programming guide or ask this question in their forums.

sytelus avatar May 25 '17 19:05 sytelus

OK, thanks.

MudeLin avatar May 26 '17 01:05 MudeLin

@Geekking Have you figured this out? I wish there was an easy way to get the coordinates and bounding box of the objects in the environment too. I need the same thing

cangokalp avatar Oct 25 '17 19:10 cangokalp

@Geekking looking for the same thing here.

alessandro-montanari avatar Nov 02 '17 16:11 alessandro-montanari

Probably way too late, but for future reference: You can get bounding boxes without modifying the source code using the segmentation outputs.

First, set the object IDs of everything to zero, except the drone (if you want to get bounding boxes around the drone):

client.simSetSegmentationObjectID("[\w]*", 0, True)
client.simSetSegmentationObjectID("Drone[\w]*", 255, True)

Save the segmentation images as in the hello-drone example.

Now you can calculate the bounding box simply by checking which rows and columns contain non-zero pixels: (there is probably a more efficient way, but it works)

import numpy as np
import cv2

img = cv2.imread('path_to_image.png')

height, width = img.shape[:2]
img_size = np.array([width, height])
start_x, start_y = -1, -1
end_x, end_y = -1, -1

for y in range(height):
    if np.sum(img[y, :, :]) > 0.0:
        end_y = y

        if start_y == -1:
            start_y = y

for x in range(width):
    if np.sum(img[:, x, :]) > 0.0:
        end_x = x

        if start_x == -1:
            start_x = x

img = cv2.rectangle(img, (start_x, start_y), (end_x, end_y), (255, 0, 0))
cv2.imwrite('frame.png', img)

The (very thin, sorry) blue box shows the bounding box in the acquired segmentation image: frame

evroon avatar Mar 07 '21 15:03 evroon

Probably way too late, but for future reference: You can get bounding boxes without modifying the source code using the segmentation outputs.

First, set the object IDs of everything to zero, except the drone (if you want to get bounding boxes around the drone):

client.simSetSegmentationObjectID("[\w]*", 0, True)
client.simSetSegmentationObjectID("Drone[\w]*", 255, True)

Save the segmentation images as in the hello-drone example.

Now you can calculate the bounding box simply by checking which rows and columns contain non-zero pixels: (there is probably a more efficient way, but it works)

import numpy as np
import cv2

img = cv2.imread('path_to_image.png')

height, width = img.shape[:2]
img_size = np.array([width, height])
start_x, start_y = -1, -1
end_x, end_y = -1, -1

for y in range(height):
    if np.sum(img[y, :, :]) > 0.0:
        end_y = y

        if start_y == -1:
            start_y = y

for x in range(width):
    if np.sum(img[:, x, :]) > 0.0:
        end_x = x

        if start_x == -1:
            start_x = x

img = cv2.rectangle(img, (start_x, start_y), (end_x, end_y), (255, 0, 0))
cv2.imwrite('frame.png', img)

The (very thin, sorry) blue box shows the bounding box in the acquired segmentation image: frame

This will only work when there is no overlapping of the object, if anything obscured this object, 2D bounding box will be off.

rancheng avatar May 28 '21 13:05 rancheng

@rancheng Yes that is true, that is exactly what someone noted here as well. So if you want bounding box detections where occlusion is taking place, you can use PR #3472.

evroon avatar May 28 '21 13:05 evroon

But I think that in a lot of cases, the segmentation method suffices, and it is easier to work with, but indeed it has its limitations.

evroon avatar May 28 '21 13:05 evroon

@evroon that is exactly what I am trying to do but sort of struggling with the PR #3472. Is there a way to determine if objects are overlapped or occluded ?.

Thanks.

zohaibjan avatar Aug 09 '22 03:08 zohaibjan

@zohaibjan what do you mean exactly? Segmentation masks typically don't support occlusion, i.e. multiple segmentation IDs per pixel from what I know.

I don't have experience with that PR you link btw.

evroon avatar Aug 09 '22 16:08 evroon

@evroon I am trying to generate object detection database but so far cannot isolate occluded objects from ground truth. I have tried multiple openCV "tricks" but there is still a level of stochasticity in it left which could be detrimental for the training algorithm. So need more deterministic approach rather than a random one.

zohaibjan avatar Aug 10 '22 23:08 zohaibjan

This solution should be fully deterministic, every pixel belonging to the object you want to segment should be highlighted in the mask. I'm not really sure what your exact problem is, maybe if you show an example?

evroon avatar Aug 11 '22 10:08 evroon

@evroon think if a car is occluded behind a tree and only half of it is visible. In that case only half of the segmentation is visible even after switching everything else to -1 or 0 in setSegmentationObjectID. So basically, the question is can I determine if the object is occluded using the segmentation

zohaibjan avatar Aug 11 '22 11:08 zohaibjan

Ah okay, I don't think that's possible AFAIK, but I haven't done anything with AirSim in a while

evroon avatar Aug 11 '22 11:08 evroon

Thanks for the reply though. I am struggling at the moment neither the Object Detection API nor segmentation AFAIK can support this.

zohaibjan avatar Aug 11 '22 23:08 zohaibjan

I wonder if we get the world coordinates, depth image and camera parameters, if projection possible or segmentation better?

Vanessa-Yehui avatar Oct 24 '23 02:10 Vanessa-Yehui