diff --git a/src/image_publisher.py b/src/image_publisher.py deleted file mode 100755 index 1be574d..0000000 --- a/src/image_publisher.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/python3 - -import rospy -import cv2 -from cv_bridge import CvBridge, CvBridgeError -from sensor_msgs.msg import Image - -def publish_image(): - """Capture frames from a camera and publish it to the topic image_raw - """ - image_pub = rospy.Publisher("image_raw", Image, queue_size=10) - bridge = CvBridge() - capture = cv2.VideoCapture("/dev/video0") - - while not rospy.is_shutdown(): - # Capture a frame - ret, img = capture.read() - if not ret: - rospy.ERROR("Could not grab a frame!") - break - - # Publish the image to the topic image_raw - try: - img_msg = bridge.cv2_to_imgmsg(img, "bgr8") - #img_msg.header.stamp = rospy.Time.now() - image_pub.publish(img_msg) - except CvBridgeError as error: - print(error) - - -if __name__=="__main__": - rospy.init_node("my_cam", anonymous=True) - print("Image is being published to the topic /image_raw ...") - publish_image() - - try: - rospy.spin() - except KeyboardInterrupt: - print("Shutting down!") diff --git a/src/image_publisher_launch.py b/src/image_publisher_launch.py index 40be2cb..5187bed 100755 --- a/src/image_publisher_launch.py +++ b/src/image_publisher_launch.py @@ -5,12 +5,13 @@ from cv_bridge import CvBridge, CvBridgeError from sensor_msgs.msg import Image + class camera_publisher: - def __init__(self): + def __init__(self, topic): """Initialize the class """ - self.image_pub = rospy.Publisher("image_raw", Image, queue_size=10) + self.image_pub = rospy.Publisher(topic, Image, queue_size=10) self.bridge = CvBridge() self.capture = cv2.VideoCapture(rospy.get_param("my_webcam/camera_name")) @@ -21,7 +22,7 @@ def publish_image(self): # Capture a frame ret, img = self.capture.read() if not ret: - rospy.ERROR("Could not grab a frame!") + rospy.logerr("Could not grab a frame!") break # Publish the image to the topic image_raw @@ -34,9 +35,12 @@ def publish_image(self): if __name__=="__main__": - cam_pub = camera_publisher() + topic = input("Enter the topic name to publish the image: ") + if not topic: + topic = "image_raw" + cam_pub = camera_publisher(topic) rospy.init_node("my_cam", anonymous=True) - print("Image is being published to the topic image_raw...") + print(f"Image is being published to the topic {topic}...") cam_pub.publish_image() try: