From 2d34201cfcb399141fbf1a691f505223f14743b8 Mon Sep 17 00:00:00 2001 From: Highpressure <1310485+Highpressure@users.noreply.github.com> Date: Fri, 6 Sep 2024 20:58:38 +0200 Subject: [PATCH] Update ui.py added dropdown for multi camera device selection --- modules/ui.py | 63 +++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index 1d91253..e744a0d 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -42,6 +42,16 @@ def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk: PREVIEW = create_preview(ROOT) return ROOT + +def list_available_cameras(max_tested: int = 10): + """ List all available camera indices. """ + available_cameras = [] + for i in range(max_tested): + cap = cv2.VideoCapture(i) + if cap.isOpened(): + available_cameras.append(i) + cap.release() + return available_cameras def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk: @@ -67,40 +77,21 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C select_face_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1) swap_faces_button = ctk.CTkButton(root, text='↔', cursor='hand2', command=lambda: swap_faces_paths()) - swap_faces_button.place(relx=0.45, rely=0.4, relwidth=0.1, relheight=0.1) + swap_faces_button.place(relx=0.45, relwidth=0.1, relheight=0.1) select_target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path()) select_target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1) - keep_fps_value = ctk.BooleanVar(value=modules.globals.keep_fps) - keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_fps', not modules.globals.keep_fps)) - keep_fps_checkbox.place(relx=0.1, rely=0.6) - - keep_frames_value = ctk.BooleanVar(value=modules.globals.keep_frames) - keep_frames_switch = ctk.CTkSwitch(root, text='Keep frames', variable=keep_frames_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_frames', keep_frames_value.get())) - keep_frames_switch.place(relx=0.1, rely=0.65) - - # for FRAME PROCESSOR ENHANCER tumbler: - enhancer_value = ctk.BooleanVar(value=modules.globals.fp_ui['face_enhancer']) - enhancer_switch = ctk.CTkSwitch(root, text='Face Enhancer', variable=enhancer_value, cursor='hand2', command=lambda: update_tumbler('face_enhancer',enhancer_value.get())) - enhancer_switch.place(relx=0.1, rely=0.7) - - keep_audio_value = ctk.BooleanVar(value=modules.globals.keep_audio) - keep_audio_switch = ctk.CTkSwitch(root, text='Keep audio', variable=keep_audio_value, cursor='hand2', command=lambda: setattr(modules.globals, 'keep_audio', keep_audio_value.get())) - keep_audio_switch.place(relx=0.6, rely=0.6) - - many_faces_value = ctk.BooleanVar(value=modules.globals.many_faces) - many_faces_switch = ctk.CTkSwitch(root, text='Many faces', variable=many_faces_value, cursor='hand2', command=lambda: setattr(modules.globals, 'many_faces', many_faces_value.get())) - many_faces_switch.place(relx=0.6, rely=0.65) - - # Add color correction toggle button - color_correction_value = ctk.BooleanVar(value=modules.globals.color_correction) - color_correction_switch = ctk.CTkSwitch(root, text='Fix Blueish Cam\n(force cv2 to use RGB instead of BGR)', variable=color_correction_value, cursor='hand2', command=lambda: setattr(modules.globals, 'color_correction', color_correction_value.get())) - color_correction_switch.place(relx=0.6, rely=0.70) - -# nsfw_value = ctk.BooleanVar(value=modules.globals.nsfw_filter) -# nsfw_switch = ctk.CTkSwitch(root, text='NSFW filter', variable=nsfw_value, cursor='hand2', command=lambda: setattr(modules.globals, 'nsfw_filter', nsfw_value.get())) -# nsfw_switch.place(relx=0.6, rely=0.7) + # Create dropdown for camera selection + available_cameras = list_available_cameras() + if available_cameras: + camera_var = ctk.StringVar(value=str(available_cameras[0])) # Default to the first available camera + camera_dropdown = ctk.CTkOptionMenu(root, variable=camera_var, values=[str(i) for i in available_cameras]) + camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05) + else: + camera_var = ctk.StringVar(value="No cameras found") + camera_dropdown = ctk.CTkLabel(root, text="No cameras found") + camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05) start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start)) start_button.place(relx=0.15, rely=0.80, relwidth=0.2, relheight=0.05) @@ -111,7 +102,7 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview()) preview_button.place(relx=0.65, rely=0.80, relwidth=0.2, relheight=0.05) - live_button = ctk.CTkButton(root, text='Live', cursor='hand2', command=lambda: webcam_preview()) + live_button = ctk.CTkButton(root, text='Live', cursor='hand2', command=lambda: webcam_preview(int(camera_var.get()))) live_button.place(relx=0.40, rely=0.86, relwidth=0.2, relheight=0.05) status_label = ctk.CTkLabel(root, text=None, justify='center') @@ -125,6 +116,8 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C return root + + def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel: global preview_label, preview_slider @@ -315,14 +308,14 @@ def update_preview(frame_number: int = 0) -> None: update_status('Processing succeed!') PREVIEW.deiconify() -def webcam_preview(): +def webcam_preview(camera_index=0): if modules.globals.source_path is None: # No image selected return global preview_label, PREVIEW - camera = cv2.VideoCapture(0) # Use index for the webcam (adjust the index accordingly if necessary) + camera = cv2.VideoCapture(camera_index) # Use the selected camera index camera.set(cv2.CAP_PROP_FRAME_WIDTH, PREVIEW_DEFAULT_WIDTH) # Set the width of the resolution camera.set(cv2.CAP_PROP_FRAME_HEIGHT, PREVIEW_DEFAULT_HEIGHT) # Set the height of the resolution camera.set(cv2.CAP_PROP_FPS, 60) # Set the frame rate of the webcam @@ -344,10 +337,10 @@ def webcam_preview(): if source_image is None and modules.globals.source_path: source_image = get_one_face(cv2.imread(modules.globals.source_path)) - temp_frame = frame.copy() #Create a copy of the frame + temp_frame = frame.copy() # Create a copy of the frame if modules.globals.live_mirror: - temp_frame = cv2.flip(temp_frame, 1) # horizontal flipping + temp_frame = cv2.flip(temp_frame, 1) # Horizontal flipping if modules.globals.live_resizable: temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height())