pcdtovideo_pji_lidar_bak.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Nov 1 13:38:28 2023
  5. @author: dell
  6. """
  7. import subprocess
  8. from subprocess import Popen, PIPE
  9. import os
  10. import sys
  11. import math
  12. import cv2
  13. import numpy as np
  14. import shutil
  15. import rosbag
  16. def draw_points(pic, filename):
  17. f = open(filename, "r")
  18. ff = f.readlines()[11:]
  19. for i in range(0, len(ff)):
  20. point = ff[i].split()[:4]
  21. point_x = float(point[0])
  22. point_y = -float(point[1])
  23. if math.isnan(point_x):
  24. pass
  25. else:
  26. cv2.circle(pic, (int(800 - point_x * 20), int(400 - point_y * 20)), 1, (250, 250, 250), 0)
  27. if __name__ == '__main__':
  28. input_dir = sys.argv[1]
  29. # input_dir = '/media/dell/BFAC-F22B/data/pujin_datareturn/pjirobot_20231207_2023-12-07-07-18-46_54.bag'
  30. output_dir = sys.argv[2]
  31. # output_dir='/media/dell/BFAC-F22B/data/pujin_datareturn'
  32. bag_name = input_dir.split('/')[-1].split('.')[0]
  33. output_dir = os.path.join(output_dir, bag_name + '_pcd_lidar' + '/pcd')
  34. if not os.path.exists(output_dir):
  35. os.makedirs(output_dir)
  36. with rosbag.Bag(input_dir, 'r') as bag:
  37. num_count = 0
  38. for topic, msg, t in bag.read_messages():
  39. if topic == "/scan_map_icp_amcl_node/scan_point_transformed":
  40. num_count += 1
  41. bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
  42. hz = str(float(num_count / bagtime))
  43. print(hz)
  44. #######################解析rosbag中的点云point_concat话题,获得原始pcd文件#######################
  45. command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + ['/scan_map_icp_amcl_node/scan_point_transformed'] + [
  46. output_dir]
  47. command = ' '.join(command)
  48. os.system(command)
  49. # Process_rosbag = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  50. # Process_rosbag.wait()
  51. file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
  52. file2 = os.path.join(output_dir[0:-4], 'jpg')
  53. if not os.path.exists(file1):
  54. os.makedirs(file1)
  55. if not os.path.exists(file2):
  56. os.makedirs(file2)
  57. #######################将原始pcd文件转化成pcd-ascii格式,并生成jpg文件#######################
  58. for root, dirs, files in os.walk(output_dir[0:-4]):
  59. # find .pcd files and do the transfer twice
  60. for file in files:
  61. if "pcd" == root[-3:] and ".pcd" == file[-4:]:
  62. inputFilePath = root + "/" + file
  63. tempFilePath = root[:-3] + "pcd_ascii/" + file
  64. outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
  65. # transfer pcd_utf-8 to pcd_ascii
  66. command = "pcl_convert_pcd_ascii_binary " + \
  67. inputFilePath + \
  68. " " + \
  69. tempFilePath + \
  70. " 0"
  71. # print(command)
  72. os.system(command)
  73. # make sure the temp file exist, invalid input file may cause this issue
  74. if not os.path.exists(tempFilePath):
  75. continue
  76. # transfer pcd_ascii to jpg
  77. pic = np.zeros([800, 1600, 3])
  78. draw_points(pic, tempFilePath)
  79. cv2.imwrite(outputFilePath, pic)
  80. # print("jpg generated: ", outputFilePath)
  81. else:
  82. break
  83. #######################将转化的点云jpg合成视频#######################
  84. jpg_list = os.listdir(file2)
  85. if not jpg_list == []:
  86. command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
  87. 'avc1',
  88. '-y'] + [
  89. 'pcd_lidar.mp4']
  90. result_string = " ".join(command)
  91. p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  92. p.wait()
  93. shutil.rmtree(file1)
  94. shutil.rmtree(file2)
  95. shutil.rmtree(output_dir)
  96. def parse(input_dir, output_dir):
  97. bag_name = input_dir.split('/')[-1].split('.')[0]
  98. output_dir = os.path.join(output_dir, bag_name + '_pcd_lidar' + '/pcd')
  99. if not os.path.exists(output_dir):
  100. os.makedirs(output_dir)
  101. with rosbag.Bag(input_dir, 'r') as bag:
  102. num_count = 0
  103. for topic, msg, t in bag.read_messages():
  104. if topic == "/scan_map_icp_amcl_node/scan_point_transformed":
  105. num_count += 1
  106. bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
  107. hz = str(float(num_count / bagtime))
  108. print(hz)
  109. #######################解析rosbag中的点云point_concat话题,获得原始pcd文件#######################
  110. command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + [
  111. '/scan_map_icp_amcl_node/scan_point_transformed'] + [output_dir]
  112. command = ' '.join(command)
  113. os.system(command)
  114. # Process_rosbag = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  115. # Process_rosbag.wait()
  116. file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
  117. file2 = os.path.join(output_dir[0:-4], 'jpg')
  118. if not os.path.exists(file1):
  119. os.makedirs(file1)
  120. if not os.path.exists(file2):
  121. os.makedirs(file2)
  122. #######################将原始pcd文件转化成pcd-ascii格式,并生成jpg文件#######################
  123. for root, dirs, files in os.walk(output_dir[0:-4]):
  124. # find .pcd files and do the transfer twice
  125. for file in files:
  126. if "pcd" == root[-3:] and ".pcd" == file[-4:]:
  127. inputFilePath = root + "/" + file
  128. tempFilePath = root[:-3] + "pcd_ascii/" + file
  129. outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
  130. # transfer pcd_utf-8 to pcd_ascii
  131. command = "pcl_convert_pcd_ascii_binary " + \
  132. inputFilePath + \
  133. " " + \
  134. tempFilePath + \
  135. " 0"
  136. # print(command)
  137. os.system(command)
  138. # make sure the temp file exist, invalid input file may cause this issue
  139. if not os.path.exists(tempFilePath):
  140. continue
  141. # transfer pcd_ascii to jpg
  142. pic = np.zeros([800, 1600, 3])
  143. draw_points(pic, tempFilePath)
  144. cv2.imwrite(outputFilePath, pic)
  145. # print("jpg generated: ", outputFilePath)
  146. else:
  147. break
  148. #######################将转化的点云jpg合成视频#######################
  149. jpg_list = os.listdir(file2)
  150. if not jpg_list == []:
  151. command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
  152. 'avc1',
  153. '-y'] + [
  154. 'pcd_lidar.mp4']
  155. result_string = " ".join(command)
  156. p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE,
  157. stderr=subprocess.PIPE)
  158. p.wait()
  159. shutil.rmtree(file1)
  160. shutil.rmtree(file2)
  161. shutil.rmtree(output_dir)
  162. return output_dir[0:-4]