def show(img): """展示图片 """ if isinstance(img, bytes): img = IPyImage(img) elif isinstance(img, np.ndarray): if img.max() <= 1: img = img * 255 data = cv.imencode(".png", img)[1].tobytes() img = IPyImage(data) elif isinstance(img, str) and os.path.isfile(img): with open(img, "rb") as f: img = IPyImage(f.read()) display(img)
def predict(test_image_np, detection_model): label_id_offset = 1 for i in range(len(test_images_np)): input_tensor = tf.convert_to_tensor(test_images_np[i], dtype=tf.float32) detections = detect(input_tensor, detection_model) plot_detections( test_images_np[i][0], detections['detection_boxes'][0].numpy(), detections['detection_classes'][0].numpy().astype(np.uint32) + label_id_offset, detections['detection_scores'][0].numpy(), category_index, figsize=(15, 20), image_name="gif_frame_" + ('%02d' % i) + ".jpg") imageio.plugins.freeimage.download() anim_file = 'duckies_test.gif' filenames = glob.glob('gif_frame_*.jpg') filenames = sorted(filenames) last = -1 images = [] for filename in filenames: image = imageio.imread(filename) images.append(image) imageio.mimsave(anim_file, images, 'GIF-FI', fps=5) display(IPyImage(open(anim_file, 'rb').read()))
def test_show_remote_ref(): from IPython.display import Image as IPyImage uri = "https://octodex.github.com/images/original.png" img = Image(uri) # TODO check the actual content assert img._repr_html_() == img.display()._repr_html_() assert img.display()._repr_html_() == IPyImage(uri)._repr_html_()
def pil2gif(file_name, img_list, file_dir='gif', fps=10, show=True): if not os.path.exists(file_dir): os.makedirs(file_dir) file_path = os.path.join(file_dir, file_name) img_arr = np.array([np.array(img) for img in img_list]) imageio.mimwrite(file_path, img_arr, fps=fps) if show: with open(file_path, 'rb') as file: display(IPyImage(file.read()))
def screenshot(): def peer(a): print(type(a).__name__) print(type(a).__module__) print(dir(a)) #display(self.pil_image_test) # print(self.core_image.value) #peer(self.figure) #self.figure.save_png("test.png") #display(IPyImage("test.png")) #display(self.core.image) clear_output(wait=True) self.plot.model.plot.show() display(IPyImage(self.core_image.value))
def ipy_image(self) -> IPyImage: """ Converts a clip to an image. """ if self._ipy_image_cache is not None: return self._ipy_image_cache size = self.first_frame.size() raw = self.environment.parent.output.bytes_of(self.first_frame.to_pil()) self._ipy_image_cache = IPyImage( data=raw, format="png", embed=True, unconfined=True, width=size.width, height=size.height ) return self._ipy_image_cache
def on_epoch_end(self, epoch, logs=None): # Randomly sample data indexes = np.random.choice(len(self.inputs), size=self.n_samples) X_test, y_test = self.inputs[indexes], self.ground_truth[indexes] predictions = np.argmax(self.model.predict(X_test), axis=1) # Plot the digits display_digits(X_test, predictions, y_test, epoch, n=self.display_freq) # Save the figure buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) image = Image.open(buf) self.images.append(np.array(image)) # Display the digits every 'display_freq' number of epochs if epoch % self.display_freq == 0: plt.show() def on_train_end(self, logs=None): imageio.mimsave(GIF_PATH, self.images, fps=1) callbacks = [VisCallback(x_test, y_test)] SCALE = 60 # FYI, the format is set to PNG here to bypass checks for acceptable embeddings IPyImage(GIF_PATH, format='png', width=15 * SCALE, height=3 * SCALE)
for i in range(len(test_images_np)): input_tensor = tf.convert_to_tensor(test_images_np[i], dtype=tf.float32) detections = detect(input_tensor) plot_detections( test_images_np[i][0], detections['detection_boxes'][0].numpy(), detections['detection_classes'][0].numpy().astype(np.uint32) + label_id_offset, detections['detection_scores'][0].numpy(), category_index, figsize=(15, 20), image_name="gif_frame_" + ('%02d' % i) + ".jpg") imageio.plugins.freeimage.download() anim_file = 'duckies_test.gif' filenames = glob.glob('gif_frame_*.jpg') filenames = sorted(filenames) last = -1 images = [] for filename in filenames: image = imageio.imread(filename) images.append(image) imageio.mimsave(anim_file, images, 'GIF-FI', fps=5) display(IPyImage(open(anim_file, 'rb').read()))