def GetDefaultSettingsFilename(self): ''' @brief Get default save settings filename @return filename ''' CheckDir(self._static_settings['settings_params_folder']) filename = self._static_settings[ 'settings_params_folder'] + self._static_settings[ 'settings_params_fname'] return filename
def CreateFolders(self): ''' @brief Create folders before creating camera link or video recorders. ''' # Create output_folder if self.__me_master: side_folder = 'left_camera/' else: side_folder = 'right_camera/' self.__output_folder = self.GetSettings('DATABASE', 'output_folder') + side_folder CheckDir(self.__output_folder)
def RecordProcessFrames(self): ''' @brief Record process frames ''' CheckDir(self.__output_folder) for key in self.__process_frames: if not (key in self.__frame_recorders_dict): self.__frame_recorders_dict[key] = self.CreateFrameRecorder( self.__fps, self.__output_folder, key, self.__store_frames_as_video, self.__store_frames_as_images) self.RecordFrame(self.__frame_recorders_dict[key], self.__process_frames[key])
def PrintSettingsInfo(self, configure_settings=False, ask_user_for_settings_parameters=False): ''' @brief Print settings information and configure settings if specified. @param configure_settings (Default=False) @param ask_user_for_settings_parameters (default=False) ''' settings_info = self.GetSettingsInfo() settings_changed = False if configure_settings: print "#------------------------------------------ CONFIGURE SETTINGS ------------------------------------------#" else: print "#----------------------------------------- SETTINGS INFORMATION -----------------------------------------#" for main_settings_key in settings_info: print "# Settings type: {0}".format(main_settings_key) for sub_settings_key in settings_info[main_settings_key]: print "# \t - Setting: {0} \t\t\t - {1}".format( sub_settings_key, settings_info[main_settings_key][sub_settings_key]) if configure_settings: if self.AskUserToConfigureSetting(main_settings_key, sub_settings_key, n_tabs=2): settings_changed = True print "#------------------------------------- STATIC SETTINGS INFORMATION --------------------------------------#" static_settings_info = self.GetStaticSettingsInfo() print "# Following settings are static and may only be configured in the SetttingsConfigured unit." for static_settings_key in static_settings_info: print "# \t - Static setting: {0} \t\t\t - {1}".format( static_settings_key, static_settings_info[static_settings_key]) print "#--------------------------------------------------------------------------------------------------------#" if configure_settings and settings_changed: CheckDir(self._static_settings['settings_params_folder']) settings_filename = self._static_settings[ 'settings_params_folder'] + self._static_settings[ 'settings_params_fname'] self.SaveSettingsToJson(settings_filename) if ask_user_for_settings_parameters: answer = raw_input( "# Would you like to see the settings parameters? [Y/n]: ") if answer.upper() == 'Y': self.PrintSettings()
def TestRecordFrames(self, input_image_fn): ''' @brief Test unit for RecordFrames @param input_image_fn ''' import time from src.bin.tools import CheckDir CheckDir(self.vid_rec_folder) from src.DroneVision.DroneVision_src.hardware.imageTools import GetImage from src.DroneVision.DroneVision_src.hardware.VideoLink import VideoLink video_recorder = self.RecordFrames.RecordFrames( self.vid_rec_fps, self.vid_rec_folder, self.video_rec_output_fname) while video_recorder.GetNumberOfRecordedFrames() < self.max_rec_frames: frame = GetImage(input_image_fn) #video_recorder.WriteFrameThread(frame) #time.sleep(0.5) video_recorder.WriteFrame(frame) print 'N recorded frames: ', video_recorder.GetNumberOfRecordedFrames( ) video_recorder.CloseRecording()
def StartRecording(self): ''' @brief Start video recording Creates a folder for the video, called by the same video filename, and creates a subfolder for storing each frame. ''' # Create frames folder if self.__store_frames_as_images: self.__frames_folder = self.__folder + self.__video_output_filename + '_frames/' CheckDir(self.__frames_folder) if self.__store_frames_as_video: # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'MJPG') self.video_out = cv2.VideoWriter( filename=self.__folder + self.__video_output_filename + '.avi', fourcc=fourcc, fps=self.__fps, frameSize=(self.__width, self.__height), isColor=True) self.__frame_i = 0
def test_CameraLink(self): ''' @brief Test Ptg camera link by receiving frames, and saving them. ''' if not(self.CheckAllTests()): # Trigger test covers the same manually return import timeit from src.DroneVision.DroneVision_src.hardware.imageTools import WriteImage, GetImage from src.bin.tools import GetTimestamp, CheckDir ##### SETUP FOLDERS ###### settings_inst = self.Settings.Settings() settings_inst.ChangeSetting('CAMERA', 'manual_triggering', False) test_frame_folder = settings_inst.GetSettings('DATABASE', 'output_folder') + '/TEST/' + GetTimestamp() + '/' test_frame_folder += 'left_test_camera/' CheckDir(test_frame_folder) ########################## cameralink = self.CameraLink.CameraLink(True, settings_inst.GetSettings('CAMERA'), settings_inst.GetSettings('LASER')) start_time = timeit.default_timer() raise_error = False i = 0 try: while timeit.default_timer() - start_time < 5: frame, sl_frame = cameralink.GetFrame() print frame.shape fps, width, height = cameralink.GetFrameProperties() total_frames = cameralink.GetTotalFrames() frame_n = cameralink.GetFrameNumber() print 'GET: ', fps, width, height, total_frames, frame_n WriteImage(frame, test_frame_folder + 'frame_' + str(frame_n)) frame = GetImage(test_frame_folder + 'frame_' + str(frame_n) + '.tif') i += 1 except: raise_error = True cameralink.StopCamera() if raise_error: raise
def MatplotShow(touple_frames, main_title='', fig_number=0, interactive_mode=False, plt=None, savefig_folder='matplot_figs/', default_n_cols=2, save_fig=False, save_fig_only=False, inlude_main_title_in_plot=True): ''' @brief Show multiple grayscale frames in a single window. @param touple_frames Touple of frames - example: mult_frames = [('img_name1', frame1), ('img_name2', frame2)] @param main_title (default='') @param fig_number (>= 0, to handle more than one figure (default=0)) @param interactive_mode (Flag to handle interactive mode (True/False - default=False))) @param plt (pyplot object from matplotlib (default=None)) @param savefig_folder (Folder path for saving figures when display isn't available.) @param default_n_cols (default number of columns (default=2)) @param save_fig (Save figure (default=False)) @param save_fig_only (Save figure, and do not show plot (default=False)) @param inlude_main_title_in_plot (Include main title in plot. Also used as a filename for saving.) ''' if plt == None: import matplotlib.pyplot as plt n_frames = len(touple_frames) cols = default_n_cols if n_frames == 0: return elif n_frames < cols: cols = n_frames rows = int(math.ceil(float(n_frames) / cols)) fig = plt.figure(fig_number) i = 1 for img_name, frame in touple_frames: ax = fig.add_subplot(rows, cols, i) if len(frame.shape) == 2: plt.imshow(frame, cmap='gray') else: plt.imshow(frame) ax.set_title(img_name) i += 1 if inlude_main_title_in_plot: fig.suptitle(main_title) fig.tight_layout() if not (CheckDisplayAvailable()) or save_fig or save_fig_only: CheckDir(savefig_folder) main_title = os.path.basename( main_title ) # Does not change a normal title, just removes all paths fig.savefig(savefig_folder + main_title + '.png') if not (save_fig_only): if not (interactive_mode): if CheckDisplayAvailable(): print 'Press Esc on figure plot to continue..' try: plt.show() except Exception, err: warnings.simplefilter('always') warnings.warn(str(err), Warning) warnings.simplefilter('default') else: try: plt.pause(0.000001) except Exception, err: warnings.simplefilter('always') warnings.warn(str(err), Warning) warnings.simplefilter('default')
def test_TriggerCamera(self): ''' @brief Test manual camera triggering ''' ''' @brief Test Ptg camera link by receiving frames, and saving them. ''' if self.CheckAllTests(): #Cannot test while auto testing return import sys from getpass import getpass from src.DroneVision.DroneVision_src.hardware.imageTools import WriteImage, GetImage, MatplotShow from src.DroneVision.DroneVision_src.imgProcessing.featureDetection.PointDetection import PointDetection from src.DroneVision.DroneVision_src.imgProcessing.frameTools.frameTools import CheckGrayScale, FilterByColor from src.bin.tools import GetTimestamp, CheckDir from src.bin.UserInput.UserInput import UserInput this_master = True if self.slave_key in sys.argv: this_master = False ##### SETUP FOLDERS ###### settings_inst = self.Settings.Settings() settings_inst.ChangeSetting('CAMERA', 'manual_triggering', self.manual_triggering) settings_inst.ChangeSetting('CAMERA', 'ptg_recv_frame_timeout', self.camera_capture_timeout) test_frame_folder = settings_inst.GetSettings('DATABASE', 'output_folder') + '/TEST_TRIGGER/' + GetTimestamp() + '/' if this_master: test_frame_folder += 'left_test_camera/' else: test_frame_folder += 'right_test_camera/' CheckDir(test_frame_folder) ########################## pointDet = PointDetection.PointDetection(True, settings_inst.GetSettings()) pointDet.CalibratePointDetection() cameralink = self.CameraLink.CameraLink(this_master, settings_inst.GetSettings('CAMERA'), settings_inst.GetSettings('LASER')) if this_master: s_title = 'Master Frames' else: s_title = 'Slave Frames' settings_inst.ChangeSetting('USER_INPUT', 'automatic_mode', self.manual_triggering) getpass('Press enter to start - hold q to quit..') userInput = UserInput(settings_inst.GetSettings('USER_INPUT')) print 'START CAMERA TRIGGER TEST' i = 0 raise_error = False try: while (not(userInput.CheckTerminated())): title = s_title + ' {0}'.format(i) print 'Reading frame..' frame, sl_frame = cameralink.GetFrame() print 'FRAME SHAPE: ', frame.shape #green_mask, g_frame, g_sl_frame = pointDet.ComputeGreenMask(frame, sl_frame) green_mask, keypoints, descriptors, frame, sl_frame = pointDet.GetPointList(frame, sl_frame, draw=True, ignore_no_blobs_error=True) MatplotShow([('green_mask ({0})'.format(green_mask.shape), green_mask), ('frame ({0})'.format(frame.shape), frame), ('sl_frame ({0})'.format(sl_frame.shape), sl_frame)], title, save_fig=self.save_figs, save_fig_only=self.save_figs_only) #un_frame = pointDet.Undistort(frame) #un_sl_frame = pointDet.Undistort(sl_frame) #print 'Captured frame with shape {0}'.format(frame.shape) #MatplotShow([('Frame ({0})'.format(frame.shape), frame), ('UN FRAME ({0})'.format(un_frame.shape), un_frame), ('SL FRAME ({0})'.format(sl_frame.shape), sl_frame), ('UN SL FRAME ({0})'.format(un_sl_frame.shape), un_sl_frame)], title, save_fig=self.save_figs, save_fig_only=self.save_figs_only) #WriteImage(frame, test_frame_folder + 'frame_' + str(i)) #WriteImage(sl_frame, test_frame_folder + 'frame_sl_' + str(i)) i += 1 except: print 'RAISED ERROR' raise_error = True userInput.ForceTermination() cameralink.StopCamera() if raise_error: raise