Ask Your Question
1

How to add multiple publishers to get 4 camera images?

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

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

Hai, I am writing a rospy code to publish images from 4 cameras. Currently, i trying it with my laptop camera.it 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 = cam.read()

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

        pub.publish(msg)
        rate.sleep()


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

I am using UBUNTU 16.04, KINETIC distro.

edit retag flag offensive close merge delete

Comments

1

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

lucasw gravatar imagelucasw ( 2018-04-09 10:19:02 -0500 )edit

updated..sorry..

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-04-09 10:24:32 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

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

Comments

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

sudo_melvinyesudas gravatar imagesudo_melvinyesudas ( 2018-04-09 11:32:42 -0500 )edit
2

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 imageNEngelhard ( 2018-04-09 11:44:57 -0500 )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 imagesudo_melvinyesudas ( 2018-04-09 11:46:16 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 48 times

Last updated: Apr 09 '18