Ask Your Question
0

AttributeError: 'list' object has no attribute '...'

asked 2018-04-04 03:15:02 -0500

tolga-uni-lu gravatar image

updated 2018-04-04 03:42:24 -0500

gvdhoorn gravatar image

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2018-04-04 03:44:54 -0500

gvdhoorn gravatar image
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.

edit flag offensive delete link more

Comments

Thank you for your answer.

tolga-uni-lu gravatar imagetolga-uni-lu ( 2018-04-04 05:27:51 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-04-04 03:15:02 -0500

Seen: 5,044 times

Last updated: Apr 04 '18