opencv 我想输出视频作为虚拟凸轮在python

yqlxgs2m  于 2023-06-30  发布在  Python
关注(0)|答案(1)|浏览(131)

我写了一个脚本共享视频从笔记本电脑到笔记本电脑通过网络。我想把我的接收视频作为虚拟凸轮。请帮帮我。

import cv2, socket, numpy, pickle
def camera(ip2):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
    ip = ip2  
    port = 2323  # Server Port Number to identify the process that needs to recieve or send packets
    s.bind((ip, port)) 
    while True:
        x = s.recvfrom(100000000)  
        clientip = x[1][0]  
        data = x[0] 
        data = pickle.loads(data)  
        data = cv2.imdecode(data, cv2.IMREAD_COLOR)  
        cv2.imshow('my pic', data)  # Show Video/Stream
        if cv2.waitKey(10) == 13:
            break
    cv2.destroyAllWindows()
def main():
    a = input('Enter IP Address')
    camera(a)

main()

我想输出我的接收视频作为虚拟凸轮。

uurv41yg

uurv41yg1#

import cv2, socket, numpy, pickle
import pyvirtualcam
from pyvirtualcam import PixelFormat

def camera(ip2):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
    ip = ip2  
    port = 2323  # Server Port Number to identify the process that needs to receive or send packets
    s.bind((ip, port)) 

    # Initialize virtual camera. Default is 640x480 @ 20fps
    with pyvirtualcam.Camera(width=640, height=480, fps=20, fmt=PixelFormat.BGR) as cam:
        while True:
            x = s.recvfrom(100000000)  
            clientip = x[1][0]  
            data = x[0] 
            data = pickle.loads(data)  
            data = cv2.imdecode(data, cv2.IMREAD_COLOR)  
            
            # Ensure the image fits your virtual cam resolution. You might need to resize it with cv2.resize
            frame = cv2.resize(data, (640, 480))

            # Flip frame to RGB format and send to virtual cam
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            cam.send(frame)

            # Break loop with enter key
            if cv2.waitKey(10) == 13:
                break

        cv2.destroyAllWindows()

def main():
    a = input('Enter IP Address')
    camera(a)

main()

请注意,pyvirtualcam依赖于创建实际虚拟摄像机设备的本机库。您可能需要为此安装其他软件,具体取决于您的操作系统:
在Windows上,您需要OBS VirtualCam 2.0.4或更高版本。您可以从OBS项目网站下载它。在macOS上,你需要CamTwist。你可以从CamTwist网站下载。在Linux上,您需要v4 l2 loopback。你可以从你的包管理器中安装它,例如,在Ubuntu上使用sudo apt install v4 l2 loopback-dkms。在运行脚本之前,您还需要使用sudo modprobe v4 l2 loopback加载模块。要使此更改永久化,可以将v4 l2 loopback添加到/etc/modules。

相关问题