def __init__(self, in_frame_queue, finished_evt: Event = None, dispatching_set_evt: Event = None, gui_framerate=30, gui_dispatcher=False, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_evt: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=600) # GUI queue # for displaying the image self.output_frame_queue = TimestampedArrayQueue(max_mbytes=600) self.dispatching_set_evt = dispatching_set_evt self.finished_signal = finished_evt self.gui_framerate = gui_framerate self.gui_dispatcher = gui_dispatcher self.i = 0
def __init__(self, in_frame_queue, finished_signal: Event = None, pipeline=None, processing_parameter_queue=None, output_queue=None, processing_counter: Value = None, gui_framerate=30, gui_dispatcher=False, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_signal: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=100) # GUI queue for # displaying # the image self.output_queue = output_queue # queue for processing output (e.g., pos) self.processing_parameter_queue = processing_parameter_queue self.processing_counter = processing_counter self.finished_signal = finished_signal self.gui_framerate = gui_framerate self.gui_dispatcher = gui_dispatcher self.pipeline_cls = pipeline self.pipeline = None self.i = 0
def __init__(self, in_frame_queue, finished_signal=None, processing_class=None, preprocessing_class=None, processing_parameter_queue=None, gui_framerate=30, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_signal: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(**kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue( ) # GUI queue for displaying the image self.output_queue = Queue() # queue for processing output (e.g., pos) self.processing_parameters = dict() self.finished_signal = finished_signal self.i = 0 self.gui_framerate = gui_framerate self.preprocessing_obj = get_preprocessing_method( preprocessing_class).process self.preprocessing_state = None self.processing_obj = get_tracking_method(processing_class).detect self.processing_parameter_queue = processing_parameter_queue
def __init__(self, rotation=False, max_mbytes_queue=100): """ """ super().__init__() self.rotation = rotation self.control_queue = Queue() self.frame_queue = TimestampedArrayQueue(max_mbytes=max_mbytes_queue) self.kill_event = Event()
def __init__(self, n_items=100, timestamped=False, indexed=False, n_mbytes=2, wait=0, test_full=False): super().__init__() self.source_array = IndexedArrayQueue(max_mbytes==n_mbytes) if indexed \ else (TimestampedArrayQueue(max_mbytes=n_mbytes) if timestamped \ else ArrayQueue(max_mbytes=n_mbytes)) self.n_items = n_items self.wait = wait self.test_full = test_full
class TrackingProcess(FrameProcess): """A class which handles taking frames from the camera and processing them, as well as dispatching a subset for display Parameters ---------- Returns ------- """ def __init__(self, in_frame_queue, finished_signal: Event = None, pipeline=None, processing_parameter_queue=None, output_queue=None, processing_counter: Value = None, gui_framerate=30, gui_dispatcher=False, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_signal: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=100) # GUI queue for # displaying # the image self.output_queue = output_queue # queue for processing output (e.g., pos) self.processing_parameter_queue = processing_parameter_queue self.processing_counter = processing_counter self.finished_signal = finished_signal self.gui_framerate = gui_framerate self.gui_dispatcher = gui_dispatcher self.pipeline_cls = pipeline self.pipeline = None self.i = 0 def process_internal(self, frame): """Apply processing function to current frame with self.processing_parameters as additional inputs. Parameters ---------- frame : frame to be processed; Returns ------- type processed output """ def retrieve_params(self): while True: try: param_dict = self.processing_parameter_queue.get( timeout=0.0001) self.pipeline.deserialize_params(param_dict) except Empty: break def run(self): """Loop where the tracking function runs.""" self.pipeline = self.pipeline_cls() self.pipeline.setup() while not self.finished_signal.is_set(): # Gets the processing parameters from their queue self.retrieve_params() # Gets frame from its queue, if the input is too fast, drop frames # and process the latest, if it is too slow continue: try: time, frame_idx, frame = self.frame_queue.get(timeout=0.001) except Empty: continue # If a processing function is specified, apply it: messages, output = self.pipeline.run(frame) for msg in messages: self.message_queue.put(msg) self.output_queue.put(time, output) # calculate the frame rate self.update_framerate() # put current frame into the GUI queue self.send_to_gui( time, self.pipeline.diagnostic_image if self.pipeline.diagnostic_image is not None else frame) return def send_to_gui(self, frametime, frame): """ Sends the current frame to the GUI queue at the appropriate framerate""" if self.framerate_rec.current_framerate: every_x = max( int(self.framerate_rec.current_framerate / self.gui_framerate), 1) else: every_x = 1 if self.i == 0: try: self.gui_queue.put(frame, timestamp=frametime) except Full: self.message_queue.put("E:GUI queue full") self.i = (self.i + 1) % every_x
class DispatchProcess(FrameProcess): """ A class which handles taking frames from the camera and dispatch them to both a separate process (e.g. for saving a movie) and to a gui for display Parameters ---------- Returns ------- """ def __init__(self, in_frame_queue, finished_evt: Event = None, dispatching_set_evt: Event = None, gui_framerate=30, gui_dispatcher=False, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_evt: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=600) # GUI queue # for displaying the image self.output_frame_queue = TimestampedArrayQueue(max_mbytes=600) self.dispatching_set_evt = dispatching_set_evt self.finished_signal = finished_evt self.gui_framerate = gui_framerate self.gui_dispatcher = gui_dispatcher self.i = 0 def run(self): """Loop where the tracking function runs.""" while not self.finished_signal.is_set(): # Gets frame from its queue, if the input is too fast, drop frames # and process the latest, if it is too slow continue: try: time, frame_idx, frame = self.frame_queue.get(timeout=0.001) except Empty: continue if self.dispatching_set_evt.is_set(): self.output_frame_queue.put(frame.copy(), time) # put current frame into the GUI queue self.send_to_gui(time, frame) # calculate the frame rate self.update_framerate() return def send_to_gui(self, frametime, frame): """ Sends the current frame to the GUI queue at the appropriate framerate""" if self.framerate_rec.current_framerate: every_x = max( int(self.framerate_rec.current_framerate / self.gui_framerate), 1) else: every_x = 1 if self.i == 0: self.gui_queue.put(frame, timestamp=frametime) self.i = (self.i + 1) % every_x
class TrackingProcess(FrameProcess): """A class which handles taking frames from the camera and processing them, as well as dispatching a subset for display Parameters ---------- Returns ------- """ def __init__( self, in_frame_queue, finished_signal: Event = None, pipeline=None, processing_parameter_queue=None, output_queue=None, second_output_queue=None, recording_signal=None, gui_framerate=30, max_mb_queue=100, **kwargs ): """ Frame dispatcher process Parameters ---------- in_frame_queue: queue dispatching frames from camera finished_signal signal for the end of the acquisition pipeline: Pipeline tracking pipeline processing_parameter_queue queue for function¶meters output_queue: tracking output queue recording_signal: bool (false) processing_counter gui_framerate: int target framerate of the display GUI gui_dispatcher max_mb_queue: int (200) the maximal size of the image output queues kwargs """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=max_mb_queue) # GUI queue for self.recording_signal = recording_signal if recording_signal is not None: self.frame_copy_queue = TimestampedArrayQueue(max_mbytes=max_mb_queue) else: self.frame_copy_queue = None # displaying # the image self.output_queue = output_queue # queue for processing output (e.g., pos) self.second_output_queue = second_output_queue # user defined optional second output queue, attention needs emptying self.processing_parameter_queue = processing_parameter_queue self.finished_signal = finished_signal self.gui_framerate = gui_framerate self.pipeline_cls = pipeline self.pipeline = None self.i = 0 def process_internal(self, frame): """Apply processing function to current frame with self.processing_parameters as additional inputs. Parameters ---------- frame : frame to be processed; Returns ------- type processed output """ def retrieve_params(self): while True: try: param_dict = self.processing_parameter_queue.get(timeout=0.0001) self.pipeline.deserialize_params(param_dict) except Empty: break def run(self): """Loop where the tracking function runs.""" self.pipeline = self.pipeline_cls() self.pipeline.setup() while not self.finished_signal.is_set(): # Gets the processing parameters from their queue self.retrieve_params() # Gets frame from its queue, if the input is too fast, drop frames # and process the latest, if it is too slow continue: try: time, frame_idx, frame = self.frame_queue.get(timeout=0.001) except Empty: continue messages = [] # If we are copying the frames to another queue (e.g. for video recording), do it here if self.recording_signal is not None and self.recording_signal.is_set(): try: self.frame_copy_queue.put(frame.copy(), timestamp=time) except: messages.append("W:Dropping frames from recording") # If a processing function is specified, apply it: new_messages, output = self.pipeline.run(frame) for msg in messages + new_messages: self.message_queue.put(msg) self.output_queue.put(time, output) if self.second_output_queue is not None: self.second_output_queue.put(time, output) # calculate the frame rate self.update_framerate() # put current frame into the GUI queue self.send_to_gui( time, self.pipeline.diagnostic_image if self.pipeline.diagnostic_image is not None else frame, ) return def send_to_gui(self, frametime, frame): """Sends the current frame to the GUI queue at the appropriate framerate""" if self.framerate_rec.current_framerate: every_x = max( int(self.framerate_rec.current_framerate / self.gui_framerate), 1 ) else: every_x = 1 if self.i == 0: try: self.gui_queue.put(frame, timestamp=frametime) except Full: self.message_queue.put("E:GUI queue full") self.i = (self.i + 1) % every_x
def __init__( self, in_frame_queue, finished_signal: Event = None, pipeline=None, processing_parameter_queue=None, output_queue=None, second_output_queue=None, recording_signal=None, gui_framerate=30, max_mb_queue=100, **kwargs ): """ Frame dispatcher process Parameters ---------- in_frame_queue: queue dispatching frames from camera finished_signal signal for the end of the acquisition pipeline: Pipeline tracking pipeline processing_parameter_queue queue for function¶meters output_queue: tracking output queue recording_signal: bool (false) processing_counter gui_framerate: int target framerate of the display GUI gui_dispatcher max_mb_queue: int (200) the maximal size of the image output queues kwargs """ super().__init__(name="tracking", **kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue(max_mbytes=max_mb_queue) # GUI queue for self.recording_signal = recording_signal if recording_signal is not None: self.frame_copy_queue = TimestampedArrayQueue(max_mbytes=max_mb_queue) else: self.frame_copy_queue = None # displaying # the image self.output_queue = output_queue # queue for processing output (e.g., pos) self.second_output_queue = second_output_queue # user defined optional second output queue, attention needs emptying self.processing_parameter_queue = processing_parameter_queue self.finished_signal = finished_signal self.gui_framerate = gui_framerate self.pipeline_cls = pipeline self.pipeline = None self.i = 0
class FrameDispatcher(FrameProcess): """A class which handles taking frames from the camera and processing them, as well as dispatching a subset for display Parameters ---------- Returns ------- """ def __init__(self, in_frame_queue, finished_signal=None, processing_class=None, preprocessing_class=None, processing_parameter_queue=None, gui_framerate=30, **kwargs): """ :param in_frame_queue: queue dispatching frames from camera :param finished_signal: signal for the end of the acquisition :param processing_parameter_queue: queue for function¶meters :param gui_framerate: framerate of the display GUI """ super().__init__(**kwargs) self.frame_queue = in_frame_queue self.gui_queue = TimestampedArrayQueue( ) # GUI queue for displaying the image self.output_queue = Queue() # queue for processing output (e.g., pos) self.processing_parameters = dict() self.finished_signal = finished_signal self.i = 0 self.gui_framerate = gui_framerate self.preprocessing_obj = get_preprocessing_method( preprocessing_class).process self.preprocessing_state = None self.processing_obj = get_tracking_method(processing_class).detect self.processing_parameter_queue = processing_parameter_queue def process_internal(self, frame): """Apply processing function to current frame with self.processing_parameters as additional inputs. Parameters ---------- frame : frame to be processed; Returns ------- type processed output """ def run(self): """Loop where the tracking function runs.""" while not self.finished_signal.is_set(): # Gets the processing parameters from their queue if self.processing_parameter_queue is not None: try: # Read all parameters from the queue: self.processing_parameters.update( **self.processing_parameter_queue.get(timeout=0.0001)) except Empty: pass # Gets frame from its queue: try: time, frame = self.frame_queue.get(timeout=0.001) # If a processing function is specified, apply it: if self.preprocessing_obj is not None: processed, self.preprocessing_state = self.preprocessing_obj( frame, self.preprocessing_state, **self.processing_parameters) else: processed = frame if self.processing_obj is not None: output = self.processing_obj(processed, **self.processing_parameters) self.output_queue.put((datetime.now(), output)) # calculate the frame rate self.update_framerate() # put current frame into the GUI queue if self.processing_parameters["display_processed"]: self.send_to_gui(processed) else: self.send_to_gui(frame) except Empty: # if there is nothing in frame queue pass return def send_to_gui(self, frame): """ Sends the current frame to the GUI queue at the appropriate framerate""" if self.current_framerate: every_x = max(int(self.current_framerate / self.gui_framerate), 1) else: every_x = 1 if self.i == 0: self.gui_queue.put(frame) self.i = (self.i + 1) % every_x