|
@@ -12,15 +12,20 @@ import argparse
|
|
import struct
|
|
import struct
|
|
import subprocess
|
|
import subprocess
|
|
from subprocess import Popen, PIPE
|
|
from subprocess import Popen, PIPE
|
|
-
|
|
|
|
|
|
+import numpy as np
|
|
|
|
+import shutil
|
|
bridge = CvBridge()
|
|
bridge = CvBridge()
|
|
-
|
|
|
|
|
|
+bag_start_time=0
|
|
|
|
+bag_end_time=0
|
|
|
|
|
|
def parsebag(f, output_dir, target_topic):
|
|
def parsebag(f, output_dir, target_topic):
|
|
rosbag_name = f[f.rindex("/"):-4]
|
|
rosbag_name = f[f.rindex("/"):-4]
|
|
output_path = output_dir + rosbag_name
|
|
output_path = output_dir + rosbag_name
|
|
|
|
|
|
- with rosbag.Bag(f, 'r') as bag:
|
|
|
|
|
|
+ with rosbag.Bag(f, 'r') as bag:
|
|
|
|
+ global bag_start_time
|
|
|
|
+ global bag_end_time
|
|
|
|
+
|
|
count = 0
|
|
count = 0
|
|
for key, val in bag.get_type_and_topic_info()[1].items():
|
|
for key, val in bag.get_type_and_topic_info()[1].items():
|
|
if val[0] == 'sensor_msgs/Image':
|
|
if val[0] == 'sensor_msgs/Image':
|
|
@@ -30,7 +35,7 @@ def parsebag(f, output_dir, target_topic):
|
|
img = bridge.imgmsg_to_cv2(msg, 'bgr8')
|
|
img = bridge.imgmsg_to_cv2(msg, 'bgr8')
|
|
# img = bridge.compressed_imgmsg_to_cv2(msg, 'bgr8')
|
|
# img = bridge.compressed_imgmsg_to_cv2(msg, 'bgr8')
|
|
timestr = msg.header.stamp.to_nsec()
|
|
timestr = msg.header.stamp.to_nsec()
|
|
- image_name = str(timestr) + ".jpg"
|
|
|
|
|
|
+ image_name = str(timestr)[:10]+'.'+str(timestr)[10:] + ".jpg"
|
|
if not os.path.exists(output_path + '_' + 'image'):
|
|
if not os.path.exists(output_path + '_' + 'image'):
|
|
os.makedirs(output_path + '_' + 'image')
|
|
os.makedirs(output_path + '_' + 'image')
|
|
output_path_img = os.path.join(output_path + '_' + 'image', image_name)
|
|
output_path_img = os.path.join(output_path + '_' + 'image', image_name)
|
|
@@ -41,43 +46,145 @@ def parsebag(f, output_dir, target_topic):
|
|
|
|
|
|
|
|
|
|
def parse1(topic, input_bag_file, output_mp4_dir):
|
|
def parse1(topic, input_bag_file, output_mp4_dir):
|
|
|
|
+ global bag_start_time
|
|
|
|
+ global bag_end_time
|
|
input_dir = input_bag_file
|
|
input_dir = input_bag_file
|
|
output_dir = output_mp4_dir
|
|
output_dir = output_mp4_dir
|
|
num_count = parsebag(input_dir, output_dir, topic)
|
|
num_count = parsebag(input_dir, output_dir, topic)
|
|
- print("解析完成")
|
|
|
|
|
|
+ #print("解析完成")
|
|
dirt = os.path.join(output_dir, input_dir.split('/')[-1].split('.')[0] + '_' + 'image')
|
|
dirt = os.path.join(output_dir, input_dir.split('/')[-1].split('.')[0] + '_' + 'image')
|
|
bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
|
|
bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
|
|
- hz = str(float(num_count / bagtime))
|
|
|
|
- print(hz)
|
|
|
|
- command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + [dirt + '/*.jpg'] + ['-tag:v',
|
|
|
|
- 'avc1',
|
|
|
|
- '-y'] + [
|
|
|
|
- dirt + '/camera.mp4']
|
|
|
|
- p = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
- p.communicate()
|
|
|
|
|
|
+ #hz = str(float(num_count / bagtime))
|
|
|
|
+ #print(hz)
|
|
list1 = os.listdir(dirt)
|
|
list1 = os.listdir(dirt)
|
|
- for i in list1:
|
|
|
|
- if i.split('.')[-1] == 'jpg':
|
|
|
|
- os.remove(os.path.join(dirt, i))
|
|
|
|
|
|
+ if list1!=[]:
|
|
|
|
+ #指定视频帧率
|
|
|
|
+ hz=10.0
|
|
|
|
+
|
|
|
|
+ # 定义图片文件夹路径
|
|
|
|
+ folder_path = dirt
|
|
|
|
+
|
|
|
|
+ #根据rosbag的起止时间生成黑色图片
|
|
|
|
+ if bag_end_time!=0 and bag_end_time!=0:
|
|
|
|
+ black_image = np.zeros((512, 512, 3), dtype=np.uint8)
|
|
|
|
+ cv2.imwrite(os.path.join(dirt,str(bag_start_time)+'.jpg'), black_image)
|
|
|
|
+ cv2.imwrite(os.path.join(dirt,str(bag_end_time)+'.jpg'), black_image)
|
|
|
|
+
|
|
|
|
+ # 获取文件夹中的所有图片文件
|
|
|
|
+ image_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
|
|
|
|
+
|
|
|
|
+ # 对文件名进行排序,以便按顺序处理图片
|
|
|
|
+ image_files.sort()
|
|
|
|
+
|
|
|
|
+ # 读取第一张图片
|
|
|
|
+ first_image = cv2.imread(os.path.join(folder_path, image_files[0]))
|
|
|
|
+
|
|
|
|
+ # 获取图片的形状
|
|
|
|
+ image_shape = first_image.shape
|
|
|
|
+
|
|
|
|
+ # 创建视频写入对象
|
|
|
|
+ video_path=os.path.join(dirt,'camera1.mp4')
|
|
|
|
+ video_writer = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), hz, (image_shape[1], image_shape[0]))
|
|
|
|
+
|
|
|
|
+ # 遍历图片文件列表
|
|
|
|
+ for i in range(len(image_files) - 1):
|
|
|
|
+ # 读取当前图片和下一张图片
|
|
|
|
+ current_image = cv2.imread(os.path.join(folder_path, image_files[i]))
|
|
|
|
+ next_image = cv2.imread(os.path.join(folder_path, image_files[i + 1]))
|
|
|
|
+
|
|
|
|
+ # 获取当前图片和下一张图片的时间戳
|
|
|
|
+ current_timestamp = float(image_files[i].split('.jpg')[0])
|
|
|
|
+ next_timestamp = float(image_files[i + 1].split('.jpg')[0])
|
|
|
|
+
|
|
|
|
+ # 计算时间间隔
|
|
|
|
+ time_interval = next_timestamp - current_timestamp
|
|
|
|
+ #print(time_interval)
|
|
|
|
+
|
|
|
|
+ # 根据实际时间间隔调整循环次数
|
|
|
|
+ video_writer.write(current_image)
|
|
|
|
+ if int(time_interval * hz)>1.5:
|
|
|
|
+ for _ in range(int(time_interval * hz)):
|
|
|
|
+ video_writer.write(current_image)
|
|
|
|
+
|
|
|
|
+ # 释放资源
|
|
|
|
+ video_writer.release()
|
|
|
|
+ if os.path.exists(video_path):
|
|
|
|
+ video_output_path=os.path.join(output_dir,'camera1.mp4')
|
|
|
|
+ command = ["ffmpeg", "-i", video_path, "-c:v", "libx264", video_output_path]
|
|
|
|
+ p = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
+ p.communicate()
|
|
|
|
+ shutil.rmtree(folder_path)
|
|
|
|
+
|
|
return dirt
|
|
return dirt
|
|
|
|
|
|
def parse2(topic, input_bag_file, output_mp4_dir):
|
|
def parse2(topic, input_bag_file, output_mp4_dir):
|
|
|
|
+ global bag_start_time
|
|
|
|
+ global bag_end_time
|
|
input_dir = input_bag_file
|
|
input_dir = input_bag_file
|
|
output_dir = output_mp4_dir
|
|
output_dir = output_mp4_dir
|
|
num_count = parsebag(input_dir, output_dir, topic)
|
|
num_count = parsebag(input_dir, output_dir, topic)
|
|
- print("解析完成")
|
|
|
|
|
|
+ #print("解析完成")
|
|
dirt = os.path.join(output_dir, input_dir.split('/')[-1].split('.')[0] + '_' + 'image')
|
|
dirt = os.path.join(output_dir, input_dir.split('/')[-1].split('.')[0] + '_' + 'image')
|
|
bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
|
|
bagtime = int(input_dir.split('/')[-1].split('.')[0].split('_')[-1])
|
|
- hz = str(float(num_count / bagtime))
|
|
|
|
- print(hz)
|
|
|
|
- command = ['ffmpeg', '-f', 'image2', '-r', hz, '-pattern_type', 'glob', '-i'] + [dirt + '/*.jpg'] + ['-tag:v',
|
|
|
|
- 'avc1',
|
|
|
|
- '-y'] + [
|
|
|
|
- dirt + '/camera2.mp4']
|
|
|
|
- p = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
- p.communicate()
|
|
|
|
|
|
+ #hz = str(float(num_count / bagtime))
|
|
|
|
+ #print(hz)
|
|
list1 = os.listdir(dirt)
|
|
list1 = os.listdir(dirt)
|
|
- for i in list1:
|
|
|
|
- if i.split('.')[-1] == 'jpg':
|
|
|
|
- os.remove(os.path.join(dirt, i))
|
|
|
|
|
|
+ if list1!=[]:
|
|
|
|
+ #指定视频帧率
|
|
|
|
+ hz=10.0
|
|
|
|
+
|
|
|
|
+ # 定义图片文件夹路径
|
|
|
|
+ folder_path = dirt
|
|
|
|
+
|
|
|
|
+ #根据rosbag的起止时间生成黑色图片
|
|
|
|
+ if bag_end_time!=0 and bag_end_time!=0:
|
|
|
|
+ black_image = np.zeros((512, 512, 3), dtype=np.uint8)
|
|
|
|
+ cv2.imwrite(os.path.join(dirt,str(bag_start_time)+'.jpg'), black_image)
|
|
|
|
+ cv2.imwrite(os.path.join(dirt,str(bag_end_time)+'.jpg'), black_image)
|
|
|
|
+
|
|
|
|
+ # 获取文件夹中的所有图片文件
|
|
|
|
+ image_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
|
|
|
|
+
|
|
|
|
+ # 对文件名进行排序,以便按顺序处理图片
|
|
|
|
+ image_files.sort()
|
|
|
|
+
|
|
|
|
+ # 读取第一张图片
|
|
|
|
+ first_image = cv2.imread(os.path.join(folder_path, image_files[0]))
|
|
|
|
+
|
|
|
|
+ # 获取图片的形状
|
|
|
|
+ image_shape = first_image.shape
|
|
|
|
+
|
|
|
|
+ # 创建视频写入对象
|
|
|
|
+ video_path=os.path.join(dirt,'camera2.mp4')
|
|
|
|
+ video_writer = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), hz, (image_shape[1], image_shape[0]))
|
|
|
|
+
|
|
|
|
+ # 遍历图片文件列表
|
|
|
|
+ for i in range(len(image_files) - 1):
|
|
|
|
+ # 读取当前图片和下一张图片
|
|
|
|
+ current_image = cv2.imread(os.path.join(folder_path, image_files[i]))
|
|
|
|
+ next_image = cv2.imread(os.path.join(folder_path, image_files[i + 1]))
|
|
|
|
+
|
|
|
|
+ # 获取当前图片和下一张图片的时间戳
|
|
|
|
+ current_timestamp = float(image_files[i].split('.jpg')[0])
|
|
|
|
+ next_timestamp = float(image_files[i + 1].split('.jpg')[0])
|
|
|
|
+
|
|
|
|
+ # 计算时间间隔
|
|
|
|
+ time_interval = next_timestamp - current_timestamp
|
|
|
|
+ #print(time_interval)
|
|
|
|
+
|
|
|
|
+ # 根据实际时间间隔调整循环次数
|
|
|
|
+ video_writer.write(current_image)
|
|
|
|
+ if int(time_interval * hz)>1.5:
|
|
|
|
+ for _ in range(int(time_interval * hz)):
|
|
|
|
+ video_writer.write(current_image)
|
|
|
|
+
|
|
|
|
+ # 释放资源
|
|
|
|
+ video_writer.release()
|
|
|
|
+ if os.path.exists(video_path):
|
|
|
|
+ video_output_path=os.path.join(output_dir,'camera2.mp4')
|
|
|
|
+ command = ["ffmpeg", "-i", video_path, "-c:v", "libx264", video_output_path]
|
|
|
|
+ p = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
+ p.communicate()
|
|
|
|
+ shutil.rmtree(folder_path)
|
|
|
|
+
|
|
return dirt
|
|
return dirt
|