pcdtovideo_pji_deepcamera.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if __name__ == '__main__':
  17. input_dir = sys.argv[1]
  18. #input_dir = '/media/dell/BFAC-F22B/data/pujin_datareturn/pujin_robot-01_data_merge_2023-12-20-02-22-18_obstacledetection_10.bag'
  19. output_dir = sys.argv[2]
  20. #output_dir='/media/dell/BFAC-F22B/data/pujin_datareturn'
  21. bag_name = input_dir.split('/')[-1].split('.')[0]
  22. output_dir = os.path.join(output_dir, bag_name + '_pcd_depthcamera' + '/pcd')
  23. if not os.path.exists(output_dir):
  24. os.makedirs(output_dir)
  25. with rosbag.Bag(input_dir, 'r') as bag:
  26. num_count = 0
  27. for topic, msg, t in bag.read_messages():
  28. if topic == "/camera/depth/points": #/camera/depth/points # /scan_map_icp_amcl_node/scan_point_transformed
  29. num_count += 1
  30. bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
  31. hz = str(float(num_count / bagtime))
  32. print(hz)
  33. #######################解析rosbag中的点云point_concat话题,获得原始pcd文件#######################
  34. command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + ['/camera/depth/points'] + [output_dir]
  35. command = ' '.join(command)
  36. os.system(command)
  37. file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
  38. file2 = os.path.join(output_dir[0:-4], 'jpg')
  39. if not os.path.exists(file1):
  40. os.makedirs(file1)
  41. if not os.path.exists(file2):
  42. os.makedirs(file2)
  43. #######################将原始pcd文件转化成pcd-ascii格式,并生成jpg文件#######################
  44. for root, dirs, files in os.walk(output_dir[0:-4]):
  45. # find .pcd files and do the transfer twice
  46. for file in files:
  47. if "pcd" == root[-3:] and ".pcd" == file[-4:]:
  48. inputFilePath = root + "/" + file
  49. tempFilePath = root[:-3] + "pcd_ascii/" + file
  50. outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
  51. # transfer pcd_utf-8 to pcd_ascii
  52. command = "pcl_convert_pcd_ascii_binary " + \
  53. inputFilePath + \
  54. " " + \
  55. tempFilePath + \
  56. " 0"
  57. # print(command)
  58. os.system(command)
  59. # make sure the temp file exist, invalid input file may cause this issue
  60. if not os.path.exists(tempFilePath):
  61. continue
  62. # transfer pcd_ascii to jpg
  63. #pic = np.zeros([200, 400, 3]) #pic = np.zeros([800, 1600, 3])
  64. #draw_points(pic, tempFilePath)
  65. #cv2.imwrite(outputFilePath, pic)
  66. # print("jpg generated: ", outputFilePath)
  67. #pcd_path = "D:/文件/2-课题/科技部3.2/影子模式/pcd/data/pcd_ascii/1666841252.226625000.pcd"
  68. pic = np.zeros([800, 1600, 3])
  69. f = open(tempFilePath, "r")
  70. ff = f.readlines()[11:]
  71. for i in range(0, len(ff)):
  72. point = ff[i].split()[:4]
  73. point_x = float(point[0])
  74. point_y = float(point[1])
  75. point_z = float(point[2])
  76. points_intensity = float(point[2])
  77. #t = np.dot(np.linalg.inv(a),np.array([point_x,point_y,point_z,1]))
  78. #x = t[0]
  79. #y = t[1]
  80. x = -point_x
  81. y = -point_y
  82. if math.isnan(point_x):
  83. pass
  84. else:
  85. cv2.circle(pic, (int(800 - x * 60), int(400 - y * 60)), 0, (255,255,255), 0)
  86. cv2.imwrite(outputFilePath, pic)
  87. else:
  88. break
  89. #######################将转化的点云jpg合成视频#######################
  90. jpg_list = os.listdir(file2)
  91. if not jpg_list == []:
  92. command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
  93. 'avc1',
  94. '-y'] + [
  95. 'pcd_depthcamera.mp4']
  96. result_string = " ".join(command)
  97. p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  98. p.wait()
  99. shutil.rmtree(file1)
  100. shutil.rmtree(file2)
  101. shutil.rmtree(output_dir)
  102. def parse(input_dir, output_dir):
  103. bag_name = input_dir.split('/')[-1].split('.')[0]
  104. output_dir = os.path.join(output_dir, bag_name + '_pcd_depthcamera' + '/pcd')
  105. if not os.path.exists(output_dir):
  106. os.makedirs(output_dir)
  107. with rosbag.Bag(input_dir, 'r') as bag:
  108. num_count = 0
  109. for topic, msg, t in bag.read_messages():
  110. if topic == "/camera/depth/points": # /camera/depth/points # /scan_map_icp_amcl_node/scan_point_transformed
  111. num_count += 1
  112. bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
  113. hz = str(float(num_count / bagtime))
  114. print(hz)
  115. #######################解析rosbag中的点云point_concat话题,获得原始pcd文件#######################
  116. command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + ['/camera/depth/points'] + [output_dir]
  117. command = ' '.join(command)
  118. os.system(command)
  119. file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
  120. file2 = os.path.join(output_dir[0:-4], 'jpg')
  121. if not os.path.exists(file1):
  122. os.makedirs(file1)
  123. if not os.path.exists(file2):
  124. os.makedirs(file2)
  125. #######################将原始pcd文件转化成pcd-ascii格式,并生成jpg文件#######################
  126. for root, dirs, files in os.walk(output_dir[0:-4]):
  127. # find .pcd files and do the transfer twice
  128. for file in files:
  129. if "pcd" == root[-3:] and ".pcd" == file[-4:]:
  130. inputFilePath = root + "/" + file
  131. tempFilePath = root[:-3] + "pcd_ascii/" + file
  132. outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
  133. # transfer pcd_utf-8 to pcd_ascii
  134. command = "pcl_convert_pcd_ascii_binary " + \
  135. inputFilePath + \
  136. " " + \
  137. tempFilePath + \
  138. " 0"
  139. # print(command)
  140. os.system(command)
  141. # make sure the temp file exist, invalid input file may cause this issue
  142. if not os.path.exists(tempFilePath):
  143. continue
  144. # transfer pcd_ascii to jpg
  145. # pic = np.zeros([200, 400, 3]) #pic = np.zeros([800, 1600, 3])
  146. # draw_points(pic, tempFilePath)
  147. # cv2.imwrite(outputFilePath, pic)
  148. # print("jpg generated: ", outputFilePath)
  149. # pcd_path = "D:/文件/2-课题/科技部3.2/影子模式/pcd/data/pcd_ascii/1666841252.226625000.pcd"
  150. pic = np.zeros([800, 1600, 3])
  151. f = open(tempFilePath, "r")
  152. ff = f.readlines()[11:]
  153. for i in range(0, len(ff)):
  154. point = ff[i].split()[:4]
  155. point_x = float(point[0])
  156. point_y = float(point[1])
  157. point_z = float(point[2])
  158. points_intensity = float(point[2])
  159. # t = np.dot(np.linalg.inv(a),np.array([point_x,point_y,point_z,1]))
  160. # x = t[0]
  161. # y = t[1]
  162. x = -point_x
  163. y = -point_y
  164. if math.isnan(point_x):
  165. pass
  166. else:
  167. cv2.circle(pic, (int(800 - x * 60), int(400 - y * 60)), 0, (255, 255, 255), 0)
  168. cv2.imwrite(outputFilePath, pic)
  169. else:
  170. break
  171. #######################将转化的点云jpg合成视频#######################
  172. jpg_list = os.listdir(file2)
  173. if not jpg_list == []:
  174. command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
  175. 'avc1',
  176. '-y'] + [
  177. 'pcd_depthcamera.mp4']
  178. result_string = " ".join(command)
  179. p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  180. p.wait()
  181. shutil.rmtree(file1)
  182. shutil.rmtree(file2)
  183. shutil.rmtree(output_dir)
  184. return output_dir[0:-4]