如何将视频或图像序列转换为文件包? [英] How do I convert a video or a sequence of images to a bag file?

查看:268
本文介绍了如何将视频或图像序列转换为文件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ROS的新手.我需要将预先存在的视频文件或可以连接为视频流的大量图片数量转换为ROS中的.bag文件.我在网上找到了以下代码: http://answers.ros.org/question/11537/creating-a-bag-file-out-of-a-image-sequence/,但是它说这是用于相机校准的,所以不确定如果适合我的目的.

I am new to ROS. I need to convert a preexisting video file, or a large amount of images that can be concatenated into a video stream, into a .bag file in ROS. I found this code online: http://answers.ros.org/question/11537/creating-a-bag-file-out-of-a-image-sequence/, but it says it is for camera calibration, so not sure if it fits my purpose.

精通ROS的人可以确认我可以使用提供的链接中的代码来实现我的目的吗,或者如果有人真正拥有我所需要的代码,可以请您在此处发布它吗?

Could someone with a good knowledge of ROS confirm that I can use the code in the link provided for my purposes, or if anyone actually has the code I'm looking for, could you please post it here?

推荐答案

以下代码受提供的链接中的代码启发,将视频文件转换为文件包文件.

The following code converts a video file to a bag file, inspired from the code in the link provided.

小提醒:

1)此代码取决于cv2(opencv python)

1) this code depends on cv2 (opencv python)

2)ROS消息的时间戳是通过帧索引和fps计算的.如果opencv无法从视频中读取,则fps将设置为24.

2) time stamp of ROS message is calculated by frame index and fps. fps will be set to 24 if opencv unable to read it from the video.

import time, sys, os
from ros import rosbag
import roslib, rospy
roslib.load_manifest('sensor_msgs')
from sensor_msgs.msg import Image

from cv_bridge import CvBridge
import cv2

TOPIC = 'camera/image_raw'

def CreateVideoBag(videopath, bagname):
    '''Creates a bag file with a video file'''
    bag = rosbag.Bag(bagname, 'w')
    cap = cv2.VideoCapture(videopath)
    cb = CvBridge()
    prop_fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
    if prop_fps != prop_fps or prop_fps <= 1e-2:
        print "Warning: can't get FPS. Assuming 24."
        prop_fps = 24
    ret = True
    frame_id = 0
    while(ret):
        ret, frame = cap.read()
        if not ret:
            break
        stamp = rospy.rostime.Time.from_sec(float(frame_id) / prop_fps)
        frame_id += 1
        image = cb.cv2_to_imgmsg(frame, encoding='bgr8')
        image.header.stamp = stamp
        image.header.frame_id = "camera"
        bag.write(TOPIC, image, stamp)
    cap.release()
    bag.close()

if __name__ == "__main__":
    if len( sys.argv ) == 3:
        CreateVideoBag(*sys.argv[1:])
    else:
        print( "Usage: video2bag videofilename bagfilename")

这篇关于如何将视频或图像序列转换为文件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆