parent
97a76c5e5b
commit
51b38fe253
|
@ -1378,43 +1378,42 @@ 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()
|
||||||
|
|
||||||
# Only clear and recreate widgets if they don't exist
|
# Clear any existing widgets in the PREVIEW window
|
||||||
if not hasattr(PREVIEW, "main_frame"):
|
for widget in PREVIEW.winfo_children():
|
||||||
for widget in PREVIEW.winfo_children():
|
widget.destroy()
|
||||||
widget.destroy()
|
|
||||||
|
|
||||||
PREVIEW.main_frame = ctk.CTkFrame(PREVIEW)
|
# Create a main frame to contain all widgets
|
||||||
PREVIEW.main_frame.pack(fill="both", expand=True)
|
main_frame = ctk.CTkFrame(PREVIEW)
|
||||||
|
main_frame.pack(fill="both", expand=True)
|
||||||
|
|
||||||
preview_frame = ctk.CTkFrame(PREVIEW.main_frame)
|
# Create a frame for the preview label
|
||||||
preview_frame.pack(fill="both", expand=True, padx=10, pady=10)
|
preview_frame = ctk.CTkFrame(main_frame)
|
||||||
|
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))
|
|
||||||
)
|
|
||||||
|
|
||||||
# FPS tracking variables
|
# Variables for source image and FPS calculation
|
||||||
|
source_image = None
|
||||||
prev_time = time.time()
|
prev_time = time.time()
|
||||||
fps_update_interval = 1.0 # Increased to 1 second for stability
|
fps_update_interval = 0.5
|
||||||
frame_count = 0
|
frame_count = 0
|
||||||
fps = 0
|
fps = 0
|
||||||
fps_display = "FPS: --" # Initialize fps display text
|
|
||||||
|
|
||||||
# Cache frequently accessed values
|
# Function to update frame size when the window is resized
|
||||||
live_mirror = modules.globals.live_mirror
|
def update_frame_size(event):
|
||||||
live_resizable = modules.globals.live_resizable
|
nonlocal temp_frame
|
||||||
show_fps = modules.globals.show_fps
|
if modules.globals.live_resizable:
|
||||||
map_faces = modules.globals.map_faces
|
temp_frame = fit_image_to_size(temp_frame, event.width, event.height)
|
||||||
|
|
||||||
|
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:
|
||||||
|
@ -1422,36 +1421,46 @@ def create_webcam_preview(camera_index: int):
|
||||||
|
|
||||||
temp_frame = frame.copy()
|
temp_frame = frame.copy()
|
||||||
|
|
||||||
if live_mirror:
|
# Apply mirroring if enabled
|
||||||
|
if modules.globals.live_mirror:
|
||||||
temp_frame = cv2.flip(temp_frame, 1)
|
temp_frame = cv2.flip(temp_frame, 1)
|
||||||
|
|
||||||
if live_resizable:
|
# Resize frame if enabled
|
||||||
|
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
|
# Process frame based on face mapping mode
|
||||||
if not map_faces:
|
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)
|
||||||
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)
|
||||||
|
|
||||||
# FPS calculation and display
|
# Calculate and display FPS
|
||||||
frame_count += 1
|
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
|
frame_count += 1
|
||||||
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
|
||||||
|
|
||||||
# Always show the last calculated FPS value
|
if modules.globals.show_fps:
|
||||||
if show_fps:
|
|
||||||
cv2.putText(
|
cv2.putText(
|
||||||
temp_frame,
|
temp_frame,
|
||||||
fps_display, # Use stored fps display text
|
f"FPS: {fps:.1f}",
|
||||||
(10, 30),
|
(10, 30),
|
||||||
cv2.FONT_HERSHEY_SIMPLEX,
|
cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
1,
|
1,
|
||||||
|
@ -1459,8 +1468,9 @@ def create_webcam_preview(camera_index: int):
|
||||||
2,
|
2,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Display frame
|
# Convert frame to RGB and display in preview label
|
||||||
image = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB))
|
image = 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
|
||||||
)
|
)
|
||||||
|
@ -1468,6 +1478,7 @@ 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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue