示例#1
0
    def run(self):
        while not self.quit and not self.is_timed_out():
            #start_time = time.time()
            if self.static_file_mode:
                self.check_increment_and_load_image()
            else:
                if self.freeze_cam and self.latest_cam_frame is not None:
                    # If static file mode was switched to cam mode but cam is still frozen, we need to push the cam frame again
                    if not self.latest_frame_is_from_cam:
                        self._increment_and_set_frame(self.latest_cam_frame, True)
                else:
                    # print "get ros image"
                    frame_full = self.image_converter.get_frame()
                    mask_full = self.image_converter.get_mask()
                    # frame_full = read_cam_frame(self.bound_cap_device)
                    #print '====> just read frame', frame_full.shape
                    # frame = crop_to_square(frame_full)
                    # mask = crop_to_square(mask_full)
                    frame = crop_to_center(frame_full)
                    mask = crop_to_center(mask_full)
                    # remove chanel dim of mask
                    mask = np.reshape(mask, (mask.shape[0], mask.shape[1]))
                    with self.lock:
                        self.latest_cam_frame = frame
                        self._increment_and_set_frame(self.latest_cam_frame, True)
                        self.latest_mask = mask
            #if self.latest_frame is not None:
            #    self.update_frame(self.latest_frame)
            #    self.latest_frame = None
            #    #self.read_frames += 1

            time.sleep(self.sleep_after_read_frame)
            #print 'Reading one frame took', time.time() - start_time

        print 'InputImageFetcher: exiting run method'
示例#2
0
 def check_increment_and_load_image(self):
     with self.lock:
         if (self.static_file_idx_increment == 0 and
             self.static_file_idx is not None and
             not self.latest_frame_is_from_cam and
             self.latest_static_frame is not None):
             # Skip if a static frame is already loaded and there is no increment
             return
         available_files = []
         match_flags = re.IGNORECASE if self.settings.static_files_ignore_case else 0
         for filename in os.listdir(self.settings.static_files_dir):
             if re.match(self.settings.static_files_regexp, filename, match_flags):
                 available_files.append(filename)
         #print 'Found files:'
         #for filename in available_files:
         #    print '   %s' % filename
         assert len(available_files) != 0, ('Error: No files found in %s matching %s (current working directory is %s)' %
                                            (self.settings.static_files_dir, self.settings.static_files_regexp, os.getcwd()))
         if self.static_file_idx is None:
             self.static_file_idx = 0
         self.static_file_idx = (self.static_file_idx + self.static_file_idx_increment) % len(available_files)
         self.static_file_idx_increment = 0
         if self.latest_static_filename != available_files[self.static_file_idx] or self.latest_static_frame is None:
             self.latest_static_filename = available_files[self.static_file_idx]
             im = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename))
             if not self.static_file_stretch_mode:
                 im = crop_to_center(im)
                 # im = crop_to_square(im)
             self.latest_static_frame = im
         self._increment_and_set_frame(self.latest_static_frame, False)
         self.latest_mask = np.zeros(self.latest_static_frame.shape[0:2])
         self.latest_mask.fill(255)