捕获和操作网络摄像头源并将其作为“虚拟网络摄像头"公开- 在 Python 中,在 Windows 上 [英] Capturing and manipulating a webcam feed and exposing it as a "virtual webcam" - in Python, on Windows

查看:191
本文介绍了捕获和操作网络摄像头源并将其作为“虚拟网络摄像头"公开- 在 Python 中,在 Windows 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终目标是捕获常规网络摄像头提要,以某种方式对其进行处理(模糊面部、替换背景等),然后以某种方式输出结果,以便可以选择被处理的提要作为输入任何需要网络摄像头的应用程序(Discord、Teams 等).

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, ...).

我在 Windows 机器上工作,更喜欢用 Python 来做这件事.目前,这种组合让我迷失了方向.

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

显然,在 Linux 上有提供该功能的 Python 库,但它们在 Windows 上不起作用.一切听起来像是暗示一个好的解决方案的东西都直接进入了 C++ 国家.有些程序基本上可以做我想做的事,例如webcamoid (https://webcamoid.github.io/) 我可以破解一个通过 Python 捕获和处理提要的解决方案,然后使用 webcamoid 记录输出并将其提供给虚拟网络摄像头.但我更愿意将整个事情一次性完成.

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.

我一直在四处搜索,发现有关该主题的 stackoverflow 的这些问题:

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

我有强烈的印象,我需要 C++ 或必须在 Linux 上工作.然而,由于缺乏 Linux 机器和任何设置以及 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?

因此,问题是:有没有办法暴露网络摄像头"?在 Windows 上通过 Python 流式传输?

而且,最后一个想法:如果我使用带有 Linux Python 环境的 docker 容器来实现我想要的功能会怎样.然后该容器可以流式传输虚拟网络摄像头"吗?给主持人?

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?

推荐答案

您可以使用 pyvirtualcam

首先,您需要使用 pip 安装它

First, you need to install it using pip

pip install pyvirtualcam

然后去这个链接下载最新的zip文件发布

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

解压并导航到\bin\[您的计算机的比特性]

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"

现在您可以使用 pyvirtualcam 向相机发送帧

Now you can send frames to the camera using pyvirtualcam

这是一个示例:

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

这篇关于捕获和操作网络摄像头源并将其作为“虚拟网络摄像头"公开- 在 Python 中,在 Windows 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆