Compare commits

...

3 Commits

Author SHA1 Message Date
Bryn Whyman 5fbae9b0e3
Merge c1a6dc693d into d0d90ecc03 2025-08-05 22:53:59 +01:00
Kenneth Estanislao d0d90ecc03 Creating a fallback and switching of models
Models switch depending on the execution provider
2025-08-02 02:56:20 +08:00
Bryn Whyman c1a6dc693d Fix: Safe camera detection for macOS (M1/M2) to prevent OpenCV AVX/thread crashes 2025-05-08 20:40:22 +12:00
3 changed files with 32 additions and 61 deletions

View File

@ -1,3 +1,3 @@
name = 'Deep-Live-Cam'
version = '1.8'
version = '1.8.1'
edition = 'GitHub Edition'

View File

@ -28,12 +28,14 @@ models_dir = os.path.join(
def pre_check() -> bool:
download_directory_path = abs_dir
download_directory_path = models_dir
model_url = "https://huggingface.co/hacksider/deep-live-cam/resolve/main/inswapper_128.onnx"
if "CUDAExecutionProvider" in modules.globals.execution_providers:
model_url = "https://huggingface.co/hacksider/deep-live-cam/resolve/main/inswapper_128_fp16.onnx"
conditional_download(
download_directory_path,
[
"https://huggingface.co/hacksider/deep-live-cam/blob/main/inswapper_128_fp16.onnx"
],
[model_url],
)
return True
@ -60,7 +62,10 @@ def get_face_swapper() -> Any:
with THREAD_LOCK:
if FACE_SWAPPER is None:
model_path = os.path.join(models_dir, "inswapper_128_fp16.onnx")
model_name = "inswapper_128.onnx"
if "CUDAExecutionProvider" in modules.globals.execution_providers:
model_name = "inswapper_128_fp16.onnx"
model_path = os.path.join(models_dir, model_name)
FACE_SWAPPER = insightface.model_zoo.get_model(
model_path, providers=modules.globals.execution_providers
)

View File

@ -799,73 +799,39 @@ def webcam_preview(root: ctk.CTk, camera_index: int):
def get_available_cameras():
"""Returns a list of available camera names and indices."""
"""
Safe camera detection for macOS and Unix-like systems that avoids threading and AVX crashes.
Returns a tuple of (camera_indices, camera_names).
"""
import cv2
import platform
if platform.system() == "Windows":
try:
from pygrabber.dshow_graph import FilterGraph
graph = FilterGraph()
devices = graph.get_input_devices()
# Create list of indices and names
camera_indices = list(range(len(devices)))
camera_names = devices
# If no cameras found through DirectShow, try OpenCV fallback
if not camera_names:
# Try to open camera with index -1 and 0
test_indices = [-1, 0]
working_cameras = []
for idx in test_indices:
cap = cv2.VideoCapture(idx)
if cap.isOpened():
working_cameras.append(f"Camera {idx}")
cap.release()
if working_cameras:
return test_indices[: len(working_cameras)], working_cameras
# If still no cameras found, return empty lists
if not camera_names:
return [], ["No cameras found"]
return camera_indices, camera_names
except Exception as e:
print(f"Error detecting cameras: {str(e)}")
return [], ["No cameras found"]
else:
# Unix-like systems (Linux/Mac) camera detection
camera_indices = []
camera_names = []
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(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:
print(f"[Camera Detection Error - Windows]: {e}")
return [], ["No cameras found"]
return camera_indices, camera_names
# macOS or Linux
try:
print("[Info] Safely checking for available cameras...")
cap = cv2.VideoCapture(0)
if cap is None or not cap.isOpened():
print("[Warning] Default camera (index 0) not available.")
return [], ["No cameras found"]
cap.release()
return [0], ["Default Camera (Index 0)"]
except Exception as e:
print(f"[Camera Detection Error - Unix]: {e}")
return [], ["No cameras found"]
def create_webcam_preview(camera_index: int):