Issues with subscribing to multiple camera image topics

asked 2021-06-24 15:05:58 -0500

pointsnadpixels gravatar image

ROS: Noetic Ubuntu: 20.04

Hello, I'm having a strange issue when subscribing to multiple cameras simultaneously while I try to save the frames.

This is the code I'm using:

class Camera():
    def __init__(self, camera):

        self.br = CvBridge()
        self.camera_name = camera[0]
        # rospy.resolve_name(self.camera_name, caller_id=None)
        self.inner_folder =  str(save_folder)+'/'+self.camera_name
        self.inner_folder_path = Path(self.inner_folder)
        self.inner_folder_path.mkdir(parents=True,exist_ok=True)
self.topic = camera[1][0]
self.compressed = camera[1][1]
if self.compressed==1:
    self.topic = self.topic+'/compressed'
    self.sub = rospy.Subscriber(self.topic, CompressedImage, self.compressed_callback, queue_size=1)
else:
    self.sub = rospy.Subscriber(self.topic, Image, self.image_callback, queue_size=1)
self.time_beg=False
self.start_time = 0

def compressed_callback(self,msg):
    time = msg.header.stamp
    if self.time_beg == False:
        self.start_time=time
        self.time_beg=True

# time = time - self.start_time    
try:
    np_arr = np.frombuffer(msg.data, np.uint8)
    image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
    # print(self.inner_folder+'/'+str(time)+'.jpg')
    cv2.imwrite(self.inner_folder+'/'+str(time)+'.jpg', image)
except:
    pass

def image_callback(self,msg):
    time = msg.header.stamp
    if self.time_beg == False:
        self.start_time=time
        self.time_beg=True

# time = time - self.start_time    
try:
    image = self.br.imgmsg_to_cv2(msg)
    cv2.imwrite(self.inner_folder+'/'+str(time)+'.jpg', image)
except:
    pass

cameras = {
 'head_front':('/head_front_camera/image_raw',1),
 'front_fisheye':('/front_fisheye_camera/image_raw',1),
 'torso_front_color':('/torso_front_camera/color/image_raw',0),
 'torso_frontIR1':('/torso_front_camera/infra1/image_rect_raw',0),  
 'torso_frontIR2':('/torso_front_camera/infra2/image_rect_raw',0),
 'rear_fisheye':('/rear_camera/fisheye/image_raw',1), 
 'torso_back1':('/torso_back_camera/fisheye1/image_raw',0), 
 'torso_back2':('/torso_back_camera/fisheye2/image_raw',0) 
 }

cameras_list = list(cameras.keys())


if __name__=='__main__':
    rospy.init_node('random_node')

    for i in range(len(cameras_list):
        camera = cameras_list[i]
        topic, compressed =  cameras[camera]    
        cam = Camera((camera, (topic,compressed)))

    rospy.spin()

When I run it for any one camera, it works perfectly well, but when I do it this way, for all of them, the behaviour is pretty erratic, in that it saves a few images in each, and most times stops saving the images. Any ideas on why this could be happening and how I should fix it?

Thanks in advance!

edit retag flag offensive close merge delete