letmaik / pyvirtualcam

🎥 Send frames to a virtual camera from Python
GNU General Public License v2.0
472 stars 50 forks source link

Streaming is very slow #23

Closed KochC closed 3 years ago

KochC commented 3 years ago

I am sending data from C++ to Python in order to stream on a webcam. The streaming itself, without data from C++, works great and without big delay using this code.

import cv2
import numpy as np
from numpy import asarray
import pyvirtualcam

vc = cv2.VideoCapture(0)

if not vc.isOpened():
    raise RuntimeError('Could not open video source')

try:
    with pyvirtualcam.Camera(width=640, height=480, fps=20) as cam:

        while True:
            # Read frame from webcam
            s,frame = vc.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            array_frame = asarray(frame)
            frame = np.zeros((cam.height, cam.width, 4), np.uint8)
            frame[:,:,:3] = array_frame
            cam.send(frame)
            cam.sleep_until_next_frame()
finally:
    vc.release()

So it seems like the streaming is not the issue. With the following code, I can stream data from C++ with almost the same minimal delay to Python and display it using opencv.

import pyvirtualcam
import numpy as np
from numpy import asarray
import cv2

camera = pyvirtualcam.Camera(width=640, height=480, fps=20)

def foo_bar(img=None):  
    if img is not None:
        cv2.imshow("From Python", img)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        array_frame = asarray(img)
        img = np.zeros((camera.height, camera.width, 4), np.uint8)
        img[:,:,:3] = array_frame
        camera.send(img)
        # camera.sleep_until_next_frame()

But the webcam feed has a delay of around 2 seconds.

letmaik commented 3 years ago

Try using delay=0 in the Camera constructor. The minimum actual delay of the camera buffer is 10 frames. I'm currently adding support for the new built-in virtual camera of OBS (not the plugin), which reduces that to 3 and should help further.

letmaik commented 3 years ago

Could you retry with the latest release of pyvirtualcam (0.4.0)?

KochC commented 3 years ago

tried the old version with delay 0, then the whole system crashes. Using delay 1 works with quite a low delay. I will try the new version later today and report the outcome.

KochC commented 3 years ago

works nicely. No crashes anymore so far. Almost no delay.

camera = pyvirtualcam.Camera(width=640, height=480, fps=10, delay=0)