Deep-Live-Cam
Deep-Live-Cam copied to clipboard
Fix: Safe camera detection for macOS (M1/M2) to prevent OpenCV AVX/th…
This PR addresses a crash I had to work around when launching on Apple Silicon (M1/M2/M3). The issue stemmed from OpenCV's threaded camera enumeration causing a fatal error due to AVX instruction mismatches or GIL violations.
- Replaces the original
get_available_cameras()logic with a safe fallback for macOS that avoids multithreaded access and AVX instruction usage. - Adds platform-specific logging to help users understand what's detected.
- Retains the original Windows-specific logic (via
pygrabber) for full cross-platform compatibility.
Tested on:
- macOS 14.4.1 (Apple M2)
- Python 3.10
- Camera detection succeeded and GUI preview works without crash
Let me know if you'd like help extending this further. Thanks!
Summary by Sourcery
Implement a safe camera detection mechanism for macOS and Unix-like systems to prevent crashes related to OpenCV threading and AVX instructions
New Features:
- Implemented a safe fallback camera detection method for macOS and Unix systems
Bug Fixes:
- Fixed camera detection crash on Apple Silicon (M1/M2/M3) platforms
- Resolved OpenCV threading and AVX instruction compatibility issues on macOS
Enhancements:
- Simplified camera detection logic for non-Windows platforms
- Added platform-specific logging for camera detection
Reviewer's Guide
This pull request modifies the get_available_cameras() function in modules/ui.py to prevent crashes on Apple Silicon (macOS) by introducing a simplified camera detection strategy. For macOS and other Unix-like systems, it now exclusively attempts to access the default camera (index 0) via cv2.VideoCapture(0), thereby avoiding multithreaded operations and potential AVX instruction issues inherent in the previous, more exhaustive camera probing. The Windows-specific camera detection using pygrabber is largely retained, but its OpenCV fallback for cases where no cameras are found by pygrabber has been removed. Logging for camera detection has also been updated to be more platform-specific.
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Implemented a safer, simplified camera detection method for macOS and Linux systems. |
|
modules/ui.py |
| Streamlined the camera detection process for Windows. |
|
modules/ui.py |
| Enhanced diagnostic logging during camera detection. |
|
modules/ui.py |
Refactored import statements within get_available_cameras. |
|
modules/ui.py |
Possibly linked issues
- #-1: PR changes camera detection on macOS to fix OpenCV initialization failure reported in the issue.
- #0: The PR fixes the webcam detection problem on Apple Silicon described in the issue by changing the camera detection logic.
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
- Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with
@sourcery-ai issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore. - Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
- Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Likely solves this issue too: https://github.com/hacksider/Deep-Live-Cam/issues/1166
Should I make the changes suggested by the bot?
tryed on M4 and seem that didn't work.
Seems the camera is loading but the UI doesn't show anything
Log: DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning. [Info] Safely checking for available cameras...
Version apple: 15.5 Python 3.10.13
Solved: Just use python 3.10.16
Now I have the fps problems
@arturo-lr-dev thanks for testing.
Now I have the fps problems
Is this something that exists when trying to install from the main branch too? Wondering if it's an issue with this PR or not
@brynwhyman it happens with both branches, i think is not a problem of this branch
tryed on M4 and seem that didn't work.
Seems the camera is loading but the UI doesn't show anything
Log: DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning. [Info] Safely checking for available cameras...
Version apple: 15.5 Python 3.10.13
Solved: Just use python 3.10.16
Now I have the fps problems
Hi, i am using the same python version and the same mac version. I got blank view of the UI
the log doesnt show any error (except the SSL)
(venv) ➜ Deep-Live-Cam git:(main) ✗ python3.10 run.py --execution-provider coreml
/Users/ridhoperdana/workspace/Deep-Live-Cam/venv/lib/python3.10/site-packages/albumentations/check_version.py:147: UserWarning: Error fetching version info <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)>
data = fetch_version_info()
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
[Info] Safely checking for available cameras...
do you ever got the same issue?
@arturo-lr-dev
tryed on M4 and seem that didn't work.
Seems the camera is loading but the UI doesn't show anything
Log: DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning. [Info] Safely checking for available cameras...
Version apple: 15.5 Python 3.10.13
Solved: Just use python 3.10.16
Now I have the fps problems
Same behaviour with python 3.10.12 on my M1. Using python 3.10.16 works fine
The main branch fails with my m1 chip, but this fix works for me too using python 3.10.16 please merge :D
@KRSHH / @noahximus can you check this one out