def render_widget(self): if bimpy.slider_int(self.bimpy_name, self._bimpy_value, self.min, self.max): self._value = self._bimpy_value.value
ctx.new_frame() bimpy.show_demo_window() # Widget reference if bimpy.begin_main_menu_bar(): if bimpy.menu_item('Save', ''): save_data(args.save_path) if bimpy.menu_item('Load', ''): load_data(args.save_path) bimpy.end_main_menu_bar( ) # According to bimpy docs, this is a special case where end is called inside the if. if bimpy.begin("Video", opened=tab_video_view): is_placing_rect = True s = bimpy.text(args.base_path_video) b_i = bimpy.Int(display_frame) bimpy.slider_int("Frame", b_i, 0, video_len, "%d") if bimpy.button(" < Prev (z) ") or bimpy.is_key_released(ord('Z')): b_i.value -= 1 bimpy.same_line() bimpy.checkbox("Autoplay (c to stop)", is_autoplay) if bimpy.is_key_down(ord('C')): is_autoplay.value = False bimpy.same_line() if bimpy.button(" Next > (x) ") or bimpy.is_key_released( ord('X')) or is_autoplay.value: b_i.value += 1 if display_frame != b_i.value: simulate_to_frame(b_i.value)
def main(): parser = ArgumentParser(description="Preview animations") parser.add_argument("--version", action="version", version="%(prog)s " + __version__) parser.add_argument("--width", dest="width", type=int, default=DEF_WIDTH, help="frame width (default: %s)" % DEF_WIDTH) parser.add_argument("--height", dest="height", type=int, default=DEF_HEIGHT, help="frame height (default: %s)" % DEF_HEIGHT) parser.add_argument("--scale", dest="scale", type=int, default=DEF_SCALE, help="scale preview (default: %s)" % DEF_SCALE) parser.add_argument("--double-w", dest="dw", action="store_true", help="double width for 2:1") parser.add_argument( "--mtime", dest="mtime", type=int, default=DEF_MTIME, help="seconds between checks for changes (default: %s)" % DEF_MTIME) parser.add_argument("image", help="image to convert") args = parser.parse_args() def load_image(filename): @with_retry def load(): return Image.open(filename).convert("RGB") try: image = load() except IOError: parser.error("failed to open the image") (w, h) = image.size if w % args.width or h % args.height: parser.error("%s size is not multiple of tile size (%s, %s)" % (filename, args.width, args.height)) frames = [] for y in range(0, h, args.height): for x in range(0, w, args.width): frames.append((x, y, x + args.width, y + args.height)) return image, frames image, frames = load_image(args.image) frame_list = list(range(len(frames))) def scale_image(scale, frameno): scale_w = scale if not args.dw else scale * 2 current = image.resize((args.width * scale_w, args.height * scale), box=frames[frame_list[frameno]], resample=0) return bimpy.Image(current) ctx = bimpy.Context() ctx.init(320, 420, "Preview animation") orig = bimpy.Image(image) scale = bimpy.Int(args.scale) fps = bimpy.Int(args.scale) frame_list_str = bimpy.String(','.join(map(str, frame_list))) im = scale_image(scale.value, 0) cur_frame = 0 paused = False start_time = time() check_mtime = time() last_mtime = os.stat(args.image).st_mtime while (not ctx.should_close()): if time() - check_mtime > args.mtime: if os.stat(args.image).st_mtime != last_mtime: last_mtime = os.stat(args.image).st_mtime image, frames = load_image(args.image) cur_frame = 0 start_time = time() if any([f >= len(frames) for f in frame_list]): frame_list = list(range(len(frames))) frame_list_str = bimpy.String(','.join(map( str, frame_list))) ctx.new_frame() bimpy.set_next_window_pos(bimpy.Vec2(10, 10), bimpy.Condition.Once) bimpy.set_next_window_size(bimpy.Vec2(300, 400), bimpy.Condition.Once) bimpy.begin("Image: %s" % args.image) if not paused: if time() - start_time >= 1. / fps.value: start_time = time() cur_frame += 1 if cur_frame == len(frame_list): cur_frame = 0 im = scale_image(scale.value, cur_frame) bimpy.image(orig) bimpy.image(im) bimpy.text("Frame: %02d" % frame_list[cur_frame]) if bimpy.slider_int("Scale", scale, 1, 20): im = scale_image(scale.value, cur_frame) if bimpy.slider_int("FPS", fps, 1, 30): start_time = time() cur_frame = 0 if bimpy.input_text("Frames", frame_list_str, 64, bimpy.InputTextFlags.EnterReturnsTrue): try: new_frame_list = [ int(i.strip()) for i in frame_list_str.value.split(",") ] frame_list = new_frame_list start_time = time() cur_frame = 0 except Exception as ex: print("Error parsing frame list: %s" % ex) if bimpy.button("Play" if paused else "Pause"): paused = not paused bimpy.end() ctx.render()
def sample(cfg, logger): model = Model( startf=cfg.MODEL.START_CHANNEL_COUNT, layer_count= cfg.MODEL.LAYER_COUNT, maxf=cfg.MODEL.MAX_CHANNEL_COUNT, latent_size=cfg.MODEL.LATENT_SPACE_SIZE, truncation_psi=cfg.MODEL.TRUNCATIOM_PSI, truncation_cutoff=cfg.MODEL.TRUNCATIOM_CUTOFF, mapping_layers=cfg.MODEL.MAPPING_LAYERS, channels=3) model.eval() logger.info("Trainable parameters generator:") count_parameters(model.generator) model_dict = { 'generator_s': model.generator, 'mapping_fl_s': model.mapping, 'dlatent_avg': model.dlatent_avg, } checkpointer = Checkpointer(cfg, model_dict, logger=logger, save=True) checkpointer.load() ctx = bimpy.Context() remove = bimpy.Bool(False) layers = bimpy.Int(8) ctx.init(1800, 1600, "Styles") rnd = np.random.RandomState(5) latents = rnd.randn(1, cfg.MODEL.LATENT_SPACE_SIZE) sample = torch.tensor(latents).float().cuda() def update_image(sample): with torch.no_grad(): torch.manual_seed(0) model.eval() x_rec = model.generate(layers.value, remove.value, z=sample) #model.generator.set(l.value, c.value) resultsample = ((x_rec * 0.5 + 0.5) * 255).type(torch.long).clamp(0, 255) resultsample = resultsample.cpu()[0, :, :, :] return resultsample.type(torch.uint8).transpose(0, 2).transpose(0, 1) with torch.no_grad(): save_image(model.generate(8, True, z=sample) * 0.5 + 0.5, 'sample.png') im = bimpy.Image(update_image(sample)) while(not ctx.should_close()): with ctx: bimpy.set_window_font_scale(2.0) if bimpy.checkbox('REMOVE BLOB', remove): im = bimpy.Image(update_image(sample)) if bimpy.button('NEXT'): latents = rnd.randn(1, cfg.MODEL.LATENT_SPACE_SIZE) sample = torch.tensor(latents).float().cuda() im = bimpy.Image(update_image(sample)) if bimpy.slider_int("Layers", layers, 0, 8): im = bimpy.Image(update_image(sample)) bimpy.image(im, bimpy.Vec2(1024, 1024))
if img_urls: bimpy.set_next_window_pos(bimpy.Vec2(625, 20), bimpy.Condition.Once) bimpy.set_next_window_size(bimpy.Vec2(532, 600), bimpy.Condition.Once) bimpy.begin('Collage', bimpy.Bool(True), bimpy.WindowFlags.HorizontalScrollbar) if imgs_downloading: bimpy.text("Downloading thumbnails") bimpy.progress_bar(percent_downloaded, bimpy.Vec2(-1, 0), f"{imgs_downloaded}/{imgs_total}") else: bimpy.text("Collage columns") bimpy.same_line() bimpy.slider_int("", b_col_count, 1, len(img_urls)) if not imgs_downloading and bimpy.button("Save Collage"): # print(data) saved = save_collage(current_playlist_id, img_urls, imgdict, program_start_dir, b_col_count.value) saved_time = time.clock() print("saved:", saved) if time.clock() - saved_time <= 2: bimpy.same_line() bimpy.text(f"Saved to {saved}.png") width, height = 64, 64 first = True col_count = 0