无法从 CompressedImage 消息中获取视频流

Cannot get video stream from CompressedImage message

我在这里的某个地方犯了一个我似乎无法弄清楚的新手错误。我有以下有效的示例代码。下半部分是我尝试从我的机​​器人获取视频源。启动代码时,我什么也得不到。甚至 window.

也没有
#!/usr/bin/env python

import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image, CompressedImage
import time

class LineFollower(object):

    def __init__(self):

        rospy.logwarn("Init line Follower")
        self.bridge = CvBridge()       
        self.image_sub = rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, self.camera_callback, queue_size = 1)
        time.sleep(5) 

    def camera_callback(self,data):

        try:
            image_np = self.bridge.compressed_imgmsg_to_cv2(data)
        except CvBridgeError as e:
            print(e)

      
        cv2.imshow("Full Img", image_np)

        cv2.waitKey(1)

    
    def clean_up(self):
         cv2.destroyAllWindows()

def main():
    rospy.init_node('line_following_node', anonymous=True)

    line_follower_object = LineFollower()

    rate = rospy.Rate(5) #originally 5
    ctrl_c = False
    def shutdownhook():
        # Works better than the rospy.is_shut_down()
        line_follower_object.clean_up()
        rospy.loginfo("shutdown time!")
        ctrl_c = True

    rospy.on_shutdown(shutdownhook)

    while not ctrl_c:
        rate.sleep()

if __name__ == '__main__':
    main()

我正在处理的代码如下:

#!/usr/bin/env python
import cv2
import numpy as np
from timeit import default_timer as timer
from std_msgs.msg import Float64
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy

import sys
print(sys.version)
print(cv2.__version__)

height = 480
width = 640
global_frame = np.zeros((height,width,3), np.uint8)
def calculate_lane_pose(frame):


    # Display the resulting frame
    cv2.imshow('Frame', frame)
  

def camera_callback(self,data):

    try:
        global_frame = self.CvBridge.compressed_imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)

    height, width, channels = global_frame.shape
    print(height)
    cv2.imshow("Original", global_frame)

def lane_pose_publisher():
    # Set the node name
    rospy.init_node('lane_pose_publisher', anonymous=True)

    rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, queue_size = 1)
    # set rate
    rate = rospy.Rate(1000) # 1000hz

    while (1):
        rate.sleep()

        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
    # cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    try:
        lane_pose_publisher()
    except rospy.ROSInterruptException:
        pass

您的示例代码未在对 rospy.Subscriber() 的调用中设置回调。需要这样定义

#!/usr/bin/env python
import cv2
import numpy as np
from timeit import default_timer as timer
from std_msgs.msg import Float64
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy

import sys
print(sys.version)
print(cv2.__version__)

height = 480
width = 640
global_frame = np.zeros((height,width,3), np.uint8)
def calculate_lane_pose(frame):


    # Display the resulting frame
    cv2.imshow('Frame', frame)
  

def camera_callback(data):

    bridge = CvBridge() 

    try:
        global_frame = bridge.compressed_imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)

    height, width, channels = global_frame.shape
    print(height)
    cv2.imshow("Original", global_frame)

def lane_pose_publisher():
    # Set the node name
    rospy.init_node('lane_pose_publisher', anonymous=True)

    rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, camera_callback, queue_size = 1)
    # set rate
    rate = rospy.Rate(1000) # 1000hz

    while (1):
        rate.sleep()

        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
    # cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    try:
        lane_pose_publisher()
    except rospy.ROSInterruptException:
        pass