Unable to use cv2.VideoCapture in 2 different programs parallely

I am using cv2.VideoCapture(0) to open the camera and read it’s frames in 2 different neural networks namely Object detection and Semantic segmentation.

I tried passing the frames to both of the networks at the same time, but due to a bug in tensorflow, I am now forced to run these network seperately. But unfornately, opencv doesn’t allow to run cv2.VideoCapture(0) in 2 different programs at the same time. I know that this is not possible, but is there any other alternative to make it work? Any help is appreciated.

virtual camera, read from camera once, write to two virtual cameras, use those as sources. that requires a “virtual camera” driver.

or python multiprocessing.

Thanks! I found a link to use virtual camera.

But I am not sure about multi-processing in this scenario though as I have to execute ‘objdetection.py’ and ‘semsegmentation.py’ seperately.

1 Like

I have a small update after using pyfakeWebcam. The output image of pyfakeWebcam is not np.ndarray as in opencv. Hence I couldn’t apply the same operations on the image. Please find the image attached.

Type of pyfakewebcam-> <class 'pyfakewebcam.pyfakewebcam.FakeWebcam'>
Traceback (most recent call last):
  File "test_virtual_camera.py", line 15, in <module>
    gray = cv2.resize(gray, (500, 300))
TypeError: Expected Ptr<cv::UMat> for argument '%s'

please do not post images of errors or code anywhere on the net, replace with TEXT, thank you

and now, we need to see your code…

so, what is it, then ?

Ok updated with the code.

This is the type <class 'pyfakewebcam.pyfakewebcam.FakeWebcam'>

My code


#Open the camera and read the frames

import cv2
import pyfakewebcam

gray = pyfakewebcam.FakeWebcam('/dev/video1', 640, 480)

while(True):
    # Capture frame-by-frame

    print('Type of pyfakewebcam->', type(gray))
    gray = cv2.resize(gray, (500, 300))
    
    
    cv2.imshow("Pyfakewebcam", gray)

    key = cv2.waitKey(1)
    if key == 27:
        break

gray.release()
cv2.destroyAllWindows()

it pretty much looks, like

is the camera instance (not an image), and that you have to call some method on it to get an image

your job, to read the docs there, not ours …

Ok, relax! will do. Thanks!