예제 #1
0
    def load_image_into_pane_original_format(self,
                                             state_layer_name,
                                             state_selected_unit,
                                             resize_shape,
                                             images,
                                             sub_folder,
                                             file_pattern,
                                             image_index_to_set,
                                             should_crop_to_corner=False):

        jpg_path = os.path.join(
            self.settings.caffevis_outputs_dir, sub_folder, state_layer_name,
            file_pattern % (state_layer_name, state_selected_unit))

        try:
            img = caffe.io.load_image(jpg_path)

            if should_crop_to_corner:
                img = crop_to_corner(img, 2)
            images[image_index_to_set] = ensure_uint255_and_resize_without_fit(
                img, resize_shape)

        except IOError:
            print('\nAttempted to load file %s but failed. To supress this warning, remove layer "%s" from settings.caffevis_jpgvis_layers' % \
                  (jpg_path, state_layer_name))
            # set black image as place holder
            images[image_index_to_set] = np.zeros(
                (resize_shape[0], resize_shape[1], 3), dtype=np.uint8)
            pass
    def display_frame(self, frame):

        full_pane_shape = self.panes['input'].data.shape[:2][::-1]
        if self.settings.is_siamese and ((type(frame),len(frame)) == (tuple,2)):
            frame1 = frame[0]
            frame2 = frame[1]

            half_pane_shape = (full_pane_shape[0], full_pane_shape[1]/2)
            frame_disp1 = ensure_uint255_and_resize_without_fit(frame1[:], half_pane_shape)
            frame_disp2 = ensure_uint255_and_resize_without_fit(frame2[:], half_pane_shape)
            frame_disp = np.concatenate((frame_disp1, frame_disp2), axis=1)
        else:
            frame_disp = ensure_uint255_and_resize_without_fit(frame[:], full_pane_shape)

        if self.settings._calculated_is_gray_model:
            frame_disp = gray_to_color(frame_disp)

        self.panes['input'].data[:] = frame_disp
예제 #3
0
def get_image_from_files(settings, unit_folder_path, should_crop_to_corner, resize_shape, first_only, captions = [], values = []):
    try:

        # list unit images
        unit_images_path = sorted(glob.glob(unit_folder_path))

        mega_image = np.zeros((resize_shape[0], resize_shape[1], 3), dtype=np.uint8)

        # if no images
        if not unit_images_path:
            return mega_image

        if first_only:
            unit_images_path = [unit_images_path[0]]

        # load all images
        unit_images = [caffe_load_image(unit_image_path, color=True, as_uint=True) for unit_image_path in
                       unit_images_path]

        if settings.caffevis_clear_negative_activations:
            # clear images with 0 value
            if values:
                for i in range(len(values)):
                    if values[i] < float_info.epsilon:
                        unit_images[i] *= 0

        if should_crop_to_corner:
            unit_images = [crop_to_corner(img, 2) for img in unit_images]

        num_images = len(unit_images)
        images_per_axis = int(np.math.ceil(np.math.sqrt(num_images)))
        padding_pixel = 1

        if first_only:
            single_resized_image_shape = (resize_shape[0] - 2*padding_pixel, resize_shape[1] - 2*padding_pixel)
        else:
            single_resized_image_shape = ((resize_shape[0] / images_per_axis) - 2*padding_pixel, (resize_shape[1] / images_per_axis) - 2*padding_pixel)
        unit_images = [ensure_uint255_and_resize_without_fit(unit_image, single_resized_image_shape) for unit_image in unit_images]

        # build mega image

        should_add_caption = (len(captions) == num_images)
        defaults = {'face': settings.caffevis_score_face,
                    'fsize': settings.caffevis_score_fsize,
                    'clr': to_255(settings.caffevis_score_clr),
                    'thick': settings.caffevis_score_thick}

        for i in range(num_images):

            # add caption if we have exactly one for each image
            if should_add_caption:
                loc = settings.caffevis_score_loc[::-1]   # Reverse to OpenCV c,r order
                fs = FormattedString(captions[i], defaults)
                cv2_typeset_text(unit_images[i], [[fs]], loc)

            cell_row = i / images_per_axis
            cell_col = i % images_per_axis
            mega_image_height_start = 1 + cell_row * (single_resized_image_shape[0] + 2 * padding_pixel)
            mega_image_height_end = mega_image_height_start + single_resized_image_shape[0]
            mega_image_width_start = 1 + cell_col * (single_resized_image_shape[1] + 2 * padding_pixel)
            mega_image_width_end = mega_image_width_start + single_resized_image_shape[1]
            mega_image[mega_image_height_start:mega_image_height_end, mega_image_width_start:mega_image_width_end,:] = unit_images[i]

        return mega_image

    except:
        print '\nAttempted to load files from %s but failed. ' % unit_folder_path
        # set black image as place holder
        return np.zeros((resize_shape[0], resize_shape[1], 3), dtype=np.uint8)
        pass

    return