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

How to add multiple publishers to get 4 camera images?

asked 2018-04-09 09:37:31 -0600

updated 2018-04-09 11:47:14 -0600

Hai, I am writing a rospy code to publish images from 4 cameras. Currently, i trying it with my laptop works..but how to add multiple publishers for camera. My code is...

#!/usr/bin/env python

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

def webcam_pub():
    pub = rospy.Publisher('webcam/image_raw', Image, queue_size=1)
    rospy.init_node('webcam_pub', anonymous=True)
    rate = rospy.Rate(60) # 60hz

    cam = cv2.VideoCapture(0)
    bridge = CvBridge()

    if not cam.isOpened():
         sys.stdout.write("Webcam is not available")
         return -1

    while not rospy.is_shutdown():
        ret, frame =

        msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")
        if ret:
             rospy.loginfo("Capturing image failed.")


if __name__ == '__main__':
    except rospy.ROSInterruptException:

I am using UBUNTU 16.04, KINETIC distro.

edit retag flag offensive close merge delete



Cut and paste the text from the terminal window instead of using a screenshot.

lucasw gravatar image lucasw  ( 2018-04-09 10:19:02 -0600 )edit


sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-04-09 10:24:32 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-09 11:20:14 -0600

NEngelhard gravatar image

Your code works, you are only interpreting the return value wrong. "ret" is set if the capturing worked!

If you add a cv2.waitKey(1) after the imshow, the image is also shown. You should also only create publishers or subscribers after you created the nodehandle.

edit flag offensive delete link more


thank you i add cv2.waitKey(1) camera is working..but it still showing [INFO] [1523284871.416538]: Capturing image failed.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-04-09 11:32:42 -0600 )edit

Please do not change your question that much after you got the first feedback. Your code doesn't even contain the loginfo anymore...

NEngelhard gravatar image NEngelhard  ( 2018-04-09 11:44:57 -0600 )edit

thank you so much, i add cv2.waitKey(1) camera is working..but how can i publish more camera images? i mean i have 3 more cameras. but when i remove log info, that works without error.

sudo_melvinyesudas gravatar image sudo_melvinyesudas  ( 2018-04-09 11:46:16 -0600 )edit

Question Tools

1 follower


Asked: 2018-04-09 09:37:31 -0600

Seen: 246 times

Last updated: Apr 09 '18