Home > Net >  Subscribing to Image Topic but doesn't output images
Subscribing to Image Topic but doesn't output images

Time:09-24

I have the following code that I've been struggling to get images from. There's a topic "CompressedImage" that has jpeg images from a camera in my robot. Below is my attempt. Terminal just gives me the versions from print and exits out.

#!/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 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


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

CodePudding user response:

This is because you are not spinning ROS. Check out the corresponding ROS tutorial to create a subscriber where

# spin() simply keeps python from exiting until this node is stopped
rospy.spin()

is called to ensure ROS can do his job (calling callbacks, ...).

So you should replace your lines

# set rate
rate = rospy.Rate(1000) # 1000hz

with the spinning ones.

  • Related