Home > Blockchain >  Error using pyvirtualcam: 'NoneType' object has no attribute 'send'
Error using pyvirtualcam: 'NoneType' object has no attribute 'send'

Time:01-22

I am trying to fix my webcam, that has a horizontal green line in it, using OpenCV to inpaint the line and pyvirtualcam to create a virtual cam and use it in apps like discord and google meet. The image processing with OpenCV is working correctly, but I am getting te following error trying to create the virtual cam:

Traceback (most recent call last):
  File "...\webcamfix\camfix.py", line 37, in <module>     
    cam.send(frame)
  File "...\pyvirtualcam\camera.py", line 349, in send
    self._backend.send(frame)
    ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'send'

Here is my code:

import cv2
import pyvirtualcam
from pyvirtualcam import PixelFormat

# Read webcam
webcam = cv2.VideoCapture(0)

# Set width to 1280 and height to 720
webcam.set(3, 1280)
webcam.set(4, 720)

# Read mask
mask = cv2.imread('mask.jpg', cv2.IMREAD_GRAYSCALE)

# Start virtual cam with 1280x720 30fps
with pyvirtualcam.Camera(1280, 720, 30, fmt=PixelFormat.BGR) as cam:
    print('Virtual camera device: '   cam.device)


if webcam.isOpened():
    sucess, frame = webcam.read()
    while sucess:
        sucess, frame = webcam.read()

        # Inpainting frame where mask limits using TELEA technique
        frame = cv2.inpaint(frame, mask, 3, cv2.INPAINT_NS)
        
        # Fliping image
        frame = cv2.flip(frame, 1)

        # Displaying video and closing the window if ESC is pressed
        cv2.imshow("webcamfix", frame)
        key = cv2.waitKey(5)
        if key == 27: # ESC
            break

        cam.send(frame)
        cam.sleep_until_next_frame()

As pyvirtualcam documentation recommends, I am using OBS virtual camera and Unity Video Capture. If there is some easier way to create a virtual camera with python on windows I would be glad to know.

CodePudding user response:

When using with ... as cam statement, the cam object is automatically closed after the nested block of code.

cam object applies the same behaviour as opening a file using with statement (the file is automatically closed after the nested block of code).

Since the cam object is closed we are getting an exception "'NoneType' object has no attribute 'send'".


Replace with pyvirtualcam.Camera(1280, 720, 30, fmt=PixelFormat.BGR) as cam: with:

cam = pyvirtualcam.Camera(1280, 720, 30, fmt=PixelFormat.BGR)

Alternatively, place the code after with pyvirtualcam.Camera(1280, 720, 30, fmt=PixelFormat.BGR) as cam inside the nested block:

with pyvirtualcam.Camera(1280, 720, 30, fmt=PixelFormat.BGR) as cam:
    print('Virtual camera device: '   cam.device)

    if webcam.isOpened():
        success = True
        while success:
            success, frame = webcam.read()
            
            # Displaying video and closing the window if ESC is pressed
            if success:
                # Inpainting frame where mask limits using TELEA technique
                frame = cv2.inpaint(frame, mask, 3, cv2.INPAINT_NS)
            
                # Flipping image
                frame = cv2.flip(frame, 1)
                
                cv2.imshow("webcamfix", frame)
                key = cv2.waitKey(1)
                if key == 27: # ESC
                    break

                cam.send(frame)
                cam.sleep_until_next_frame()
  • Related