camera_tools.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 
33 
34 from os import mkdir, path
35 import sys
36 import datetime
37 import cv2
38 
39 import rospy
40 from std_msgs.msg import Header
41 
42 import timeit
43 from test_cam import testCam
44 
45 class Camera(object):
46  """Class defining a camera"""
47  def __init__(self, source, save_images=False):
48  self.original_image_size = None
49  self.show_size_ratio = None
50  self.image_size_to_show = None
51  self.camera_type = None
52  self.video_source = None
53  self.frame_counter = None
54  self.a = testCam()
55  self.count = 0
56  self.delay = 0
57  # Setup ROS and Camera
58  if "." not in source:
59  self.setup_ros_cam(source)
60  else:
61  self.setup_cv2_cam(source)
62 
63  if save_images:
64  self.setup_image_saving()
65 
66  def __del__(self):
67  if "cv2" in self.camera_type and self.video_source.isOpened():
68  self.video_source.release()
69 
70  def setup_ros_cam(self, source):
71  """Set up a ROS camera
72  Input:
73  source (str) The camera to setup
74  Output: None"""
75  try:
76  from image_subscriber import ImageSubscriber
77  except ImportError:
78  # You forgot to initialize submodules
79  rospy.loginfo("Could not import the submodules.")
80 
81  import traceback
82  ex_type, ex, tb = sys.exc_info()
83  traceback.print_tb(tb)
84 
85  exit(1)
86 
87  self.camera_type = "ROS"
88  self.video_source = ImageSubscriber(topic=source)
90 
91  def setup_cv2_cam(self, source):
92  """Set up a cv2 camera
93  Input:
94  source (str) The camera to setup
95  Output: None"""
96  try:
97  warning_prefix = '\n\t__Warning__: \t Video '
98  self.camera_type = "cv2cam"
99  source = int(source)
100  if not path.exists('/dev/video%d' % source):
101  rospy.loginfo(warning_prefix + 'stream /dev/video%d is absent\n' %
102  source)
103 
104  except ValueError:
105  self.camera_type = "cv2vid"
106  if not path.exists(source):
107  rospy.loginfo(warning_prefix +
108  'file %s is not found\n' % source)
109  raise Exception("Input Video " + source + " is absent")
110 
111  self.video_source = cv2.VideoCapture(source)
112 
113  self.check_cv2_camera(source)
114 
115  self.frame_counter = 0
116  self.frame_count = self.find_frame_counter(source)
117  self.set_image_parameters(self.get_image()[0])
118 
119  def check_cv2_camera(self, source):
120  """Verify that a cv2 camera is opened
121  Input:
122  source (str) The camera to setup
123  Output: None"""
124  # Check if video source is open
125  if not self.video_source.isOpened():
126  raise Exception("Can not open video " + source)
127 
128  def find_frame_counter(self, source):
129  """Find the frame count of a video
130  Input:
131  source (str) The cv2 source
132  Output: (int or None)
133  The number of frames"""
134  if isinstance(source, str):
135  rospy.logdebug('Video %s opened with %d frames' %
136  (source, self.video_source.get(cv2.CAP_PROP_FRAME_COUNT)))
137  return int(self.video_source.get(cv2.CAP_PROP_FRAME_COUNT))
138  return None
139 
141  """Change the resolution of a cv2 device to match the input size
142  Input: None
143  Output: None"""
144  # Try to change camera resolution to 800x600
145  self.video_source.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
146  self.video_source.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
147  # h_video_in.set(cv2.CAP_PROP_FOCUS,10)
148  self.original_image_size = (
149  self.video_source.get(cv2.CAP_PROP_FRAME_WIDTH),
150  self.video_source.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float
151  rospy.logdebug('Try to change resolution. Get:' +
152  str(self.original_image_size))
153 
154  # def delay_calc(self, start_time):
155  # self.count += 1
156  # self.delay += float((timeit.default_timer() - start_time))
157  # if self.count % 100 == 0 and (self.camera_type == "ROS" and self.video_source.currentImage is not None):
158  # rospy.logwarn( "Delay "+ str(self.count) +":\t" + str(float(self.delay / self.count)))
159 
160 
161  def get_image(self):
162  """Get a single image
163  Input: None
164  Output: cv2 image in RGB"""
165  start_time = timeit.default_timer()
166 
167  if self.camera_type == 'ROS':
168  retn = self.video_source.currentImage, self.video_source.currentHeader
169  # self.delay_calc(start_time)
170  return retn
171 
172  ret, frame = self.video_source.read()
173 
174  header = Header()
175  header.seq = self.frame_counter
176  header.stamp = rospy.Time.now()
177  header.frame_id = self.camera_type
178 
179  # self.delay_calc(start_time)
180  if ret:
181  self.frame_counter += 1
182  return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), header
183  else:
184  return None, None
185 
187  """Wait for the first frame of a ROS camera.
188  Due to issues with the initialization sometimes it takes
189  several calls to get the first image
190 
191  Input: None
192  Output: cv2 image in BGR"""
193  first_image = self.get_image()[0]
194  while first_image is None and not rospy.is_shutdown():
195  first_image = self.get_image()[0]
196  return first_image
197 
198  def set_image_parameters(self, image):
199  """Set the size parameters of the image
200  Input:
201  image (cv2 Image) The image to take the sizes from
202  Output: None"""
203  self.original_image_size = (image.shape[1],
204  image.shape[0]) # float
205  self.show_size_ratio = 1000.0 / self.original_image_size[0]
206  print(self.show_size_ratio)
207  self.image_size_to_show = (int(self.original_image_size[0] * self.show_size_ratio),
208  int(self.original_image_size[1] * self.show_size_ratio))
209 
211  """Create name for output video if source is a camera"""
212  #Create default name
213  if self.camera_type is not 'cv2vid':
214  return 'Camera_' + self.camera_type + '__' + \
215  datetime.datetime.now().strftime("%Y-%m-%d__%H:%M:%S")
216  else:
217  return ''
218 
220  """Set up the ability to save all the capture images
221  Input: None
222  Output: None"""
223  base_dir_name = self.camera_type + datetime.datetime.now().strftime("%Y-%m-%d__%H:%M:%S")
224 
225  if path.exists(base_dir_name + '_org') or path.exists(base_dir_name + '_grn'):
226  add_ind = 0
227  dir_name = base_dir_name
228  while path.exists(dir_name + '_org') or path.exists(dir_name + '_grn'):
229  add_ind += 1
230  dir_name = base_dir_name + \
231  '_%d' % add_ind
232  else:
233  dir_name = base_dir_name
234 
235  mkdir(dir_name + '_org')
236  mkdir(dir_name + '_grn')
237  rospy.loginfo('Folders for images ' + dir_name + '_org' +
238  ' and ' + dir_name + '_grn' + ' were created')
def __init__(self, source, save_images=False)
Definition: camera_tools.py:47
def setup_ros_cam(self, source)
Definition: camera_tools.py:70
def setup_cv2_cam(self, source)
Definition: camera_tools.py:91
def set_image_parameters(self, image)
def check_cv2_camera(self, source)
def find_frame_counter(self, source)
def get_from_camera_video_name(self)


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