Deep-Live-Cam icon indicating copy to clipboard operation
Deep-Live-Cam copied to clipboard

Added virtual camera output and fetching of input camera devices with names using pygrabber on windows and linux

Open bit-wrangler opened this issue 1 year ago • 1 comments

The primary change in this PR is to allow the output of video to a virtual camera using pyvirtualcam package. This should work on Windows and Linux. IIRC, this may not work on osx - so, I put it behind a toggle. I refactored the live preview loop contents to be a function so that the loop can be conditionally place inside a "with virtualcam" scope. I increased the height of the window to fit the extra toggle, plus space things out just a little bit. I've also used the pygrabber package to retrieve input camera options on windows and linux with their names, which IMO is better than seeing "Camera 0", "Camera 1", etc.

Summary by Sourcery

Introduce virtual camera output functionality and enhance camera device selection by displaying device names using pygrabber. Refactor the live preview loop for better modularity and adjust UI layout to support new features.

New Features:

  • Add support for outputting video to a virtual camera using the pyvirtualcam package on Windows and Linux.
  • Implement fetching of input camera devices with their names using the pygrabber package on Windows and Linux.

Enhancements:

  • Refactor the live preview loop into a separate function to allow conditional execution within a virtual camera context.
  • Increase the height of the UI window to accommodate additional UI elements and improve spacing.

bit-wrangler avatar Aug 15 '24 05:08 bit-wrangler

Reviewer's Guide by Sourcery

This pull request adds virtual camera output functionality and improves the fetching of input camera devices with names using pygrabber on Windows and Linux. The changes primarily affect the modules/ui.py file, introducing new features and refactoring existing code to accommodate these additions.

File-Level Changes

Files Changes
modules/ui.py Added virtual camera output functionality using pyvirtualcam package
modules/ui.py Refactored live preview loop contents into a separate function for better code organization
modules/ui.py Increased the height of the main window to accommodate new UI elements
modules/ui.py Implemented camera device fetching with names using pygrabber on Windows and Linux
modules/ui.py Added a new toggle switch for virtual camera output in the UI
modules/ui.py Updated the webcam_preview function to support virtual camera output
modules/ui.py Modified the get_available_cameras function to use pygrabber for device detection on Windows and Linux

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

sourcery-ai[bot] avatar Aug 15 '24 07:08 sourcery-ai[bot]

I merged and resolved conflicts from upstream. I've also added a README note for new functionality.

bit-wrangler avatar Aug 15 '24 16:08 bit-wrangler

Down the line, it would be nice to add audio capture and audio output with a delay to match audio to video. And an even further goal might be to add audio voice mapping between the audio input and output.

bit-wrangler avatar Aug 15 '24 16:08 bit-wrangler

Hi, thank you for this interesting PR! I've read that pyvirtualcam uses v4l2loopback, which should (in theory) make it possible to populate the new v4l device as usb webcam via usb uvc gadget device (so any device attached to it would see a regular ready to use webcam)

I just tried the experimental branch, but it crashes with the following trace on my (Arch Linux) system:

python run.py --execution-provider cuda
Traceback (most recent call last):
  File "/media/nvme/ai/Deep-Live-Cam-experimental/run.py", line 3, in <module>
    from modules import core
  File "/media/nvme/ai/Deep-Live-Cam-experimental/modules/core.py", line 21, in <module>
    import modules.ui as ui
  File "/media/nvme/ai/Deep-Live-Cam-experimental/modules/ui.py", line 9, in <module>
    from pygrabber.dshow_graph import FilterGraph
  File "/media/nvme/ai/deep-live-cam/lib/python3.10/site-packages/pygrabber/dshow_graph.py", line 33, in <module>
    from ctypes import POINTER, byref, cast, create_unicode_buffer, pointer, windll, wstring_at
ImportError: cannot import name 'windll' from 'ctypes' (/usr/lib/python3.10/ctypes/__init__.py)

Likely the cause is

from pygrabber.dshow_graph import FilterGraph

(or better DirectShow -> pygrabber generally) being windows only

No big deal, just wanted to let you know

frostworx avatar Aug 24 '24 13:08 frostworx