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

孤街浪徒 提交于 2021-02-07 13:00:22

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/63376473/capturing-and-manipulating-a-webcam-feed-and-exposing-it-as-a-virtual-webcam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!