forked from tier4/YabLoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish_video_file.py
executable file
·51 lines (40 loc) · 1.28 KB
/
publish_video_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
'''
This node reads a video file and publishes it as an image topic.
e.g. publish_video_file.py video.mp4
'''
from sensor_msgs.msg import Image
from rclpy.node import Node
import rclpy
import sys
import cv2
from cv_bridge import CvBridge
class Mp4Publisher(Node):
def __init__(self, mp4_file):
super().__init__('mp4_publisher')
self.publisher_ = self.create_publisher(
Image, '/sensing/camera/undistorted/image_raw', 10)
self.timer_ = self.create_timer(0.1, self.timer_callback)
self.video_ = cv2.VideoCapture(mp4_file)
self.bridge_ = CvBridge()
def timer_callback(self):
ret, frame = self.video_.read()
if not ret:
self.video_.set(cv2.CAP_PROP_POS_FRAMES, 0)
return
msg = self.bridge_.cv2_to_imgmsg(frame)
msg.header.stamp = self.get_clock().now().to_msg()
msg.header.frame_id = "/map"
msg._encoding = 'bgr8'
self.publisher_.publish(msg)
self.get_logger().info('Publishing')
def main(args=None):
if len(sys.argv) == 1:
return
rclpy.init(args=args)
mp4_publisher = Mp4Publisher(sys.argv[1])
rclpy.spin(mp4_publisher)
mp4_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()