def test_custom_img(self): max_pixel_value = lambda img: img.max() custom = lambda img: img.tolist() img_interface = Interface(max_pixel_value, "image", "label", interpretation=custom) result = img_interface.interpret([gradio.test_data.BASE64_IMAGE])[0] expected_result = np.asarray(decode_base64_to_image(gradio.test_data.BASE64_IMAGE).convert('RGB')).tolist() self.assertEqual(result, expected_result)
def rebuild(self, dir, data): """ Default rebuild method to decode a base64 image """ im = processing_utils.decode_base64_to_image(data) timestamp = datetime.datetime.now() filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png' im.save(f'{dir}/{filename}', 'PNG') return filename
def preprocess(self, x): """ Default preprocessing method for is to convert the picture to black and white and resize to be 48x48 """ im = processing_utils.decode_base64_to_image(x) im = im.convert('RGB') im = processing_utils.resize_and_crop( im, (self.image_width, self.image_height)) return np.array(im)
def rebuild(self, dir, data): """ Default rebuild method to decode a base64 image """ im = processing_utils.decode_base64_to_image(data) timestamp = datetime.datetime.now() filename = 'output_{}_{}.png'.format(self.label, timestamp.strftime("%Y-%m-%d-%H-%M-%S")) im.save('{}/{}'.format(dir, filename), 'PNG') return filename
def get_interpretation_scores(self, x, scores, masks): x = processing_utils.decode_base64_to_image(x) x = np.array(x) output_scores = np.zeros((x.shape[0], x.shape[1])) for score, mask in zip(scores, masks): output_scores += score * mask max_val, min_val = np.max(output_scores), np.min(output_scores) if max_val > 0: output_scores = (output_scores - min_val) / (max_val - min_val) return output_scores.tolist()
def preprocess(self, x): im = processing_utils.decode_base64_to_image(x) with warnings.catch_warnings(): warnings.simplefilter("ignore") im = im.convert(self.image_mode) if self.shape is not None: im = processing_utils.resize_and_crop( im, (self.shape[0], self.shape[1])) if self.type == "pil": return im elif self.type == "numpy": return np.array(im) elif self.type == "file": file_obj = tempfile.NamedTemporaryFile() im.save(file_obj.name) return file_obj
def get_interpretation_scores(self, x, neighbors, scores, masks): """ Returns: (List[List[float]]): A 2D array representing the interpretation score of each pixel of the image. """ x = processing_utils.decode_base64_to_image(x) x = np.array(x) output_scores = np.zeros((x.shape[0], x.shape[1])) for score, mask in zip(scores, masks): output_scores += score * mask max_val, min_val = np.max(output_scores), np.min(output_scores) if max_val > 0: output_scores = (output_scores - min_val) / (max_val - min_val) return output_scores.tolist()
def get_interpretation_neighbors(self, x): x = processing_utils.decode_base64_to_image(x) if self.shape is not None: x = processing_utils.resize_and_crop(x, self.shape) image = np.array(x) segments_slic = slic(image, self.interpretation_segments, compactness=10, sigma=1) leave_one_out_tokens, masks = [], [] replace_color = np.mean(image, axis=(0, 1)) for (i, segVal) in enumerate(np.unique(segments_slic)): mask = segments_slic == segVal white_screen = np.copy(image) white_screen[segments_slic == segVal] = replace_color leave_one_out_tokens.append( processing_utils.encode_array_to_base64(white_screen)) masks.append(mask) return leave_one_out_tokens, {"masks": masks}, True
def preprocess(self, x): """ Default preprocessing method for the SketchPad is to convert the sketch to black and white and resize 28x28 """ im_transparent = processing_utils.decode_base64_to_image(x) # Create a white background for the alpha channel im = PIL.Image.new("RGBA", im_transparent.size, "WHITE") im.paste(im_transparent, (0, 0), im_transparent) im = im.convert('L') if self.invert_colors: im = PIL.ImageOps.invert(im) im = im.resize((self.image_width, self.image_height)) if self.flatten: array = np.array(im).flatten().reshape( 1, self.image_width * self.image_height) else: array = np.array(im).flatten().reshape(1, self.image_width, self.image_height) return array
def preprocess(self, x): im = processing_utils.decode_base64_to_image(x) fmt = im.format with warnings.catch_warnings(): warnings.simplefilter("ignore") im = im.convert(self.image_mode) if self.shape is not None: im = processing_utils.resize_and_crop(im, self.shape) if self.invert_colors: im = PIL.ImageOps.invert(im) if self.type == "pil": return im elif self.type == "numpy": return np.array(im) elif self.type == "file": file_obj = tempfile.NamedTemporaryFile(suffix=("."+fmt.lower() if fmt is not None else ".png")) im.save(file_obj.name) return file_obj else: raise ValueError("Unknown type: " + str(self.type) + ". Please choose from: 'numpy', 'pil', 'file'.")