save_run.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # BSD 3-Clause License
3 
4 # Copyright (c) 2019, Noam C. Golombek
5 # All rights reserved.
6 
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 
10 # 1. Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 from __future__ import print_function
33 
34 import logging
35 import datetime
36 import cv2
37 import rospy
38 
39 class VideoSaver(object):
40  """A class to save images into a video"""
41  def __init__(self, image, video_name=""):
42  self.h_video_out = None
43  self.fourcc = cv2.VideoWriter_fourcc(*'mp4v')
44  if video_name is "" or video_name is True:
45  self.video_base_name = 'Camera__' + \
46  datetime.datetime.now().strftime("%Y-%m-%d__%H:%M:%S")
47  else:
48  self.video_base_name = video_name
49  (height, width, _) = image.shape
50  self.video_sz = (width, height)
51  self.open_out_video()
52  # self.print_info()
53 
54  def __del__(self):
55  self.close_video()
56 
57  def close_video(self):
58  """ Close video if opened
59  Since, we need it also in reopen module put in in separate function
60  Input:
61  None
62  Output:
63  None
64  """
65  if self.h_video_out is not None and self.h_video_out.isOpened():
66  # print('Close ' + self.video_base_name + ' video')
67  self.h_video_out.release()
68  self.h_video_out = None
69 
70  def open_out_video(self, count=None):
71  """Open a video out
72  Input:
73  count (int) The frame number
74  Output: None"""
75 
76  self.close_video()
77 
78  if count is not None:
79  video_name = self.video_base_name + '._%d_' % count + '.mp4'
80  else:
81  video_name = self.video_base_name + '.mp4'
82 
83  self.h_video_out = cv2.VideoWriter(
84  video_name, self.fourcc, 10.0, self.video_sz)
85 
86  if self.h_video_out.isOpened():
87  rospy.loginfo("Open video stream {0} of size ({1}, {2})".format(video_name, self.video_sz[0],self.video_sz[1]))
88  else:
89  rospy.logfatal("Failed to open video {0}".format(video_name))
90 
91  def print_info(self):
92  """Print info for debug """
93 
94  if self.h_video_out is not None:
95  rospy.logdebug('Video_base_name: {0} is opened: {1}'.\
96  format( self.video_base_name, self.h_video_out.isOpened()))
97 
def __init__(self, image, video_name="")
Definition: save_run.py:41
def open_out_video(self, count=None)
Definition: save_run.py:70


cnn_bridge
Author(s): Noam C. Golombek , Alexander Beringolts
autogenerated on Mon Jun 10 2019 12:53:26