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

USB camera does not take any pictures when ROS is launched at system startup

asked 2018-07-25 02:51:39 -0600

erjohn gravatar image

I have been trying to take pictures using a USB cam connected to my Jetson TX2. My code works pretty well when I execute it from terminal using the command roslaunch my_ros my_launch_file.launch and takes the pictures and saves them in a particular path. However, when I run the launch file at system startup, the code runs and the USB camera is triggered (the light on the camera tuns on) but no images are generated in the given path. Here is my code along with the explanations.

import rospy
from std_msgs.msg import Float32MultiArray
import cv2
import commands
import time

no_of_cameras = 2 # define number of attached cameras
time.sleep(120) # allow some time for the system see the usb camera

# this function returns the number of connected cameras
def getCameraInfo():

   Res=commands.getoutput("ls -l /dev/video*") #search for cameras attached
   CamCnt=Res.count("video") #getting number of cameras attached the board
   return CamCnt/2 

def process_image(frame, i):
    ### Image Processing code comes here
    if i == 6:
        res = True
    if i != 6:
        res = False    
    return res      

def talker():
    pub = rospy.Publisher('servo', Float32MultiArray, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    # check for the number of cameras continuously 
    while True: 
        er = getCameraInfo()
        if er > 1:

    cap = cv2.VideoCapture(1) # indicator light on my usb cam is turned ON when this command is executed

    rate1 = rospy.Rate(0.5)

    forward_slow = Float32MultiArray()  
    dont_move = Float32MultiArray() = [90,84] = [90,90]

    i = 0
    while not rospy.is_shutdown():
        image_name = "/home/nvidia/Desktop/my_ros/cam" + str(i) + ".png" # define path and name of the image
        cv2.imwrite(image_name, frame)
        res = process_image(frame, i) # image processing function. not related to to the problem

        if res:

        if not res:

        i = i + 1
    cap.release() # indicator light on my usb cam is turned OFF when this command is executed

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

What I am basically trying to do here is to take pictures with predefined intervals and then process them. According to the result of image processing, I will control robot with some basic commands (go forward, dont move,...). But I cannot figure out what sort of problem may be preventing the program from saving images when it is run automatically at system startup. Any kind of comments are appreciated (ros distro: kinetic)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-07-27 09:03:04 -0600

erjohn gravatar image

I solved the problem by following the solution given here.

I used to call roslaunch in my rc.local file as

su ubuntu -c "source /home/nvidia/catkin_ws/devel/ ; roslaunch my_ros my_launch_file.launch > /dev/null 2>&1 &"

Now, I replaced it with /home/nvidia/ where content file is

cd $home    
source /home/nvidia/catkin_ws/devel/setup.bash    
roslaunch my_ros my_launch_file.launch    
exit 0

Restarting the Jetson after making the file executable with the command $ sudo chmod u+x /home/username/, I managed to save the image files. They are read-only though it not a problem for me.

edit flag offensive delete link more

Question Tools



Asked: 2018-07-25 02:51:39 -0600

Seen: 361 times

Last updated: Jul 27 '18