Update ui.py

added dropdown for multi camera device selection
pull/556/head
Highpressure 2024-09-06 20:58:38 +02:00 committed by GitHub
parent f762b61a12
commit 2d34201cfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 35 deletions

View File

@ -42,6 +42,16 @@ def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
PREVIEW = create_preview(ROOT) PREVIEW = create_preview(ROOT)
return 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: 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) 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 = 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 = 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) 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) # Create dropdown for camera selection
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)) available_cameras = list_available_cameras()
keep_fps_checkbox.place(relx=0.1, rely=0.6) if available_cameras:
camera_var = ctk.StringVar(value=str(available_cameras[0])) # Default to the first available camera
keep_frames_value = ctk.BooleanVar(value=modules.globals.keep_frames) camera_dropdown = ctk.CTkOptionMenu(root, variable=camera_var, values=[str(i) for i in available_cameras])
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())) camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05)
keep_frames_switch.place(relx=0.1, rely=0.65) else:
camera_var = ctk.StringVar(value="No cameras found")
# for FRAME PROCESSOR ENHANCER tumbler: camera_dropdown = ctk.CTkLabel(root, text="No cameras found")
enhancer_value = ctk.BooleanVar(value=modules.globals.fp_ui['face_enhancer']) camera_dropdown.place(relx=0.1, rely=0.55, relwidth=0.5, relheight=0.05)
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)
start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start)) 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) 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 = 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) 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) live_button.place(relx=0.40, rely=0.86, relwidth=0.2, relheight=0.05)
status_label = ctk.CTkLabel(root, text=None, justify='center') 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 return root
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel: def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
global preview_label, preview_slider global preview_label, preview_slider
@ -315,14 +308,14 @@ def update_preview(frame_number: int = 0) -> None:
update_status('Processing succeed!') update_status('Processing succeed!')
PREVIEW.deiconify() PREVIEW.deiconify()
def webcam_preview(): def webcam_preview(camera_index=0):
if modules.globals.source_path is None: if modules.globals.source_path is None:
# No image selected # No image selected
return return
global preview_label, PREVIEW 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_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_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 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: 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))
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: 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: if modules.globals.live_resizable:
temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height()) temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height())