diff --git a/modules/ui.py b/modules/ui.py index 0fc8a0f..b637dc6 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -795,74 +795,76 @@ def create_webcam_preview(camera_index: int): frame_count = 0 fps = 0 - while camera: - ret, frame = camera.read() - if not ret: - break + try: + while camera: + ret, frame = camera.read() + if not ret: + break - temp_frame = frame.copy() + temp_frame = frame.copy() - if modules.globals.live_mirror: - temp_frame = cv2.flip(temp_frame, 1) + if modules.globals.live_mirror: + temp_frame = cv2.flip(temp_frame, 1) - if modules.globals.live_resizable: - temp_frame = fit_image_to_size( - temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() - ) + if modules.globals.live_resizable: + temp_frame = fit_image_to_size( + temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() + ) - if not modules.globals.map_faces: - if source_image is None and modules.globals.source_path: - source_image = get_one_face(cv2.imread(modules.globals.source_path)) + if not modules.globals.map_faces: + if source_image is None and modules.globals.source_path: + source_image = get_one_face(cv2.imread(modules.globals.source_path)) - for frame_processor in frame_processors: - if frame_processor.NAME == "DLC.FACE-ENHANCER": - if modules.globals.fp_ui["face_enhancer"]: - temp_frame = frame_processor.process_frame(None, temp_frame) - else: - temp_frame = frame_processor.process_frame(source_image, temp_frame) - else: - modules.globals.target_path = None + for frame_processor in frame_processors: + if frame_processor.NAME == "DLC.FACE-ENHANCER": + if modules.globals.fp_ui["face_enhancer"]: + temp_frame = frame_processor.process_frame(None, temp_frame) + else: + temp_frame = frame_processor.process_frame(source_image, temp_frame) + else: + modules.globals.target_path = None - for frame_processor in frame_processors: - if frame_processor.NAME == "DLC.FACE-ENHANCER": - if modules.globals.fp_ui["face_enhancer"]: + for frame_processor in frame_processors: + if frame_processor.NAME == "DLC.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) - else: - temp_frame = frame_processor.process_frame_v2(temp_frame) - # Calculate and display FPS - current_time = time.time() - frame_count += 1 - if current_time - prev_time >= fps_update_interval: - fps = frame_count / (current_time - prev_time) - frame_count = 0 - prev_time = current_time + # Calculate and display FPS + current_time = time.time() + frame_count += 1 + if current_time - prev_time >= fps_update_interval: + fps = frame_count / (current_time - prev_time) + frame_count = 0 + prev_time = current_time - if modules.globals.show_fps: - cv2.putText( - temp_frame, - f"FPS: {fps:.1f}", - (10, 30), - cv2.FONT_HERSHEY_SIMPLEX, - 1, - (0, 255, 0), - 2, + if modules.globals.show_fps: + cv2.putText( + temp_frame, + f"FPS: {fps:.1f}", + (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, + 1, + (0, 255, 0), + 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) - 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() + if PREVIEW.state() == "withdrawn": + break + finally: + camera.release() + PREVIEW.withdraw() - if PREVIEW.state() == "withdrawn": - break - - camera.release() - PREVIEW.withdraw() def create_source_target_popup_for_webcam(