Update ui.py

Added:
- try-finally Block: This makes sure the camera.release() is called no matter how the while loops end.
- Resource Cleanup: The finally block takes care of cleaning up resources to keep the application stable.
This commit is contained in:
Makaru 2025-01-05 20:19:36 +08:00 committed by GitHub
parent 22940d1b99
commit 742bcab130
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -795,74 +795,76 @@ def create_webcam_preview(camera_index: int):
frame_count = 0 frame_count = 0
fps = 0 fps = 0
while camera: try:
ret, frame = camera.read() while camera:
if not ret: ret, frame = camera.read()
break if not ret:
break
temp_frame = frame.copy() temp_frame = frame.copy()
if modules.globals.live_mirror: if modules.globals.live_mirror:
temp_frame = cv2.flip(temp_frame, 1) temp_frame = cv2.flip(temp_frame, 1)
if modules.globals.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()
) )
if not modules.globals.map_faces: if not modules.globals.map_faces:
if source_image is None and modules.globals.source_path: if source_image is None and modules.globals.source_path:
source_image = get_one_face(cv2.imread(modules.globals.source_path)) source_image = get_one_face(cv2.imread(modules.globals.source_path))
for frame_processor in frame_processors: for frame_processor in frame_processors:
if frame_processor.NAME == "DLC.FACE-ENHANCER": if frame_processor.NAME == "DLC.FACE-ENHANCER":
if modules.globals.fp_ui["face_enhancer"]: if modules.globals.fp_ui["face_enhancer"]:
temp_frame = frame_processor.process_frame(None, temp_frame) temp_frame = frame_processor.process_frame(None, temp_frame)
else: else:
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 modules.globals.target_path = None
for frame_processor in frame_processors: for frame_processor in frame_processors:
if frame_processor.NAME == "DLC.FACE-ENHANCER": if frame_processor.NAME == "DLC.FACE-ENHANCER":
if modules.globals.fp_ui["face_enhancer"]: if modules.globals.fp_ui["face_enhancer"]:
temp_frame = frame_processor.process_frame_v2(temp_frame)
else:
temp_frame = frame_processor.process_frame_v2(temp_frame) temp_frame = frame_processor.process_frame_v2(temp_frame)
else:
temp_frame = frame_processor.process_frame_v2(temp_frame)
# Calculate and display FPS # Calculate and display FPS
current_time = time.time() current_time = time.time()
frame_count += 1 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)
frame_count = 0 frame_count = 0
prev_time = current_time prev_time = current_time
if modules.globals.show_fps: if modules.globals.show_fps:
cv2.putText( cv2.putText(
temp_frame, temp_frame,
f"FPS: {fps:.1f}", f"FPS: {fps:.1f}",
(10, 30), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_SIMPLEX,
1, 1,
(0, 255, 0), (0, 255, 0),
2, 2,
)
image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
image = ImageOps.contain(
image, (temp_frame.shape[1], temp_frame.shape[0]), Image.LANCZOS
) )
image = ctk.CTkImage(image, size=image.size)
preview_label.configure(image=image)
ROOT.update()
image = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) if PREVIEW.state() == "withdrawn":
image = Image.fromarray(image) break
image = ImageOps.contain( finally:
image, (temp_frame.shape[1], temp_frame.shape[0]), Image.LANCZOS camera.release()
) PREVIEW.withdraw()
image = ctk.CTkImage(image, size=image.size)
preview_label.configure(image=image)
ROOT.update()
if PREVIEW.state() == "withdrawn":
break
camera.release()
PREVIEW.withdraw()
def create_source_target_popup_for_webcam( def create_source_target_popup_for_webcam(