0
$\begingroup$

Rosanswers logo

Hello, I created a subscriber node to get data from a topic. Topic broadcasts humans on the camera in a list called human_list. I want to read this data with below code. However when I rosrun it I get an error stating this

[ERROR] [1522830692.692015]: bad callback: <function callback at 0x7f7c0598aaa0>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback cb(msg)
  File "/home/robolab3/catkin_ws/src/hand_raise_detector/scripts/armraiselistener.py", line 8, in callback
    x = human.body_key_points_with_prob.x
AttributeError: 'list' object has no attribute 'x'

rosmsg show openpose_ros_msgs/OpenPoseHumanList is as follows:

msgs/OpenPoseHumanList
  std_msgs/Header header
    uint32 seq
    time stamp
  string frame_id
  std_msgs/Header rgb_image_header
    uint32 seq
    time stamp
    string frame_id 
  int32 num_humans
  openpose_ros_msgs/OpenPoseHuman[] human_list
    int32 num_body_key_points_with_non_zero_prob
    openpose_ros_msgs/PointWithProb[18] body_key_points_with_prob
      float64 x
      float64 y
      float64 prob

My code to subscribe this topic is as follows. I appreciate your help to identify what about my code is wrong.

import rospy
from openpose_ros_msgs.msg import OpenPoseHumanList
def callback(msg):
  for human in msg.human_list:
      x = human.body_key_points_with_prob.x
      y = human.body_key_points_with_prob.y
      prob = human.body_key_points_with_prob.prob
      rospy.loginfo('x: {}, y: {}, prob: {}'.format(x, y, prob))
def armraiselistener():
 rospy.init_node('armraiselistener', anonymous=True)
 rospy.Subscriber("/openpose_ros/human_list", OpenPoseHumanList, callback)
 rospy.spin()
if __name__== '__main__':
 armraiselistener()

Originally posted by tolga-uni-lu on ROS Answers with karma: 33 on 2018-04-04

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

def callback(msg):
  for human in msg.human_list:
      x = human.body_key_points_with_prob.x
      ...

This is a Python issue more than a ROS one, but: human.body_key_points_with_prob is of type list, not of type openpose_ros_msgs/PointWithProb. So the list contains openpose_ros_msgs/PointWithProb instances.

To access those, you'll need to use the [] operator to index into the list. Something like:

x = human.body_key_points_with_prob[0].x

would give you the .x attribute of the first entry in the list.

You would obviously first have to make sure that the list actually contains any items.


Originally posted by gvdhoorn with karma: 86574 on 2018-04-04

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by tolga-uni-lu on 2018-04-04:
Thank you for your answer.

$\endgroup$

Your Answer

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