pythonubuntuv4l2loopback

IOError when trying to use PyFakeWebcam


So, I was recently trying to use PyFakeWebcam, but I have ran into a bit of a problem. Whenever I try to do anything with it, I run into this error:

Traceback (most recent call last):
    File "fakecam.py", line 13, in <module>
        fake1 = pyfakewebcam.FakeWebcam('/dev/video1', IMG_W, IMG_H)
    File "/home/user/.local/lib/python3.6/site-packages/pyfakewebcam/pyfakewebcam.py", line 54, in __init__
        fcntl.ioctl(self._video_device, _v4l2.VIDIOC_S_FMT, self._settings)
OSError: [Errno 22] Invalid argument

For reference, I am using this code:

import cv2
import time
import pyfakewebcam
import numpy as np

IMG_W = 1280
IMG_H = 720

cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, IMG_W)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, IMG_H)

fake1 = pyfakewebcam.FakeWebcam('/dev/video1', IMG_W, IMG_H)
fake2 = pyfakewebcam.FakeWebcam('/dev/video2', IMG_W, IMG_H)

while True:
    ret, frame = cam.read()

    flipped = cv2.flip(frame, 1)

    # Mirror effect 
    frame[0 : IMG_H, IMG_W//2 : IMG_W] = flipped[0 : IMG_H, IMG_W//2 : IMG_W]

    fake1.schedule_frame(frame)
    fake2.schedule_frame(flipped)

    time.sleep(1/15.0)

What is causing this error, and is it resolvable?


Solution

  • Try selecting different device - /dev/video1 might be unavailabe and modprobe created /dev/video2 and /dev/video3. Check ls /dev | grep -P '^video\d+$' output for available video devices and select one of the last ones.

    After running the script check the result by ffplay /dev/video3.

    If this doesn't solve your issue, you'll have to install v4l2loopback-utils specific version.

    For more details take a look at this github issue