how to show image via opencv using rospy ROS Kinetic
i want to stream my camera using opencv, this is my whole code
#!/usr/bin/env python2.7
import rospy
from sensor_msgs.msg import Image, CompressedImage
import cv2
from cv_bridge import CvBridge, CvBridgeError
import sys
rospy.init_node('opencv_example', anonymous=True)
bridge = CvBridge()
def show_image(img):
cv2.imshow("Image Window", img)
cv2.waitKey(3)
def image_callback(img_msg):
try:
np_arr = np.fromstring(img_msg.data, np.uint8)
image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
cv_image = bridge.imgmsg_to_cv2(img_msg, "bgr8")
except CvBridgeError, e:
rospy.logerr("CvBridge Error: {0}".format(e))
show_image(cv_image)
sub_image = rospy.Subscriber("/raspicam_node/image/compressed", CompressedImage, image_callback)
cv2.namedWindow("Image Window", 1)
while not rospy.is_shutdown():
rospy.spin()
after i try to stream the compressed image from my raspicam node, all i get is this code:
[ERROR] [1635390346.816659]: bad callback: <function image_callback at 0x7f9c224ccb18>
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/anto/catkin_ws/src/opencv_example/src/opencv_ex.py", line 22, in image_callback
cv_image = bridge.imgmsg_to_cv2(img_msg, "bgr8")
File "/home/anto/catkin_ws/src/vision_opencv-kinetic/cv_bridge/python/cv_bridge/core.py", line 163, in imgmsg_to_cv2
dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
AttributeError: 'CompressedImage' object has no attribute 'encoding'
currently, I am working with
- Ubuntu 16.04
- Ros Kinetic
- Open CV 3.3.1
- python 2.7