Ask Your Question

AttributeError: '_sensor_msgs__CameraInfo' object has no attribute 'encoding'

asked 2019-01-19 01:40:11 -0500

daohu527 gravatar image

updated 2019-01-19 20:44:27 -0500

jayess gravatar image

I want to save image from a bag file.And I have fellow the example, but I will always get a error like this:

Traceback (most recent call last): 
File "", line 55, in main() File "", line 50, in main readImage(topic_name) File "", line 31, in readImage cv_image = CvBridge().imgmsg_to_cv2(msg, "32FC1") File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/", line 163, in imgmsg_to_cv2 dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) AttributeError: '_sensor_msgs__CameraInfo' object has no attribute 'encoding'

Ros version:

ROS Kinetic Kame

And the cv_bridge is in the path:


I print out the attr of the "msg"

['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_check_types', '_connection_header', '_full_text', '_get_types', '_has_header', '_md5sum', '_slot_types', '_spec', '_type', 'data', 'deserialize', 'deserialize_numpy', 'encoding', 'header', 'height', 'is_bigendian', 'serialize', 'serialize_numpy', 'step', 'width']

By the way I get this error too:

Unrecognized image encoding [yuyv]

my code:

def readImage(topic_name):  
    bag  = rosbag.Bag('2018-11-20-15-54-07.bag') 

    bridge = CvBridge()

    for topic, msg, t in bag.read_messages(topics=topic_name):
            print dir(msg)
            cv_image = CvBridge().imgmsg_to_cv2(msg, "32FC1")
        timestr = "%.6f" %  msg.header.stamp.to_sec()
            image_name = timestr+ ".jpg"
        cv2.imwrite(image_name, cv_image)
        except CvBridgeError as e:
            print e

Any ideas ! thanks a lot

edit retag flag offensive close merge delete



I don't know if you noticed, but that tutorial is for Diamondback which is very old and is no longer supported.

jayess gravatar image jayess  ( 2019-01-19 20:46:07 -0500 )edit

It kinds looks like you're trying to convert CameraInfo messages (you should be trying to convert Image messages). Maybe double-check that you're reading from the correct topic.

ahendrix gravatar image ahendrix  ( 2019-01-19 21:53:22 -0500 )edit

Your code also appears to be trying to read single channel 32 bit floating point images from the bag and save them as JPEGs. JPEG doesn't support float images so that will fail, also as @ahendrix mentioned your trying to read a camera_info topic which carries calibration info not images.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-01-20 04:17:54 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-01-20 19:16:05 -0500

daohu527 gravatar image

@ahendrix You are right, I had convert the "image_short" and "image_short_info", so I remove the "image_short_info", and the error is disappear. @PeteBlackerThe3rd As you mentioned I have remove the camera_info topic,and I got the error "Unrecognized image encoding [yuyv]" Thx!

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2019-01-19 01:31:14 -0500

Seen: 1,571 times

Last updated: Jan 19 '19