From 9a472e2435794af6c1e980e59538f9f8263ff0d6 Mon Sep 17 00:00:00 2001 From: KRSHH <136873090+KRSHH@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:07:47 +0530 Subject: [PATCH] Slight Performance Improvement avg 9.5 to avg 10 --- modules/ui.py | 83 ++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index d712ee1..a748e8c 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -1378,42 +1378,43 @@ def create_webcam_preview(camera_index: int): camera.set(cv2.CAP_PROP_FRAME_HEIGHT, PREVIEW_DEFAULT_HEIGHT) camera.set(cv2.CAP_PROP_FPS, 60) + # Simplified window setup - reduce widget creation/destruction PREVIEW.deiconify() - # Clear any existing widgets in the PREVIEW window - for widget in PREVIEW.winfo_children(): - widget.destroy() + # Only clear and recreate widgets if they don't exist + if not hasattr(PREVIEW, "main_frame"): + for widget in PREVIEW.winfo_children(): + widget.destroy() - # Create a main frame to contain all widgets - main_frame = ctk.CTkFrame(PREVIEW) - main_frame.pack(fill="both", expand=True) + PREVIEW.main_frame = ctk.CTkFrame(PREVIEW) + PREVIEW.main_frame.pack(fill="both", expand=True) - # Create a frame for the preview label - preview_frame = ctk.CTkFrame(main_frame) - preview_frame.pack(fill="both", expand=True, padx=10, pady=10) + preview_frame = ctk.CTkFrame(PREVIEW.main_frame) + preview_frame.pack(fill="both", expand=True, padx=10, pady=10) - preview_label = ctk.CTkLabel(preview_frame, text="") - preview_label.pack(fill="both", expand=True) + preview_label = ctk.CTkLabel(preview_frame, text="") + preview_label.pack(fill="both", expand=True) - # Initialize frame processors frame_processors = get_frame_processors_modules(modules.globals.frame_processors) + source_image = ( + None + if not modules.globals.source_path + else get_one_face(cv2.imread(modules.globals.source_path)) + ) - # Variables for source image and FPS calculation - source_image = None + # FPS tracking variables prev_time = time.time() - fps_update_interval = 0.5 + fps_update_interval = 1.0 # Increased to 1 second for stability frame_count = 0 fps = 0 + fps_display = "FPS: --" # Initialize fps display text - # Function to update frame size when the window is resized - def update_frame_size(event): - nonlocal temp_frame - if modules.globals.live_resizable: - temp_frame = fit_image_to_size(temp_frame, event.width, event.height) + # Cache frequently accessed values + live_mirror = modules.globals.live_mirror + live_resizable = modules.globals.live_resizable + show_fps = modules.globals.show_fps + map_faces = modules.globals.map_faces - preview_frame.bind("", update_frame_size) - - # Main loop for capturing and processing frames while camera.isOpened() and PREVIEW.state() != "withdrawn": ret, frame = camera.read() if not ret: @@ -1421,46 +1422,36 @@ def create_webcam_preview(camera_index: int): temp_frame = frame.copy() - # Apply mirroring if enabled - if modules.globals.live_mirror: + if live_mirror: temp_frame = cv2.flip(temp_frame, 1) - # Resize frame if enabled - if modules.globals.live_resizable: + if live_resizable: temp_frame = fit_image_to_size( temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() ) - # Process frame based on face mapping mode - if not modules.globals.map_faces: - # Update source image if path has changed - if modules.globals.source_path and ( - source_image is None - or modules.globals.source_path != source_image["location"] - ): - source_image = get_one_face(cv2.imread(modules.globals.source_path)) - source_image["location"] = modules.globals.source_path - - # Apply frame processors (e.g., face swapping, enhancement) + # Process frame + if not map_faces: for frame_processor in frame_processors: temp_frame = frame_processor.process_frame(source_image, temp_frame) else: - modules.globals.target_path = None for frame_processor in frame_processors: temp_frame = frame_processor.process_frame_v2(temp_frame) - # Calculate and display FPS - current_time = time.time() + # FPS calculation and display frame_count += 1 + current_time = time.time() if current_time - prev_time >= fps_update_interval: fps = frame_count / (current_time - prev_time) + fps_display = f"FPS: {fps:.1f}" # Update display text frame_count = 0 prev_time = current_time - if modules.globals.show_fps: + # Always show the last calculated FPS value + if show_fps: cv2.putText( temp_frame, - f"FPS: {fps:.1f}", + fps_display, # Use stored fps display text (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, @@ -1468,9 +1459,8 @@ def create_webcam_preview(camera_index: int): 2, ) - # Convert frame to RGB and display in preview label - image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) - image = Image.fromarray(image) + # Display frame + image = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB)) image = ImageOps.contain( image, (temp_frame.shape[1], temp_frame.shape[0]), Image.LANCZOS ) @@ -1478,7 +1468,6 @@ def create_webcam_preview(camera_index: int): preview_label.configure(image=image) ROOT.update() - # Release camera and close preview window camera.release() PREVIEW.withdraw()