-1
$\begingroup$

I am using darknet_ros_3d package and writing a subscriber. So far, I have followed the code given as under:

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):
    for box in data.bounding_boxes:
        rospy.loginfo(
            "Xmin: {}, Xmax: {} Ymin: {}, Ymax: {}".format(
                box.xmin, box.xmax, box.ymin, box.ymax
            )
        )


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


if __name__ == '__main__':
    try :
        main()
    except rospy.ROSInterruptException:
        pass

How can I save the values coming from Xmin: {}, Ymin: {}, etc. into the variables for the later usage?

$\endgroup$
1
$\begingroup$

You will need to use classes and save the data into class variables. Try 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


class darknet_ros_3d:

  def __init__(self):
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber('/darknet_ros/bounding_boxes', BoundingBoxes , self.callback)

  def callback(self, data):
    for box in data.bounding_boxes:
      rospy.loginfo(
        "Xmin: {}, Xmax: {} Ymin: {}, Ymax: {}".format(
        box.xmin, box.xmax, box.ymin, box.ymax)
        )
    # save data into variables
    self.Xmin = box.xmin
    self.Xmax = box.xmax
    self.Ymin = box.ymin
    self.Ymax = box.ymax


if __name__ == '__main__':
  darknet_ros_3d()
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
    

You can now use these variables by calling self.(variable) in other parts of your code as long as it is referenced within the class

$\endgroup$
5
  • $\begingroup$ Thanks for the response @Akhil Kurup. I am getting the following error when I called self.Xmin: "AttributeError: darknet_ros_3d instance has no attribute 'Xmin' ". $\endgroup$ – ahmed ali Feb 16 at 7:20
  • $\begingroup$ Since the __init__() function doesn't initialise the Xmin attribute, you will get this error if you try to access the attribute before the callback() is called for the first time. If this is likely, you should either initialise the attributes in the __init__() function, or check if the object has the attribute before attempting to access it using the hasattr() function. $\endgroup$ – Mark Booth Feb 16 at 15:14
  • $\begingroup$ Yes, as Mark has suggested, if you try to access self.(variable) before any value has been set, you will get this error. You could either set self.Xmin = 0.0 in the init or at the place you are trying to access the value, check if these values exist. $\endgroup$ – Akhil Kurup Feb 16 at 16:06
  • $\begingroup$ I have now initialized self.Xmin = 0 and now the error is removed. But I need to ask that will the self.Xmin update after its initial value through the callback function? The code is given as under: def __init__(self): rospy.init_node('listener', anonymous=True) self.Xmin = 0 rospy.Subscriber('/darknet_ros_3d/bounding_boxes', BoundingBoxes3d, self.callback) pose = [self.Xmin, 0.15, 0.3, 0.0, 3.14, 3.14] $\endgroup$ – ahmed ali Feb 16 at 23:26
  • $\begingroup$ self.Xmin will update every time the callback function is called. The init function is only executed once in the beginning. So you might want to populate the pose elsewhere where you want to use it. $\endgroup$ – Akhil Kurup Feb 17 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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