From 432984b3b668110c8ff3bfe1d424a3b539603372 Mon Sep 17 00:00:00 2001 From: KRSHH <136873090+KRSHH@users.noreply.github.com> Date: Mon, 23 Dec 2024 09:41:17 +0530 Subject: [PATCH 1/6] Mac Fix Pygrabber Module import only on windows --- modules/ui.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/ui.py b/modules/ui.py index 25c7de0..1d09e8f 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -7,7 +7,6 @@ from cv2_enumerate_cameras import enumerate_cameras # Add this import from PIL import Image, ImageOps import time import json -from pygrabber.dshow_graph import FilterGraph import modules.globals import modules.metadata from modules.face_analyser import ( @@ -773,6 +772,8 @@ def webcam_preview(root: ctk.CTk, camera_index: int): def get_available_cameras(): """Returns a list of available camera names and indices.""" if platform.system() == "Windows": + from pygrabber.dshow_graph import FilterGraph + try: graph = FilterGraph() devices = graph.get_input_devices() From 5ce991651d71e54f7fdfedc9617b1241aa0f08f7 Mon Sep 17 00:00:00 2001 From: KRSHH <136873090+KRSHH@users.noreply.github.com> Date: Mon, 23 Dec 2024 09:46:59 +0530 Subject: [PATCH 2/6] Formatting Moved Windows only modules, to top too. --- modules/ui.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index 1d09e8f..16e7a9f 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -28,6 +28,9 @@ from modules.utilities import ( from modules.video_capture import VideoCapturer import platform +if platform.system() == "Windows": + from pygrabber.dshow_graph import FilterGraph + ROOT = None POPUP = None POPUP_LIVE = None @@ -772,8 +775,6 @@ def webcam_preview(root: ctk.CTk, camera_index: int): def get_available_cameras(): """Returns a list of available camera names and indices.""" if platform.system() == "Windows": - from pygrabber.dshow_graph import FilterGraph - try: graph = FilterGraph() devices = graph.get_input_devices() From ed7a21687c4de9f32659c30a17571ce568c30b47 Mon Sep 17 00:00:00 2001 From: Kenneth Estanislao Date: Mon, 23 Dec 2024 12:45:53 +0800 Subject: [PATCH 3/6] Update face_enhancer.py change if from before statement to elif, also fix conditional ladder --- modules/processors/frame/face_enhancer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/processors/frame/face_enhancer.py b/modules/processors/frame/face_enhancer.py index 72e5eff..fbc56e2 100644 --- a/modules/processors/frame/face_enhancer.py +++ b/modules/processors/frame/face_enhancer.py @@ -54,12 +54,12 @@ def get_face_enhancer() -> Any: with THREAD_LOCK: if FACE_ENHANCER is None: model_path = os.path.join(models_dir, "GFPGANv1.4.pth") - if platform.system() == "Darwin": # Mac OS + elif platform.system() == "Darwin": # Mac OS mps_device = None - if torch.backends.mps.is_available(): + elif torch.backends.mps.is_available(): mps_device = torch.device("mps") FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1, device=mps_device) # type: ignore[attr-defined] - else: # Other OS + elif: # Other OS FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined] return FACE_ENHANCER From 41c69162735df3e118534a6da05e2171db9e9562 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Mon, 23 Dec 2024 06:08:45 +0000 Subject: [PATCH 4/6] Revert "Update face_enhancer.py" This reverts commit ed7a21687c4de9f32659c30a17571ce568c30b47. --- modules/processors/frame/face_enhancer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/processors/frame/face_enhancer.py b/modules/processors/frame/face_enhancer.py index fbc56e2..72e5eff 100644 --- a/modules/processors/frame/face_enhancer.py +++ b/modules/processors/frame/face_enhancer.py @@ -54,12 +54,12 @@ def get_face_enhancer() -> Any: with THREAD_LOCK: if FACE_ENHANCER is None: model_path = os.path.join(models_dir, "GFPGANv1.4.pth") - elif platform.system() == "Darwin": # Mac OS + if platform.system() == "Darwin": # Mac OS mps_device = None - elif torch.backends.mps.is_available(): + if torch.backends.mps.is_available(): mps_device = torch.device("mps") FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1, device=mps_device) # type: ignore[attr-defined] - elif: # Other OS + else: # Other OS FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined] return FACE_ENHANCER From 7472dfb694546daadaf8e86415df5774d486f1c6 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Mon, 23 Dec 2024 06:29:36 +0000 Subject: [PATCH 5/6] fix: add match statement Added for optimization Co-Authored-By: Zephira --- modules/processors/frame/face_enhancer.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/processors/frame/face_enhancer.py b/modules/processors/frame/face_enhancer.py index 72e5eff..4e1fdff 100644 --- a/modules/processors/frame/face_enhancer.py +++ b/modules/processors/frame/face_enhancer.py @@ -54,13 +54,17 @@ def get_face_enhancer() -> Any: with THREAD_LOCK: if FACE_ENHANCER is None: model_path = os.path.join(models_dir, "GFPGANv1.4.pth") - if platform.system() == "Darwin": # Mac OS - mps_device = None - if torch.backends.mps.is_available(): - mps_device = torch.device("mps") - FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1, device=mps_device) # type: ignore[attr-defined] - else: # Other OS - FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined] + + match platform.system(): + case "Darwin": # Mac OS + if torch.backends.mps.is_available(): + mps_device = torch.device("mps") + FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1, device=mps_device) # type: ignore[attr-defined] + else: + FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined] + case _: # Other OS + FACE_ENHANCER = gfpgan.GFPGANer(model_path=model_path, upscale=1) # type: ignore[attr-defined] + return FACE_ENHANCER From 77c19d1073e72fa1cf26ad4b181c59fcf6a05cb9 Mon Sep 17 00:00:00 2001 From: KRSHH <136873090+KRSHH@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:58:43 +0530 Subject: [PATCH 6/6] FaceTime Camera Index to 0 --- modules/ui.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index 16e7a9f..33ded63 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -812,13 +812,29 @@ def get_available_cameras(): camera_indices = [] camera_names = [] - # Test the first 10 indices - for i in range(10): - cap = cv2.VideoCapture(i) + if platform.system() == "Darwin": # macOS specific handling + # Try to open the default FaceTime camera first + cap = cv2.VideoCapture(0) if cap.isOpened(): - camera_indices.append(i) - camera_names.append(f"Camera {i}") + camera_indices.append(0) + camera_names.append("FaceTime Camera") cap.release() + + # On macOS, additional cameras typically use indices 1 and 2 + for i in [1, 2]: + cap = cv2.VideoCapture(i) + if cap.isOpened(): + camera_indices.append(i) + camera_names.append(f"Camera {i}") + cap.release() + else: + # Linux camera detection - test first 10 indices + for i in range(10): + cap = cv2.VideoCapture(i) + if cap.isOpened(): + camera_indices.append(i) + camera_names.append(f"Camera {i}") + cap.release() if not camera_names: return [], ["No cameras found"]