0
$\begingroup$

Rosanswers logo

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:
            break 
        time.sleep(5)

    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()
    forward_slow.data = [90,84]
    dont_move.data = [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
        ret,frame=cap.read()
        cv2.imwrite(image_name, frame)
        res = process_image(frame, i) # image processing function. not related to to the problem
        
        if res:
            rospy.loginfo(dont_move)
            pub.publish(dont_move)
            rospy.sleep(1)  
            
        if not res:
            rospy.loginfo(forward_slow)
            pub.publish(forward_slow)
            rospy.sleep(1)

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

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

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)


Originally posted by erjohn on ROS Answers with karma: 13 on 2018-07-25

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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/setup.sh ; roslaunch my_ros my_launch_file.launch > /dev/null 2>&1 &"

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

#!/bin/bash    
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 auto.sh file executable with the command $ sudo chmod u+x /home/username/auto.sh, I managed to save the image files. They are read-only though it not a problem for me.


Originally posted by erjohn with karma: 13 on 2018-07-27

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.