Change webcam to an usb cam in Rviz [closed]

asked 2019-12-20 14:14:00 -0600

abraham1798 gravatar image

updated 2019-12-26 12:17:19 -0600

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 usb_cam in github and change this 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

edit retag flag offensive reopen merge delete

Closed for the following reason OpenCV Question: THe OpenCV community prefers to answer questions at: http://answers.opencv.org/questions/ by stevemacenski
close date 2019-12-20 14:22:41.867618

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

abraham1798 gravatar imageabraham1798 ( 2019-12-26 12:18:32 -0600 )edit