andrew_giserkov
andrew_giserkov

Reputation: 141

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.

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:

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?

Upvotes: 14

Views: 5650

Answers (1)

User
User

Reputation: 41

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()

Upvotes: 4

Related Questions