34 from os
import mkdir, path
40 from std_msgs.msg
import Header
43 from test_cam
import testCam
46 """Class defining a camera""" 67 if "cv2" in self.
camera_type and self.video_source.isOpened():
68 self.video_source.release()
71 """Set up a ROS camera 73 source (str) The camera to setup 76 from image_subscriber
import ImageSubscriber
79 rospy.loginfo(
"Could not import the submodules.")
82 ex_type, ex, tb = sys.exc_info()
83 traceback.print_tb(tb)
92 """Set up a cv2 camera 94 source (str) The camera to setup 97 warning_prefix =
'\n\t__Warning__: \t Video ' 100 if not path.exists(
'/dev/video%d' % source):
101 rospy.loginfo(warning_prefix +
'stream /dev/video%d is absent\n' %
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")
120 """Verify that a cv2 camera is opened 122 source (str) The camera to setup 125 if not self.video_source.isOpened():
126 raise Exception(
"Can not open video " + source)
129 """Find the frame count of a video 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))
141 """Change the resolution of a cv2 device to match the input size 145 self.video_source.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
146 self.video_source.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
149 self.video_source.get(cv2.CAP_PROP_FRAME_WIDTH),
150 self.video_source.get(cv2.CAP_PROP_FRAME_HEIGHT))
151 rospy.logdebug(
'Try to change resolution. Get:' +
162 """Get a single image 164 Output: cv2 image in RGB""" 165 start_time = timeit.default_timer()
168 retn = self.video_source.currentImage, self.video_source.currentHeader
172 ret, frame = self.video_source.read()
176 header.stamp = rospy.Time.now()
182 return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), header
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 192 Output: cv2 image in BGR""" 194 while first_image
is None and not rospy.is_shutdown():
199 """Set the size parameters of the image 201 image (cv2 Image) The image to take the sizes from 211 """Create name for output video if source is a camera""" 215 datetime.datetime.now().strftime(
"%Y-%m-%d__%H:%M:%S")
220 """Set up the ability to save all the capture images 223 base_dir_name = self.
camera_type + datetime.datetime.now().strftime(
"%Y-%m-%d__%H:%M:%S")
225 if path.exists(base_dir_name +
'_org')
or path.exists(base_dir_name +
'_grn'):
227 dir_name = base_dir_name
228 while path.exists(dir_name +
'_org')
or path.exists(dir_name +
'_grn'):
230 dir_name = base_dir_name + \
233 dir_name = base_dir_name
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')