運行時錯誤:「v4l2loopback」後端:std::異常

運行時錯誤:「v4l2loopback」後端:std::異常

當我嘗試執行以下程式碼時出現上述錯誤:

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

我在樹莓派上運行這個(內核版本:5.10,樹莓派內核頭版本是1:1.20210527-1)。

錯誤:

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.7/site-packages/pyvirtualcam/camera.py", line 219, in __init__
    raise RuntimeError('\n'.join(errors))
  RuntimeError: 'v4l2loopback' backend: std::exception

解決了:

v4l2loopback 由於某種原因崩潰,導致此問題,程式碼開頭的以下幾行修復了該問題。

 import os

 if "raspberrypi" in str(os.uname()):
            os.system("sudo modprobe -r v4l2loopback")
            os.system("sudo modprobe v4l2loopback")

相關內容