+
95
-

回答

pyvirtualcam可以实现

安装

pip install pyvirtualcam

示例

import pyvirtualcam
import numpy as np

with pyvirtualcam.Camera(width=1280, height=720, fps=20) as cam:
print(f'Using virtual camera: {cam.device}')
frame = np.zeros((cam.height, cam.width, 3), np.uint8) # RGB
while True:
frame[:] = cam.frames_sent % 255 # grayscale animation
cam.send(frame)
cam.sleep_until_next_frame()

播放到摄像头

# This script plays back a video file on the virtual camera.
# It also shows how to:
# - select a specific camera device
# - use BGR as pixel format

import argparse
import pyvirtualcam
from pyvirtualcam import PixelFormat
import cv2

parser = argparse.ArgumentParser()
parser.add_argument("video_path", help="path to input video file")
parser.add_argument("--fps", action="store_true", help="output fps every second")
parser.add_argument("--device", help="virtual camera device, e.g. /dev/video0 (optional)")
args = parser.parse_args()

video = cv2.VideoCapture(args.video_path)
if not video.isOpened():
raise ValueError("error opening video")
length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv2.CAP_PROP_FPS)

with pyvirtualcam.Camera(width, height, fps, fmt=PixelFormat.BGR,
device=args.device, print_fps=args.fps) as cam:
print(f'Virtual cam started: {cam.device} ({cam.width}x{cam.height} @ {cam.fps}fps)')
count = 0
while True:
# Restart video on last frame.
if count == length:
count = 0
video.set(cv2.CAP_PROP_POS_FRAMES, 0)

# Read video frame.
ret, frame = video.read()
if not ret:
raise RuntimeError('Error fetching frame')

# Send to virtual cam.
cam.send(frame)

# Wait until it's time for the next frame
cam.sleep_until_next_frame()

count += 1

注意:pyvirtualcam依赖于必须首先安装的现有虚拟摄像机。

支持的虚拟摄像机

windows:obs studio

OBS包括一个内置的虚拟摄像机(自26.0起)。

要使用OBS虚拟摄像机,只需安装OBS即可。

需要注意的是,OBS仅提供单个摄像机实例,因此无法将帧从Python发送到内置的OBS虚拟摄像机,在OBS中捕获摄像机,将其与其他内容混合,然后再次输出到OBS的内置虚拟摄像机。要实现这样的工作流程,请使用Python的另一个虚拟摄像机(如Unity捕获),以便OBS的内置虚拟摄像机在OBS中免费使用。

windows:unity capture

Unity 捕获提供了一个虚拟摄像机,最初用于流式传输 Unity 游戏。与大多数其他虚拟摄像机相比,它支持RGBA帧(具有透明度的帧),而这些帧又可以在OBS中捕获以进行进一步处理。

要使用 Unity 捕捉虚拟摄像机,请按照项目现场的安装说明进行操作。

macOS: obs

OBS包括一个用于macOS的内置虚拟摄像机(自26.1起)。

注意:从虚拟摄像头 0.10 开始,仅支持 OBS 28。如果您需要OBS 26 / 27支持,请安装旧版本。

要使用OBS虚拟摄像机,请按照以下一次性设置步骤操作:

安装对象存储。

启动对象存储。

单击“启动虚拟摄像机”(右下角),然后单击“停止虚拟摄像机”。

关闭对象存储。

请注意,OBS仅提供单个摄像机实例,因此无法从Python发送帧,在OBS中捕获摄像机,将其与其他内容混合,然后将其再次输出为虚拟摄像机。

Linux: v4l2loopback

pyvirtualcam在Linux上使用v4l2loopback虚拟相机。

要在 Ubuntu 上创建 v4l2 回转虚拟摄像机,请运行以下命令:

sudo apt install v4l2loopback-dkms

sudo modprobe v4l2loopback devices=1

网友回复

我知道答案,我要回答