I'm writing a python script to run with darknet_ros and bebop_autonomy package in order to take the bounding boxes of the detected objects and depending on the position of the detected objects send different flight commands (takeoff, landing, ecc) by using if elif statement. I'm struggling as I don't know how to take the published data from darknet_ros topic (list with detected objects coordinates) and use it in the python script.
Do you have any suggestion on how to do this?

import rospy
from sensor_msgs.msg import Image
from darknet_ros_msgs.msg import BoundingBoxes
from std_msgs.msg import Header
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def main():
    while not rospy.is_shutdown():
        rospy.init_node('listener', anonymous=True)
        rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes , callback)

if __name__ == '__main__':
    try :
    except rospy.ROSInterruptException:
  • $\begingroup$ I recommend starting with a simple ROS tutorial $\endgroup$
    – koverman47
    Apr 11, 2019 at 23:40

1 Answer 1


BoundingBoxes message is a custom created msg by darknet_ros package, so due to this information we will have the like following log to get the X and Y:

def callback(data):
    for box in data.bounding_boxes:
            "Xmin: {}, Xmax: {} Ymin: {}, Ymax: {}".format(
                box.xmin, box.xmax, box.ymin, box.ymax
  • $\begingroup$ Thank you Benyamin, you're answer has been very useful. In case I have more than 1 detected object at the same time how should I modify the callback function to see the boundingboxes of each object? $\endgroup$
    – Ilyas
    Apr 14, 2019 at 8:32
  • $\begingroup$ With this code snippet, you can do it. BoundingBoxes msg is a BoundingBoxe array, so with a loop (above for) you can access them (each BoundingBoxe and its X,Y) $\endgroup$ Apr 14, 2019 at 9:26
  • $\begingroup$ so I should do another loop before for box in data.bounding_boxes? like for n in data.bounding_boxes with n=number of detected objects $\endgroup$
    – Ilyas
    Apr 14, 2019 at 14:57
  • $\begingroup$ no, my answer is enough. my answer loop turns to number of detections. $\endgroup$ Apr 14, 2019 at 16:48
  • $\begingroup$ solved, you're right. I was able to implement what I wanted to do: thanks for your precious help! $\endgroup$
    – Ilyas
    Apr 16, 2019 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.