Advertisement
Guest User

Untitled

a guest
Oct 4th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import cv2
  4. import rospy
  5. from sensor_msgs.msg import Image, PointCloud2, CameraInfo
  6. import os
  7. import yaml
  8. import rosbag
  9. from cv_bridge import CvBridge
  10.  
  11. rospy.init_node('Translator')
  12.  
  13.  
  14. class Translator:
  15.     mode = 'republish'
  16.     cam_ids = [i for i in range(4)]
  17.     video_name = 'econVideo_2021-09-29-10-29-58'
  18.     camera_info_msg = CameraInfo()
  19.     bridge = CvBridge()
  20.  
  21.     def __init__(self, input_bag, video_path, calibration_files_path):
  22.         # Takes as input a ROSbag containing
  23.         self.bag = input_bag    
  24.         self.video_path = video_path
  25.         self.calibration_files_path = calibration_files_path
  26.  
  27.     #@staticmethod
  28.     #def
  29.     #orig = os.path.splitext(bag)[0] + ".orig.bag"
  30.     #move(bag, orig)
  31.  
  32.     def translate(self, video_path):
  33.         setup_cameras(video_path)
  34.         do_translate()
  35.  
  36.     def setup_cameras(self):
  37.  
  38.         self.cameras = dict()
  39.  
  40.         for cid in self.cam_ids:
  41.             self.cam_info = dict()
  42.             print("{}".format(self.video_path) + "/{}_{}.mkv".format(self.video_name, cid))
  43.             self.cam_info['cap'] = cv2.VideoCapture("{}".format(self.video_path) + "/{}_{}.mkv".format(self.video_name, cid))
  44.             self.cameras[cid] = self.cam_info
  45.  
  46.         if self.mode == 'republish':
  47.         # Setting up camera, camera_info & velodyne publishers. Also setting up cv2.VideoCapture for each video file.
  48.             for cid in self.cam_ids:
  49.                 self.cam_info['publisher'] = rospy.Publisher('/econ_cam_{}/image_raw'.format(cid), Image, queue_size=25)
  50.                 self.cam_info['publisher_info'] = rospy.Publisher('/econ_cam_{}/camera_info'.format(cid), CameraInfo, queue_size=25)
  51.                 self.cameras[cid] = self.cam_info
  52.             self.velo_publisher = rospy.Publisher('/velodyne_points', PointCloud2, queue_size=10)
  53.             print(self.cameras[1]['publisher'])
  54.         return self.cameras, self.velo_publisher
  55.  
  56.     @staticmethod
  57.     def parse_camera_info_from_yaml(yaml_fname):
  58.         info = {}
  59.  
  60.         if os.path.isfile(yaml_fname): # if a file containing camera info exists extract its content.
  61.             with open(yaml_fname, "r") as file_handle:
  62.                 calib_data = yaml.load(file_handle)
  63.             # Parse
  64.             info['width'] = calib_data["image_width"]
  65.             info['height'] = calib_data["image_height"]
  66.             info['K'] = calib_data["camera_matrix"]["data"]
  67.             info['D'] = calib_data["distortion_coefficients"]["data"]
  68.             info['R'] = calib_data["rectification_matrix"]["data"]
  69.             info['P'] = calib_data["projection_matrix"]["data"]
  70.             info['distortion_model'] = calib_data["distortion_model"]
  71.         else: # if a file containing camera info does not exist create dummy camera info.
  72.             info['width'] = 0
  73.             info['height'] = 0
  74.             info['K'] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  75.             info['D'] = [0, 0, 0, 0, 0]
  76.             info['R'] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  77.             info['P'] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  78.             info['distortion_model'] = "plumb_bob"
  79.         return info
  80.  
  81.     def cam_info_data(self):
  82.         self.info_data = {}
  83.  
  84.         for idx in self.cameras.keys():
  85.             yaml_fname = '{}'.format(self.calibration_files_path) + 'cam-{}-camerainfo.yaml'.format(idx)
  86.             self.info_data[idx] = self.parse_camera_info_from_yaml(yaml_fname)
  87.             #print(self.info_data[idx])
  88.         return self.info_data
  89.  
  90.     def pub_camera_info(self, frames, timestamp, info):
  91.         for idx, frame in enumerate(frames):
  92.             self.camera_info_msg.header.stamp = timestamp # NOT TAKEN BY SOMETHING YET
  93.             self.camera_info_msg.header.frame_id = 'cam_{}'.format(idx)
  94.             self.camera_info_msg.width = info[idx]["width"]
  95.             self.camera_info_msg.height = info[idx]["height"]
  96.             self.camera_info_msg.K = info[idx]["K"]
  97.             self.camera_info_msg.D = info[idx]["D"]
  98.             self.camera_info_msg.R = info[idx]["R"]
  99.             self.camera_info_msg.P = info[idx]["P"]
  100.             self.camera_info_msg.distortion_model = info[idx]["distortion_model"]
  101.             if self.mode == 'republish':
  102.                 self.cameras[idx]['publisher_info'].publish(self.camera_info_msg)
  103.             #elif mode == 'rewrite':
  104.             #   outbag.write('/econ_cam_{}/camera_info'.format(idx), camera_info_msg, timestamp
  105.  
  106.  
  107.     def read_frames(self, msg):
  108.         frames = [None for i in range(4)]
  109.  
  110.         for idx, cam in enumerate(self.cameras):
  111.  
  112.             ret, frames[idx] = self.cameras[idx]['cap'].read()
  113.             if not ret:
  114.                 print("Non-valid frame.")
  115.                 break
  116.             elif not any(frame is None for frame in frames): # Wait to gather all the frames from the videos.
  117.                 timestamp = msg.header.stamp
  118.                 return frames, timestamp
  119.  
  120.     def pub_imgs(self,frames, cameras, timestamp):
  121.         for idx, frame in enumerate(frames):
  122.             img_msg = self.bridge.cv2_to_imgmsg(frame, encoding="bgr8")
  123.             img_msg.header.frame_id = 'cam_{}'.format(idx)
  124.             img_msg.header.stamp = timestamp
  125.             if self.mode == 'republish':
  126.                 self.cameras[idx]['publisher'].publish(img_msg)
  127.             elif self.mode == 'rewrite':
  128.                 pass #outbag.write('/econ_cam_{}/image_raw'.format(idx), img_msg, timestamp)
  129.  
  130.  
  131.     def translate(self):
  132.         info_data = self.cam_info_data()
  133.         bag = rosbag.Bag(self.bag)
  134.         cameras = self.cameras
  135.  
  136.         for topic, msg, t in bag.read_messages():
  137.             if topic=='/velodyne_points':
  138.                 self.velo_publisher.publish(msg)
  139.             elif topic=='/econ_vid':
  140.                 frames, timestamp = self.read_frames(msg)
  141.                 self.pub_imgs(frames, cameras, timestamp)
  142.                 self.pub_camera_info(frames, timestamp, info_data)
  143.  
  144. test_1 = Translator('/home/u2m/time_check/check.bag', '/home/u2m/time_check', '/home/u2m/time_check/')
  145.  
  146.  
  147. test_1.setup_cameras()
  148. #print(test_1.cameras)
  149. #test_1.cam_info_data()
  150. #print(test_1.cam_info_data())
  151.  
  152. print(test_1.cameras[1])
  153.  
  154. test_1.translate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement