letmaik / pyvirtualcam

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

v4l2loopback: 'error writing frame: Invalid argument' when cam.send() #90

Closed mortal-Zero closed 1 year ago

mortal-Zero commented 1 year ago

Hi, I'm trying to transfer images to virtual camera(v4l2loopback) using pyturalcam, and I just ran your example

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

And I got the error:

error writing frame: Invalid argumenterror

I found out through debugging that the error was printed while executing the line 'cam.send(frame)', What is the reason for this? Hope you can help.

letmaik commented 1 year ago

Does it happen reproducibly? It looks like an issue with v4l2loopback. Maybe you started the script twice somehow? Or another software is writing to the camera as well and has re-configured it after starting the Python script. The error message doesn't say much, so it's hard to tell.

mortal-Zero commented 1 year ago

Does it happen reproducibly? It looks like an issue with v4l2loopback. Maybe you started the script twice somehow? Or another software is writing to the camera as well and has re-configured it after starting the Python script. The error message doesn't say much, so it's hard to tell.

ok, thanks for your reply, I will try to reinstall v4l2loopback.

mortal-Zero commented 1 year ago

Does it happen reproducibly? It looks like an issue with v4l2loopback. Maybe you started the script twice somehow? Or another software is writing to the camera as well and has re-configured it after starting the Python script. The error message doesn't say much, so it's hard to tell.

Sorry to bother you again, I am having some problems with pyvituralcam. when I with pyvirtualcam.Camera(width=1920, height=1080, fps=30) as cam and cam.send(frame), can I use cv2.VideoCapture(0) to get the frame? My computer has only one camera created by v4l2loopback, '/dev/video0'.

this is my code:


import os
import sys

import cv2
import numpy as np
import pyvirtualcam

if __name__ == "__main__":
    video_path = '/workspace/test_virtual_camera/demo_video/demo.mp4'

    video_capture = cv2.VideoCapture(video_path)
    frame_height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    frame_width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
    fps = video_capture.get(cv2.CAP_PROP_FPS)

    frame_rgb_list = []
    for frame_idx in range(int(frame_count)):
        _, frame = video_capture.read()
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_rgb_list.append(frame_rgb)

    with pyvirtualcam.Camera(width=int(frame_width),
                             height=int(frame_height),
                             fps=int(fps)) as cam:
        print(f'Using virtual camera: {cam.device}')

        video_capture_virtualcam = cv2.VideoCapture(0)

        n = 0
        while True:
            frame = frame_rgb_list[n]
            cam.send(frame)
            cam.sleep_until_next_frame()

            ret, frame_from_virtualcam = video_capture_virtualcam.read()
            cv2.imshow("frame_from_virtualcam", frame_from_virtualcam)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            if n == 10:
                break
            print('==>>', n)
            n += 1

        video_capture_virtualcam.release()
        cv2.destroyAllWindows()

        sys.exit()
letmaik commented 1 year ago

Can you confirm whether you fixed your original issue? If so, please close this github issue. You can use the Discussions to ask further questions.