Robotics StackExchange | Archived questions

Change webcam to an usb cam in Rviz

I use this node and display the webcam in Rviz. But I want to change my camera to and usb camera, what I have to do?

import rospy import numpy as np import cv2 from sensor_msgs.msg import CompressedImage from __init__ import ROSNode
     class CameraPublisher(ROSNode):


def __init__(self):
    """OpenCV feature detectors with ros CompressedImage Topics in python.

    This example subscribes to a ros topic containing sensor_msgs 
    CompressedImage. It converts the CompressedImage into a numpy.ndarray, 
    then detects and marks features in that image. It finally displays 
    and publishes the new image - again as CompressedImage topic.
    """
    self.publisher = rospy.Publisher("/image_raw/compressed", CompressedImage, queue_size=4)
    self.msg = CompressedImage()

    rospy.loginfo('Setting camera level...')
    self.camera = cv2.VideoCapture(int(rospy.get_param('camera_port', 0)))

    self.takeImage(lightSettingSnaps=30)

def takeImage(self, lightSettingSnaps=0):
    for i in range(lightSettingSnaps):
        self.takeImage(lightSettingSnaps=0)
    return self.camera.read()[1]

def publishImage(self):
    image = self.takeImage()
    self.msg.header.stamp = rospy.Time.now()
    self.msg.format = "jpeg"
    self.msg.data = np.array(cv2.imencode('.jpg', image)[1]).tostring()
    # Publish new image
    self.publisher.publish(self.msg)

def main(self):
    r = rospy.Rate(rospy.get_param('imageRate', 10))
    idle = rospy.Rate(10)
    while not rospy.is_shutdown():
        self.publishImage()
        r.sleep()
        idle.sleep()

I did it!!! :D

I download the usbcam in github and change this To connect external cam. Locate the usbcam-test.launch file in folder cd ~/catkin-ws/src/usbcam/launch Change <param name="videodevice" value="/dev/video0" /> to From cd ~/catkin-ws/src/usbcam/launch.Run roslaunch usbcam-test.launch

Asked by abraham1798 on 2019-12-20 15:13:13 UTC

Comments

I did it, just download this repository https://github.com/ros-drivers/usb_cam and change the following

To connect external cam. Locate the usb_cam-test.launch file in folder cd ~/catkin-ws/src/usb_cam/launch Change to From cd ~/catkin-ws/src/usb_cam/launch.Run roslaunch usb_cam-test.launch

Asked by abraham1798 on 2019-12-26 13:18:32 UTC

Answers