def on_clients_finalized(self): io.progress_bar_close() if self.debug: io.destroy_all_windows()
def main(args, device_args): io.log_info("Running trainer.\r\n") no_preview = args.get('no_preview', False) s2c = queue.Queue() c2s = queue.Queue() thread = threading.Thread(target=trainerThread, args=(s2c, c2s, args, device_args)) thread.start() if no_preview: while True: if not c2s.empty(): input = c2s.get() op = input.get('op', '') if op == 'close': break try: io.process_messages(0.1) except KeyboardInterrupt: s2c.put({'op': 'close'}) else: wnd_name = "Training preview" io.named_window(wnd_name) io.capture_keys(wnd_name) previews = None loss_history = None selected_preview = 0 update_preview = False is_showing = False is_waiting_preview = False show_last_history_iters_count = 0 iter = 0 while True: if not c2s.empty(): input = c2s.get() op = input['op'] if op == 'show': is_waiting_preview = False loss_history = input[ 'loss_history'] if 'loss_history' in input.keys( ) else None previews = input['previews'] if 'previews' in input.keys( ) else None iter = input['iter'] if 'iter' in input.keys() else 0 if previews is not None: max_w = 0 max_h = 0 for (preview_name, preview_rgb) in previews: (h, w, c) = preview_rgb.shape max_h = max(max_h, h) max_w = max(max_w, w) max_size = 800 if max_h > max_size: max_w = int(max_w / (max_h / max_size)) max_h = max_size #make all previews size equal for preview in previews[:]: (preview_name, preview_rgb) = preview (h, w, c) = preview_rgb.shape if h != max_h or w != max_w: previews.remove(preview) previews.append( (preview_name, cv2.resize(preview_rgb, (max_w, max_h)))) selected_preview = selected_preview % len(previews) update_preview = True elif op == 'close': break if update_preview: update_preview = False selected_preview_name = previews[selected_preview][0] selected_preview_rgb = previews[selected_preview][1] (h, w, c) = selected_preview_rgb.shape # HEAD head_lines = [ '[s]:save [enter]:exit', '[p]:update [space]:next preview [l]:change history range', 'Preview: "%s" [%d/%d]' % (selected_preview_name, selected_preview + 1, len(previews)) ] head_line_height = 15 head_height = len(head_lines) * head_line_height head = np.ones((head_height, w, c)) * 0.1 for i in range(0, len(head_lines)): t = i * head_line_height b = (i + 1) * head_line_height head[t:b, 0:w] += imagelib.get_text_image( (head_line_height, w, c), head_lines[i], color=[0.8] * c) final = head if loss_history is not None: if show_last_history_iters_count == 0: loss_history_to_show = loss_history else: loss_history_to_show = loss_history[ -show_last_history_iters_count:] lh_img = models.ModelBase.get_loss_history_preview( loss_history_to_show, iter, w, c) final = np.concatenate([final, lh_img], axis=0) final = np.concatenate([final, selected_preview_rgb], axis=0) final = np.clip(final, 0, 1) io.show_image(wnd_name, (final * 255).astype(np.uint8)) is_showing = True key_events = io.get_key_events(wnd_name) key, chr_key, ctrl_pressed, alt_pressed, shift_pressed = key_events[ -1] if len(key_events) > 0 else (0, 0, False, False, False) if key == ord('\n') or key == ord('\r'): s2c.put({'op': 'close'}) elif key == ord('s'): s2c.put({'op': 'save'}) elif key == ord('p'): if not is_waiting_preview: is_waiting_preview = True s2c.put({'op': 'preview'}) elif key == ord('l'): if show_last_history_iters_count == 0: show_last_history_iters_count = 5000 elif show_last_history_iters_count == 5000: show_last_history_iters_count = 10000 elif show_last_history_iters_count == 10000: show_last_history_iters_count = 50000 elif show_last_history_iters_count == 50000: show_last_history_iters_count = 100000 elif show_last_history_iters_count == 100000: show_last_history_iters_count = 0 update_preview = True elif key == ord(' '): selected_preview = (selected_preview + 1) % len(previews) update_preview = True try: io.process_messages(0.1) except KeyboardInterrupt: s2c.put({'op': 'close'}) io.destroy_all_windows()
def mask_editor_main(input_dir, confirmed_dir=None, skipped_dir=None, no_default_mask=False): input_path = Path(input_dir) confirmed_path = Path(confirmed_dir) skipped_path = Path(skipped_dir) if not input_path.exists(): raise ValueError('Input directory not found. Please ensure it exists.') if not confirmed_path.exists(): confirmed_path.mkdir(parents=True) if not skipped_path.exists(): skipped_path.mkdir(parents=True) if not no_default_mask: eyebrows_expand_mod = np.clip( io.input_int( "Default eyebrows expand modifier? (0..400, skip:100) : ", 100), 0, 400) / 100.0 else: eyebrows_expand_mod = None wnd_name = "MaskEditor tool" io.named_window(wnd_name) io.capture_mouse(wnd_name) io.capture_keys(wnd_name) cached_images = {} image_paths = [Path(x) for x in Path_utils.get_image_paths(input_path)] done_paths = [] done_images_types = {} image_paths_total = len(image_paths) saved_ie_polys = IEPolys() zoom_factor = 1.0 preview_images_count = 9 target_wh = 256 do_prev_count = 0 do_save_move_count = 0 do_save_count = 0 do_skip_move_count = 0 do_skip_count = 0 def jobs_count(): return do_prev_count + do_save_move_count + do_save_count + do_skip_move_count + do_skip_count is_exit = False while not is_exit: if len(image_paths) > 0: filepath = image_paths.pop(0) else: filepath = None next_image_paths = image_paths[0:preview_images_count] next_image_paths_names = [path.name for path in next_image_paths] prev_image_paths = done_paths[-preview_images_count:] prev_image_paths_names = [path.name for path in prev_image_paths] for key in list(cached_images.keys()): if key not in prev_image_paths_names and \ key not in next_image_paths_names: cached_images.pop(key) for paths in [prev_image_paths, next_image_paths]: for path in paths: if path.name not in cached_images: cached_images[path.name] = cv2_imread(str(path)) / 255.0 if filepath is not None: if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None if dflimg is None: io.log_err("%s is not a dfl image file" % (filepath.name)) continue else: lmrks = dflimg.get_landmarks() ie_polys = dflimg.get_ie_polys() fanseg_mask = dflimg.get_fanseg_mask() if filepath.name in cached_images: img = cached_images[filepath.name] else: img = cached_images[filepath.name] = cv2_imread( str(filepath)) / 255.0 if fanseg_mask is not None: mask = fanseg_mask else: if no_default_mask: mask = np.zeros((target_wh, target_wh, 3)) else: mask = LandmarksProcessor.get_image_hull_mask( img.shape, lmrks, eyebrows_expand_mod=eyebrows_expand_mod) else: img = np.zeros((target_wh, target_wh, 3)) mask = np.ones((target_wh, target_wh, 3)) ie_polys = None def get_status_lines_func(): return [ 'Progress: %d / %d . Current file: %s' % (len(done_paths), image_paths_total, str(filepath.name) if filepath is not None else "end"), '[Left mouse button] - mark include mask.', '[Right mouse button] - mark exclude mask.', '[Middle mouse button] - finish current poly.', '[Mouse wheel] - undo/redo poly or point. [+ctrl] - undo to begin/redo to end', '[r] - applies edits made to last saved image.', '[q] - prev image. [w] - skip and move to %s. [e] - save and move to %s. ' % (skipped_path.name, confirmed_path.name), '[z] - prev image. [x] - skip. [c] - save. ', 'hold [shift] - speed up the frame counter by 10.', '[-/+] - window zoom [esc] - quit', ] try: ed = MaskEditor(img, [(done_images_types[name], cached_images[name]) for name in prev_image_paths_names], [(0, cached_images[name]) for name in next_image_paths_names], mask, ie_polys, get_status_lines_func) except Exception as e: print(e) continue next = False while not next: io.process_messages(0.005) if jobs_count() == 0: for (x, y, ev, flags) in io.get_mouse_events(wnd_name): x, y = int(x / zoom_factor), int(y / zoom_factor) ed.set_mouse_pos(x, y) if filepath is not None: if ev == io.EVENT_LBUTTONDOWN: ed.mask_point(1) elif ev == io.EVENT_RBUTTONDOWN: ed.mask_point(0) elif ev == io.EVENT_MBUTTONDOWN: ed.mask_finish() elif ev == io.EVENT_MOUSEWHEEL: if flags & 0x80000000 != 0: if flags & 0x8 != 0: ed.undo_to_begin_point() else: ed.undo_point() else: if flags & 0x8 != 0: ed.redo_to_end_point() else: ed.redo_point() for key, chr_key, ctrl_pressed, alt_pressed, shift_pressed in io.get_key_events( wnd_name): if chr_key == 'q' or chr_key == 'z': do_prev_count = 1 if not shift_pressed else 10 elif chr_key == '-': zoom_factor = np.clip(zoom_factor - 0.1, 0.1, 4.0) ed.set_screen_changed() elif chr_key == '+': zoom_factor = np.clip(zoom_factor + 0.1, 0.1, 4.0) ed.set_screen_changed() elif key == 27: #esc is_exit = True next = True break elif filepath is not None: if chr_key == 'e': saved_ie_polys = ed.ie_polys do_save_move_count = 1 if not shift_pressed else 10 elif chr_key == 'c': saved_ie_polys = ed.ie_polys do_save_count = 1 if not shift_pressed else 10 elif chr_key == 'w': do_skip_move_count = 1 if not shift_pressed else 10 elif chr_key == 'x': do_skip_count = 1 if not shift_pressed else 10 elif chr_key == 'r' and saved_ie_polys != None: ed.set_ie_polys(saved_ie_polys) if do_prev_count > 0: do_prev_count -= 1 if len(done_paths) > 0: if filepath is not None: image_paths.insert(0, filepath) filepath = done_paths.pop(-1) done_images_types[filepath.name] = 0 if filepath.parent != input_path: new_filename_path = input_path / filepath.name filepath.rename(new_filename_path) image_paths.insert(0, new_filename_path) else: image_paths.insert(0, filepath) next = True elif filepath is not None: if do_save_move_count > 0: do_save_move_count -= 1 ed.mask_finish() dflimg.embed_and_set( str(filepath), ie_polys=ed.get_ie_polys(), eyebrows_expand_mod=eyebrows_expand_mod) done_paths += [confirmed_path / filepath.name] done_images_types[filepath.name] = 2 filepath.rename(done_paths[-1]) next = True elif do_save_count > 0: do_save_count -= 1 ed.mask_finish() dflimg.embed_and_set( str(filepath), ie_polys=ed.get_ie_polys(), eyebrows_expand_mod=eyebrows_expand_mod) done_paths += [filepath] done_images_types[filepath.name] = 2 next = True elif do_skip_move_count > 0: do_skip_move_count -= 1 done_paths += [skipped_path / filepath.name] done_images_types[filepath.name] = 1 filepath.rename(done_paths[-1]) next = True elif do_skip_count > 0: do_skip_count -= 1 done_paths += [filepath] done_images_types[filepath.name] = 1 next = True else: do_save_move_count = do_save_count = do_skip_move_count = do_skip_count = 0 if jobs_count() == 0: if ed.switch_screen_changed(): screen = ed.make_screen() if zoom_factor != 1.0: h, w, c = screen.shape screen = cv2.resize( screen, (int(w * zoom_factor), int(h * zoom_factor))) io.show_image(wnd_name, screen) io.process_messages(0.005) io.destroy_all_windows()
def on_clients_finalized(self): if self.manual == True: io.destroy_all_windows() io.progress_bar_close()
def main(args, device_args): io.log_info("Running trainer.\r\n") no_preview = args.get('no_preview', False) flask_preview = args.get('flask_preview', False) s2c = queue.Queue() c2s = queue.Queue() e = threading.Event() previews = None loss_history = None selected_preview = 0 update_preview = False is_waiting_preview = False show_last_history_iters_count = 0 iteration = 0 batch_size = 1 zoom = Zoom.ZOOM_100 if flask_preview: from flaskr.app import create_flask_app s2flask = queue.Queue() socketio, flask_app = create_flask_app(s2c, c2s, s2flask, args) e = threading.Event() thread = threading.Thread(target=trainer_thread, args=(s2c, c2s, e, args, device_args, socketio)) thread.start() e.wait() # Wait for inital load to occur. flask_t = threading.Thread(target=socketio.run, args=(flask_app, ), kwargs={ 'debug': True, 'use_reloader': False }) flask_t.start() while True: if not c2s.empty(): item = c2s.get() op = item['op'] if op == 'show': is_waiting_preview = False loss_history = item[ 'loss_history'] if 'loss_history' in item.keys( ) else None previews = item['previews'] if 'previews' in item.keys( ) else None iteration = item['iter'] if 'iter' in item.keys() else 0 # batch_size = input['batch_size'] if 'iter' in input.keys() else 1 if previews is not None: update_preview = True elif op == 'update': if not is_waiting_preview: is_waiting_preview = True s2c.put({'op': 'preview'}) elif op == 'next_preview': selected_preview = (selected_preview + 1) % len(previews) update_preview = True elif op == 'change_history_range': if show_last_history_iters_count == 0: show_last_history_iters_count = 5000 elif show_last_history_iters_count == 5000: show_last_history_iters_count = 10000 elif show_last_history_iters_count == 10000: show_last_history_iters_count = 50000 elif show_last_history_iters_count == 50000: show_last_history_iters_count = 100000 elif show_last_history_iters_count == 100000: show_last_history_iters_count = 0 update_preview = True elif op == 'close': s2c.put({'op': 'close'}) break elif op == 'zoom_prev': zoom = zoom.prev() update_preview = True elif op == 'zoom_next': zoom = zoom.next() update_preview = True if update_preview: update_preview = False selected_preview = selected_preview % len(previews) preview_pane_image = create_preview_pane_image( previews, selected_preview, loss_history, show_last_history_iters_count, iteration, batch_size, zoom) # io.show_image(wnd_name, preview_pane_image) model_path = Path(args.get('model_path', '')) filename = 'preview.jpg' preview_file = str(model_path / filename) cv2.imwrite(preview_file, preview_pane_image) s2flask.put({'op': 'show'}) socketio.emit('preview', { 'iter': iteration, 'loss': loss_history[-1] }) try: io.process_messages(0.01) except KeyboardInterrupt: s2c.put({'op': 'close'}) else: thread = threading.Thread(target=trainer_thread, args=(s2c, c2s, e, args, device_args)) thread.start() e.wait() # Wait for inital load to occur. if no_preview: while True: if not c2s.empty(): item = c2s.get() op = item.get('op', '') if op == 'close': break try: io.process_messages(0.1) except KeyboardInterrupt: s2c.put({'op': 'close'}) else: wnd_name = "Training preview" io.named_window(wnd_name) io.capture_keys(wnd_name) previews = None loss_history = None selected_preview = 0 update_preview = False is_showing = False is_waiting_preview = False show_last_history_iters_count = 0 iteration = 0 batch_size = 1 zoom = Zoom.ZOOM_100 while True: if not c2s.empty(): item = c2s.get() op = item['op'] if op == 'show': is_waiting_preview = False loss_history = item[ 'loss_history'] if 'loss_history' in item.keys( ) else None previews = item['previews'] if 'previews' in item.keys( ) else None iteration = item['iter'] if 'iter' in item.keys( ) else 0 # batch_size = input['batch_size'] if 'iter' in input.keys() else 1 if previews is not None: update_preview = True elif op == 'close': break if update_preview: update_preview = False selected_preview = selected_preview % len(previews) preview_pane_image = create_preview_pane_image( previews, selected_preview, loss_history, show_last_history_iters_count, iteration, batch_size, zoom) io.show_image(wnd_name, preview_pane_image) key_events = io.get_key_events(wnd_name) key, chr_key, ctrl_pressed, alt_pressed, shift_pressed = key_events[ -1] if len(key_events) > 0 else (0, 0, False, False, False) if key == ord('\n') or key == ord('\r'): s2c.put({'op': 'close'}) elif key == ord('s'): s2c.put({'op': 'save'}) elif key == ord('p'): if not is_waiting_preview: is_waiting_preview = True s2c.put({'op': 'preview'}) elif key == ord('l'): if show_last_history_iters_count == 0: show_last_history_iters_count = 5000 elif show_last_history_iters_count == 5000: show_last_history_iters_count = 10000 elif show_last_history_iters_count == 10000: show_last_history_iters_count = 50000 elif show_last_history_iters_count == 50000: show_last_history_iters_count = 100000 elif show_last_history_iters_count == 100000: show_last_history_iters_count = 0 update_preview = True elif key == ord(' '): selected_preview = (selected_preview + 1) % len(previews) update_preview = True elif key == ord('-'): zoom = zoom.prev() update_preview = True elif key == ord('=') or key == ord('+'): zoom = zoom.next() update_preview = True try: io.process_messages(0.1) except KeyboardInterrupt: s2c.put({'op': 'close'}) io.destroy_all_windows()
def finalize(self): io.destroy_all_windows()
def mask_editor_main(input_dir, confirmed_dir=None, skipped_dir=None): input_path = Path(input_dir) confirmed_path = Path(confirmed_dir) skipped_path = Path(skipped_dir) if not input_path.exists(): raise ValueError('Input directory not found. Please ensure it exists.') if not confirmed_path.exists(): confirmed_path.mkdir(parents=True) if not skipped_path.exists(): skipped_path.mkdir(parents=True) wnd_name = "MaskEditor tool" io.named_window(wnd_name) io.capture_mouse(wnd_name) io.capture_keys(wnd_name) image_paths = [Path(x) for x in Path_utils.get_image_paths(input_path)] done_paths = [] image_paths_total = len(image_paths) do_prev_count = 0 do_save_move_count = 0 do_save_count = 0 do_skip_move_count = 0 do_skip_count = 0 is_exit = False while not is_exit: if len(image_paths) > 0: filepath = image_paths.pop(0) else: filepath = None if filepath is not None: if filepath.suffix == '.png': dflimg = DFLPNG.load(str(filepath)) elif filepath.suffix == '.jpg': dflimg = DFLJPG.load(str(filepath)) else: dflimg = None if dflimg is None: io.log_err("%s is not a dfl image file" % (filepath.name)) continue lmrks = dflimg.get_landmarks() ie_polys = dflimg.get_ie_polys() img = cv2_imread(str(filepath)) / 255.0 mask = LandmarksProcessor.get_image_hull_mask(img.shape, lmrks) else: img = np.zeros((256, 256, 3)) mask = np.ones((256, 256, 3)) ie_polys = None def get_status_lines_func(): return [ 'Progress: %d / %d . Current file: %s' % (len(done_paths), image_paths_total, str(filepath.name) if filepath is not None else "end"), '[Left mouse button] - mark include mask.', '[Right mouse button] - mark exclude mask.', '[Middle mouse button] - finish current poly.', '[Mouse wheel] - undo/redo poly or point. [+ctrl] - undo to begin/redo to end', '[q] - prev image. [w] - skip and move to %s. [e] - save and move to %s. ' % (skipped_path.name, confirmed_path.name), '[z] - prev image. [x] - skip. [c] - save. ', 'hold [shift] - speed up the frame counter by 10.' '[esc] - quit' ] ed = MaskEditor(img, mask, ie_polys, get_status_lines_func) next = False while not next: io.process_messages(0.005) if do_prev_count + do_save_move_count + do_save_count + do_skip_move_count + do_skip_count == 0: for (x, y, ev, flags) in io.get_mouse_events(wnd_name): ed.set_mouse_pos(x, y) if filepath is not None: if ev == io.EVENT_LBUTTONDOWN: ed.mask_point(1) elif ev == io.EVENT_RBUTTONDOWN: ed.mask_point(0) elif ev == io.EVENT_MBUTTONDOWN: ed.mask_finish() elif ev == io.EVENT_MOUSEWHEEL: if flags & 0x80000000 != 0: if flags & 0x8 != 0: ed.undo_to_begin_point() else: ed.undo_point() else: if flags & 0x8 != 0: ed.redo_to_end_point() else: ed.redo_point() for key, chr_key, ctrl_pressed, alt_pressed, shift_pressed in io.get_key_events( wnd_name): if chr_key == 'q' or chr_key == 'z': do_prev_count = 1 if not shift_pressed else 10 elif key == 27: #esc is_exit = True next = True break elif filepath is not None: if chr_key == 'e': do_save_move_count = 1 if not shift_pressed else 10 elif chr_key == 'c': do_save_count = 1 if not shift_pressed else 10 elif chr_key == 'w': do_skip_move_count = 1 if not shift_pressed else 10 elif chr_key == 'x': do_skip_count = 1 if not shift_pressed else 10 if do_prev_count > 0: do_prev_count -= 1 if len(done_paths) > 0: image_paths.insert(0, filepath) filepath = done_paths.pop(-1) if filepath.parent != input_path: new_filename_path = input_path / filepath.name filepath.rename(new_filename_path) image_paths.insert(0, new_filename_path) else: image_paths.insert(0, filepath) next = True elif filepath is not None: if do_save_move_count > 0: do_save_move_count -= 1 ed.mask_finish() dflimg.embed_and_set(str(filepath), ie_polys=ed.get_ie_polys()) done_paths += [confirmed_path / filepath.name] filepath.rename(done_paths[-1]) next = True elif do_save_count > 0: do_save_count -= 1 ed.mask_finish() dflimg.embed_and_set(str(filepath), ie_polys=ed.get_ie_polys()) done_paths += [filepath] next = True elif do_skip_move_count > 0: do_skip_move_count -= 1 done_paths += [skipped_path / filepath.name] filepath.rename(done_paths[-1]) next = True elif do_skip_count > 0: do_skip_count -= 1 done_paths += [filepath] next = True else: do_save_move_count = do_save_count = do_skip_move_count = do_skip_count = 0 if do_prev_count + do_save_move_count + do_save_count + do_skip_move_count + do_skip_count == 0: if ed.switch_screen_changed(): io.show_image(wnd_name, ed.make_screen()) io.process_messages(0.005) io.destroy_all_windows()