AttributeError: '_sensor_msgs__CameraInfo' object has no attribute 'encoding'
I want to save image from a bag file.And I have fellow the example, but I will always get a error like this:
http://library.isr.ist.utl.pt/docs/ro...
Traceback (most recent call last):
File "image.py", line 55, in main() File "image.py", line 50, in main readImage(topic_name) File "image.py", line 31, in readImage cv_image = CvBridge().imgmsg_to_cv2(msg, "32FC1") File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", 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:
/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/__init__.pyc
/opt/ros/kinetic/lib/python2.7/dist-packages/rosbag/__init__.pyc
/opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so
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):
try:
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
I don't know if you noticed, but that tutorial is for Diamondback which is very old and is no longer supported.
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.
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.