Compare commits
5 Commits
c74972ce0e
...
a1e43c72cc
Author | SHA1 | Date |
---|---|---|
|
a1e43c72cc | |
|
fc86365a90 | |
|
1dd0e8e509 | |
|
4e0ff540f0 | |
|
c1a6dc693d |
|
@ -799,73 +799,39 @@ def webcam_preview(root: ctk.CTk, camera_index: int):
|
||||||
|
|
||||||
|
|
||||||
def get_available_cameras():
|
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":
|
if platform.system() == "Windows":
|
||||||
try:
|
try:
|
||||||
|
from pygrabber.dshow_graph import FilterGraph
|
||||||
graph = FilterGraph()
|
graph = FilterGraph()
|
||||||
devices = graph.get_input_devices()
|
devices = graph.get_input_devices()
|
||||||
|
|
||||||
# Create list of indices and names
|
|
||||||
camera_indices = list(range(len(devices)))
|
camera_indices = list(range(len(devices)))
|
||||||
camera_names = 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:
|
if not camera_names:
|
||||||
return [], ["No cameras found"]
|
return [], ["No cameras found"]
|
||||||
|
|
||||||
return camera_indices, camera_names
|
return camera_indices, camera_names
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error detecting cameras: {str(e)}")
|
print(f"[Camera Detection Error - Windows]: {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:
|
|
||||||
return [], ["No cameras found"]
|
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):
|
def create_webcam_preview(camera_index: int):
|
||||||
|
|
|
@ -4,7 +4,7 @@ numpy>=1.23.5,<2
|
||||||
typing-extensions>=4.8.0
|
typing-extensions>=4.8.0
|
||||||
opencv-python==4.10.0.84
|
opencv-python==4.10.0.84
|
||||||
cv2_enumerate_cameras==1.1.15
|
cv2_enumerate_cameras==1.1.15
|
||||||
onnx==1.16.0
|
onnx==1.18.0
|
||||||
insightface==0.7.3
|
insightface==0.7.3
|
||||||
psutil==5.9.8
|
psutil==5.9.8
|
||||||
tk==0.1.0
|
tk==0.1.0
|
||||||
|
@ -15,7 +15,7 @@ torch==2.5.1; sys_platform == 'darwin'
|
||||||
torchvision; sys_platform != 'darwin'
|
torchvision; sys_platform != 'darwin'
|
||||||
torchvision==0.20.1; sys_platform == 'darwin'
|
torchvision==0.20.1; sys_platform == 'darwin'
|
||||||
onnxruntime-silicon==1.21.0; sys_platform == 'darwin' and platform_machine == 'arm64'
|
onnxruntime-silicon==1.21.0; sys_platform == 'darwin' and platform_machine == 'arm64'
|
||||||
onnxruntime-gpu==1.21.0; sys_platform != 'darwin'
|
onnxruntime-gpu==1.22.0; sys_platform != 'darwin'
|
||||||
tensorflow; sys_platform != 'darwin'
|
tensorflow; sys_platform != 'darwin'
|
||||||
opennsfw2==0.10.2
|
opennsfw2==0.10.2
|
||||||
protobuf==4.23.2
|
protobuf==4.25.1
|
||||||
|
|
Loading…
Reference in New Issue