ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

import rospy from sensor_msgs.msg import Image from cv_bridge import CvBridge import cv2 import numpy as np

def callback(data):
  br = CvBridge()
  rospy.loginfo("receiving video frame")
  current_frame = br.imgmsg_to_cv2(data)
  cv_image_array = np.array(current_frame, dtype = np.dtype('f8'))
  cv_image_norm = cv2.normalize(cv_image_norm, cv_image_array, 0, 1, cv2.NORM_MINMAX)
  cv2.imshow("camera", cv2.cvtColor(current_frame,cv2.COLOR_BGR2RGB))
  print(cv_image_norm.shape)
  cv2.waitKey(1)

def receive_message():
  rospy.init_node('video_sub_py', anonymous=True)
  rospy.Subscriber('__YOUR_TOPIC_NAME', Image, callback) # check name by rostopic list
  rospy.spin()
  cv2.destroyAllWindows()

if __name__ == '__main__':
  receive_message()
click to hide/show revision 2
No.2 Revision

Code:

import rospy
 from sensor_msgs.msg import Image 
 from cv_bridge import CvBridge 
 import cv2 
 import numpy as np

np
def callback(data):
  br = CvBridge()
  rospy.loginfo("receiving video frame")
  current_frame = br.imgmsg_to_cv2(data)
  cv_image_array = np.array(current_frame, dtype = np.dtype('f8'))
  cv_image_norm = cv2.normalize(cv_image_norm, cv_image_array, 0, 1, cv2.NORM_MINMAX)
  cv2.imshow("camera", cv2.cvtColor(current_frame,cv2.COLOR_BGR2RGB))
  print(cv_image_norm.shape)
  cv2.waitKey(1)

def receive_message():
  rospy.init_node('video_sub_py', anonymous=True)
  rospy.Subscriber('__YOUR_TOPIC_NAME', Image, callback) # check name by rostopic list
  rospy.spin()
  cv2.destroyAllWindows()

if __name__ == '__main__':
  receive_message()

This thing works for me. For getting an Image stream.

Code:

import rospy
from sensor_msgs.msg import Image 
from cv_bridge import CvBridge 
import cv2 
import numpy as np

def callback(data):
  br = CvBridge()
  rospy.loginfo("receiving video frame")
  current_frame = br.imgmsg_to_cv2(data)
  cv_image_array = np.array(current_frame, dtype = np.dtype('f8'))
  cv_image_norm = cv2.normalize(cv_image_norm, cv_image_array, 0, 1, cv2.NORM_MINMAX)
  cv2.imshow("camera", cv2.cvtColor(current_frame,cv2.COLOR_BGR2RGB))
  print(cv_image_norm.shape)
  cv2.waitKey(1)

def receive_message():
  rospy.init_node('video_sub_py', anonymous=True)
  rospy.Subscriber('__YOUR_TOPIC_NAME', Image, callback) # check name by rostopic list
  rospy.spin()
  cv2.destroyAllWindows()

if __name__ == '__main__':
  receive_message()