Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor + more versatile code. #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions src/image_publisher.py

This file was deleted.

14 changes: 9 additions & 5 deletions src/image_publisher_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand All @@ -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
Expand All @@ -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:
Expand Down