123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- """
- Created on Wed Nov 1 13:38:28 2023
- @author: dell
- """
- import subprocess
- from subprocess import Popen, PIPE
- import os
- import sys
- import math
- import cv2
- import numpy as np
- import shutil
- import rosbag
- if __name__ == '__main__':
- input_dir = sys.argv[1]
-
- output_dir = sys.argv[2]
-
- bag_name = input_dir.split('/')[-1].split('.')[0]
- output_dir = os.path.join(output_dir, bag_name + '_pcd_depthcamera' + '/pcd')
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
-
- with rosbag.Bag(input_dir, 'r') as bag:
- num_count = 0
- for topic, msg, t in bag.read_messages():
- if topic == "/camera/depth/points":
- num_count += 1
-
- bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
- hz = str(float(num_count / bagtime))
- print(hz)
-
-
- command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + ['/camera/depth/points'] + [output_dir]
- command = ' '.join(command)
- os.system(command)
- file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
- file2 = os.path.join(output_dir[0:-4], 'jpg')
- if not os.path.exists(file1):
- os.makedirs(file1)
- if not os.path.exists(file2):
- os.makedirs(file2)
-
-
- for root, dirs, files in os.walk(output_dir[0:-4]):
-
- for file in files:
- if "pcd" == root[-3:] and ".pcd" == file[-4:]:
- inputFilePath = root + "/" + file
- tempFilePath = root[:-3] + "pcd_ascii/" + file
- outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
-
- command = "pcl_convert_pcd_ascii_binary " + \
- inputFilePath + \
- " " + \
- tempFilePath + \
- " 0"
-
- os.system(command)
-
- if not os.path.exists(tempFilePath):
- continue
-
-
-
-
-
-
- pic = np.zeros([800, 1600, 3])
- f = open(tempFilePath, "r")
- ff = f.readlines()[11:]
-
- for i in range(0, len(ff)):
- point = ff[i].split()[:4]
- point_x = float(point[0])
- point_y = float(point[1])
- point_z = float(point[2])
- points_intensity = float(point[2])
-
-
-
- x = -point_x
- y = -point_y
-
- if math.isnan(point_x):
- pass
- else:
- cv2.circle(pic, (int(800 - x * 60), int(400 - y * 60)), 0, (255,255,255), 0)
-
- cv2.imwrite(outputFilePath, pic)
- else:
- break
-
- jpg_list = os.listdir(file2)
- if not jpg_list == []:
- command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
- 'avc1',
- '-y'] + [
- 'pcd_depthcamera.mp4']
- result_string = " ".join(command)
- p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- p.wait()
- shutil.rmtree(file1)
- shutil.rmtree(file2)
- shutil.rmtree(output_dir)
- def parse(input_dir, output_dir):
- bag_name = input_dir.split('/')[-1].split('.')[0]
- output_dir = os.path.join(output_dir, bag_name + '_pcd_depthcamera' + '/pcd')
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
- with rosbag.Bag(input_dir, 'r') as bag:
- num_count = 0
- for topic, msg, t in bag.read_messages():
- if topic == "/camera/depth/points":
- num_count += 1
- bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
- hz = str(float(num_count / bagtime))
- print(hz)
-
- command = ['rosrun', 'pcl_ros', 'bag_to_pcd'] + [input_dir] + ['/camera/depth/points'] + [output_dir]
- command = ' '.join(command)
- os.system(command)
- file1 = os.path.join(output_dir[0:-4], 'pcd_ascii')
- file2 = os.path.join(output_dir[0:-4], 'jpg')
- if not os.path.exists(file1):
- os.makedirs(file1)
- if not os.path.exists(file2):
- os.makedirs(file2)
-
- for root, dirs, files in os.walk(output_dir[0:-4]):
-
- for file in files:
- if "pcd" == root[-3:] and ".pcd" == file[-4:]:
- inputFilePath = root + "/" + file
- tempFilePath = root[:-3] + "pcd_ascii/" + file
- outputFilePath = root[:-3] + "jpg/" + file[:-3] + "jpg"
-
- command = "pcl_convert_pcd_ascii_binary " + \
- inputFilePath + \
- " " + \
- tempFilePath + \
- " 0"
-
- os.system(command)
-
- if not os.path.exists(tempFilePath):
- continue
-
-
-
-
-
-
- pic = np.zeros([800, 1600, 3])
- f = open(tempFilePath, "r")
- ff = f.readlines()[11:]
- for i in range(0, len(ff)):
- point = ff[i].split()[:4]
- point_x = float(point[0])
- point_y = float(point[1])
- point_z = float(point[2])
- points_intensity = float(point[2])
-
-
-
- x = -point_x
- y = -point_y
- if math.isnan(point_x):
- pass
- else:
- cv2.circle(pic, (int(800 - x * 60), int(400 - y * 60)), 0, (255, 255, 255), 0)
- cv2.imwrite(outputFilePath, pic)
- else:
- break
-
- jpg_list = os.listdir(file2)
- if not jpg_list == []:
- command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + ['"jpg/*.jpg"'] + ['-tag:v',
- 'avc1',
- '-y'] + [
- 'pcd_depthcamera.mp4']
- result_string = " ".join(command)
- p = Popen(result_string, shell=True, cwd=output_dir[0:-4] + '/', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- p.wait()
- shutil.rmtree(file1)
- shutil.rmtree(file2)
- shutil.rmtree(output_dir)
- return output_dir[0:-4]
|