Change webcam to an usb cam in Rviz [closed]

asked 2019-12-20 19:39:56 -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', 30))
    idle = rospy.Rate(30)
    while not rospy.is_shutdown():
        self.publishImage()
        r.sleep()
        idle.sleep()

I tried to change the port 0 to 1 but it does not work

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by LBerger
close date 2019-12-21 09:56:58.905173

Comments

This int(rospy.get_param('camera_port', 0) is off topics. Now if you want to use an usb cam read opencv tutorials

LBerger gravatar imageLBerger ( 2019-12-21 08:47:49 -0600 )edit

That works with out ROS, I want to use it in ROS thanks

abraham1798 gravatar imageabraham1798 ( 2019-12-21 09:06:48 -0600 )edit

ros forum is here

LBerger gravatar imageLBerger ( 2019-12-21 09:57:53 -0600 )edit

I posted there but they told me that It had to be here, why do you closed the post? You don't help me

abraham1798 gravatar imageabraham1798 ( 2019-12-21 10:14:56 -0600 )edit
1

If it works without ROS it means there is no problem with opencv

LBerger gravatar imageLBerger ( 2019-12-21 10:23:11 -0600 )edit