1

I'm trying to send a video feed from my physical camera to a virtual camera in Python so I can perform certain effects on it.

This is my code so far:

import pyvirtualcam
import numpy as np


cam = pyvirtualcam.Camera(width=1280, height=720, fps=30)
cvCam = cv2.VideoCapture(0)

while True:
    try:
        _, frame = cvCam.read()
        
        cam.send(frame)
        cam.sleep_until_next_frame()
    except KeyboardInterrupt:
        cam.close()
        cvCam.close()
        break

print("Done")

After I ran this code, I got an error saying that I also needed to add an alpha channel. I copied some code from this post. This was my new code that added an alpha channel to the code:

import pyvirtualcam
import numpy as np


cam = pyvirtualcam.Camera(width=1280, height=720, fps=30)
cvCam = cv2.VideoCapture(0)

while True:
    _, frame = cvCam.read()

    b_channel, g_channel, r_channel = cv2.split(frame)
    alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 50
    frame = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))

    cam.send(frame)
    cam.sleep_until_next_frame()

print("Done")

After running this code, it just suddenly exits the program without any error message even though it is in a while True loop. I am unable to debug this problem. What is the issue?

LazyBeast
  • 45
  • 4

1 Answers1

0

You probably have mismatching frame sizes from your source vs the virtual cam. Currently this leads to a hard crash (see also https://github.com/letmaik/pyvirtualcam/issues/17).

The solution is to query the source webcam width and height and use that to initialize the virtual camera. The webcam_filter.py sample at https://github.com/letmaik/pyvirtualcam/blob/main/examples/webcam_filter.py shows how to do exactly that.

Roughly:

cvCam = cv2.VideoCapture(0)
width = int(cvCam.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cvCam.get(cv2.CAP_PROP_FRAME_HEIGHT))
with pyvirtualcam.Camera(width=width, height=height, fps=30) as cam:
   ...
roman
  • 1,061
  • 6
  • 14
letmaik
  • 3,348
  • 1
  • 36
  • 43