Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing and manipulating a webcam feed and exposing it as a "virtual webcam" - in Python, on Windows

The final goal would be to capture the regular webcam feed, manipulate it in some way (blur face, replace background, ...) and then output the result in some way so that the manipulated feed can be chosen as input for whatever application expects a webcam (Discord, Teams, ...).

I am working on a Windows machine and would prefer to do this in Python. This combination has me lost, at the moment.

  • capturing and manipulating is easy with https://pypi.org/project/opencv-python/
  • the exposing the feed step seems overly complicated

Apparently, on Linux there are Python libraries just offering that functionality, but they do not work on Windows. Everything that sounded like it could hint towards a good solution went directly into C++ country. There are programs which basically do what I want, e.g. webcamoid (https://webcamoid.github.io/) and I could hack together a solution which captures and processes the feed via Python, then uses webcamoid to record the output and feed it into a virtual webcam. But I'd much prefer to do the whole thing in one.

I have been searching around a bit and found these questions on stackoverflow on the topic:

  • Using OpenCV Output as Webcam (uses C++ but also gives a Python solution - however, pyfakewebcam does not work on Windows)
  • How do I stream to a new video source? (not really answered, just links to other question)
  • How to simulate a webcam device (more C++ hints, links to msdn's Writing a Custom Media Source)
  • Artificial webcam on windows (basically what I want, but in C++ again)
  • Writing a virtual webcam? (more explanation on how this might work in C++)

I am getting the strong impression that I need C++ for this or have to work on Linux. However, lacking both a Linux machine and any setup as well as experience in programming in C++, this seems like a large amount of work for the "toy project" this was supposed to be. But maybe I am just missing an obvious library or functionality somewhere?

Hence, the question is: Is there a way to expose a "webcam" stream via Python on Windows?

And, one last idea: What if I used a docker container with a Linux Python environment to implement the functionality I want. Could that container then stream a "virtual webcam" to the host?

like image 231
andrew_giserkov Avatar asked Aug 12 '20 12:08

andrew_giserkov


People also ask

How to make OBS virtual webcam?

Step 1: Set up your green screen with proper lighting for better video. Step 2: Download and install OBS Studio. Step 3: Add Video Source to your Scene by clicking the '+' icon in the Sources tab. Step 4: Select and add a suitable camera from the camera setting.


1 Answers

You can do this by using pyvirtualcam

First, you need to install it using pip

pip install pyvirtualcam

Then go to This Link and download the zip file from the latest release

Unzip and navigate to \bin\[your computer's bittedness]

Open Command Prompt in that directory and type

regsvr32 /n /i:1 "obs-virtualsource.dll"

This will register a fake camera to your computer

and if you want to unregister the camera then run this command:

regsvr32 /u "obs-virtualsource.dll"

Now you can send frames to the camera using pyvirtualcam

This is a sample:

import pyvirtualcam
import numpy as np

with pyvirtualcam.Camera(width=1280, height=720, fps=30) as cam:
    while True:
        frame = np.zeros((cam.height, cam.width, 4), np.uint8) # RGBA
        frame[:,:,:3] = cam.frames_sent % 255 # grayscale animation
        frame[:,:,3] = 255
        cam.send(frame)
        cam.sleep_until_next_frame()
like image 160
User Avatar answered Oct 10 '22 11:10

User