Slight Performance Improvement

avg 9.5 to avg 10
pull/746/head
KRSHH 2024-10-29 14:07:47 +05:30
parent a581b81bd9
commit 9a472e2435
1 changed files with 36 additions and 47 deletions

View File

@ -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_FRAME_HEIGHT, PREVIEW_DEFAULT_HEIGHT)
camera.set(cv2.CAP_PROP_FPS, 60) camera.set(cv2.CAP_PROP_FPS, 60)
# Simplified window setup - reduce widget creation/destruction
PREVIEW.deiconify() PREVIEW.deiconify()
# Clear any existing widgets in the PREVIEW window # Only clear and recreate widgets if they don't exist
for widget in PREVIEW.winfo_children(): if not hasattr(PREVIEW, "main_frame"):
widget.destroy() for widget in PREVIEW.winfo_children():
widget.destroy()
# Create a main frame to contain all widgets PREVIEW.main_frame = ctk.CTkFrame(PREVIEW)
main_frame = ctk.CTkFrame(PREVIEW) PREVIEW.main_frame.pack(fill="both", expand=True)
main_frame.pack(fill="both", expand=True)
# Create a frame for the preview label preview_frame = ctk.CTkFrame(PREVIEW.main_frame)
preview_frame = ctk.CTkFrame(main_frame) preview_frame.pack(fill="both", expand=True, padx=10, pady=10)
preview_frame.pack(fill="both", expand=True, padx=10, pady=10)
preview_label = ctk.CTkLabel(preview_frame, text="") preview_label = ctk.CTkLabel(preview_frame, text="")
preview_label.pack(fill="both", expand=True) preview_label.pack(fill="both", expand=True)
# Initialize frame processors
frame_processors = get_frame_processors_modules(modules.globals.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 # FPS tracking variables
source_image = None
prev_time = time.time() prev_time = time.time()
fps_update_interval = 0.5 fps_update_interval = 1.0 # Increased to 1 second for stability
frame_count = 0 frame_count = 0
fps = 0 fps = 0
fps_display = "FPS: --" # Initialize fps display text
# Function to update frame size when the window is resized # Cache frequently accessed values
def update_frame_size(event): live_mirror = modules.globals.live_mirror
nonlocal temp_frame live_resizable = modules.globals.live_resizable
if modules.globals.live_resizable: show_fps = modules.globals.show_fps
temp_frame = fit_image_to_size(temp_frame, event.width, event.height) map_faces = modules.globals.map_faces
preview_frame.bind("<Configure>", update_frame_size)
# Main loop for capturing and processing frames
while camera.isOpened() and PREVIEW.state() != "withdrawn": while camera.isOpened() and PREVIEW.state() != "withdrawn":
ret, frame = camera.read() ret, frame = camera.read()
if not ret: if not ret:
@ -1421,46 +1422,36 @@ def create_webcam_preview(camera_index: int):
temp_frame = frame.copy() temp_frame = frame.copy()
# Apply mirroring if enabled if live_mirror:
if modules.globals.live_mirror:
temp_frame = cv2.flip(temp_frame, 1) temp_frame = cv2.flip(temp_frame, 1)
# Resize frame if enabled if live_resizable:
if modules.globals.live_resizable:
temp_frame = fit_image_to_size( temp_frame = fit_image_to_size(
temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height()
) )
# Process frame based on face mapping mode # Process frame
if not modules.globals.map_faces: if not 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)
for frame_processor in frame_processors: for frame_processor in frame_processors:
temp_frame = frame_processor.process_frame(source_image, temp_frame) temp_frame = frame_processor.process_frame(source_image, temp_frame)
else: else:
modules.globals.target_path = None
for frame_processor in frame_processors: for frame_processor in frame_processors:
temp_frame = frame_processor.process_frame_v2(temp_frame) temp_frame = frame_processor.process_frame_v2(temp_frame)
# Calculate and display FPS # FPS calculation and display
current_time = time.time()
frame_count += 1 frame_count += 1
current_time = time.time()
if current_time - prev_time >= fps_update_interval: if current_time - prev_time >= fps_update_interval:
fps = frame_count / (current_time - prev_time) fps = frame_count / (current_time - prev_time)
fps_display = f"FPS: {fps:.1f}" # Update display text
frame_count = 0 frame_count = 0
prev_time = current_time prev_time = current_time
if modules.globals.show_fps: # Always show the last calculated FPS value
if show_fps:
cv2.putText( cv2.putText(
temp_frame, temp_frame,
f"FPS: {fps:.1f}", fps_display, # Use stored fps display text
(10, 30), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_SIMPLEX,
1, 1,
@ -1468,9 +1459,8 @@ def create_webcam_preview(camera_index: int):
2, 2,
) )
# Convert frame to RGB and display in preview label # Display frame
image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB))
image = Image.fromarray(image)
image = ImageOps.contain( image = ImageOps.contain(
image, (temp_frame.shape[1], temp_frame.shape[0]), Image.LANCZOS 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) preview_label.configure(image=image)
ROOT.update() ROOT.update()
# Release camera and close preview window
camera.release() camera.release()
PREVIEW.withdraw() PREVIEW.withdraw()