def _reconstruct(self, predictions, ids): counter = 0 for (prediction, id) in predictions: # prediction = 255- (prediction*255) # prediction = prediction * 255 img_ref = next((x for x in self.test_img_refs if x.probe_file_id == id), None) try: if self._is_keras_img_model(): pred = 255 - np.array(MinMaxScaler((0, 255)).fit_transform(prediction[0].reshape(-1, 1))).flatten() img = pred.reshape(self.patch_shape, self.patch_shape) else: pred = 255 - np.array(MinMaxScaler((0, 255)).fit_transform(prediction.reshape(-1, 1))).flatten() if self.config['data_type']=="image": img = pred.reshape( img_ref.img_height//self.image_downscale_factor, img_ref.img_width//self.image_downscale_factor) elif self.config['data_type'] =="csv": img = pred.reshape( img_ref.img_height, img_ref.img_width) img_original_size = cv2.resize( img, (img_ref.img_orig_width, img_ref.img_orig_height)) except: counter +=1 img_original_size = np.zeros((img_ref.img_orig_width, img_ref.img_orig_height)) file_name = f'{img_ref.probe_file_id}.png' file_path = self.output_dir + file_name ImageUtils.save_image(img_original_size, file_path) print(f'Number of errors in reconstruction {counter}')
def _reconstruct(self, predictions, ids): for (prediction, id), patch_img_ref in zip(predictions, self.test_img_refs): prediction = 255 - (prediction * 255) # prediction = prediction * 255 img = self._reconstruct_image_from_patches(prediction, patch_img_ref) img_original_size = cv2.resize( img, (patch_img_ref.img_orig_width, patch_img_ref.img_orig_height)) file_name = f'{patch_img_ref.probe_file_id}.png' file_path = self.output_dir + file_name ImageUtils.save_image(img_original_size, file_path)
def _create_img_patch(self, img_ref, targets_path): target_image_path = os.path.join(targets_path, "manipulation", "mask", img_ref.probe_mask_file_name) + ".png" border_value = [255, 255, 255] if self.tuning['black_border_y'] is True or self.tuning[ 'dilate_y_black_border_y'] is True: border_value = [0, 0, 0] bordered_img, border_top, border_left, original_img_shape = ImageUtils.get_image_with_border( target_image_path, self.patch_shape, self.img_downscale_factor, border_value=border_value) if self.tuning["dilate_y"] is True: diluted_target = ImageUtils.dilate(bordered_img) target_image_out_dir = self.output_dir + 'target_image/' bordered_image_patches, patch_window_shape = PatchUtils.get_patches( bordered_img, self.patch_shape) for i, patch in enumerate(bordered_image_patches): path = f'{target_image_out_dir}{img_ref.probe_file_id}_{i}.png' ImageUtils.save_image(patch, path) patch_img_ref = PatchImageRefFactory.create_img_ref( img_ref.probe_file_id, bordered_img.shape, patch_window_shape, img_ref.probe_mask_file_name, original_img_shape, border_top, border_left) indicators = None for indicator_dir in self.indicator_directories: indicator_out_path = self.output_dir + indicator_dir + '/' img_path = os.path.join(self.indicators_path, indicator_dir, "mask", img_ref.probe_file_id) + ".png" try: img, _, _, _ = ImageUtils.get_image_with_border( img_path, self.patch_shape, self.img_downscale_factor, top=border_top, left=border_left) except FileNotFoundError as err: img = self._handle_missing_indicator_image(bordered_img.shape) finally: img_patches, _ = PatchUtils.get_patches(img, self.patch_shape) for i, patch in enumerate(img_patches): path = f'{indicator_out_path}{img_ref.probe_file_id}_{i}.png' ImageUtils.save_image(patch, path) return patch_img_ref
def _save_image(self,img,base_path, file_name): path = f'{base_path}{file_name}.png' ImageUtils.save_image(img,path)