I want to read from one camera in many parallel processes.
Here is what I’ve tried.
from multiprocessing import Process
import cv2
import time
import random
def get_capture():
time.sleep(random.randint(0, 3))
cap = cv2.VideoCapture(0)
w, h = cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
if (w, h) == (0, 0):
print("Seemingly failed to construct the capture.")
print(f"Feed dimensions are {w}x{h}.")
success, _ = cap.read()
if not success:
print("Failed frame reading.")
if __name__ == "__main__":
pcs = []
for _ in range(256):
p = Process(target=get_capture)
pcs.append(p)
p.start()
for p in pcs:
p.join()
The frame reading is likely to fail in many processes.
Does Open CV support creating multiple VideoCaptures for the same camera and reading from them? If it does, what am I doing wrong?
The code is only an illustration of what I want to achieve. For instance, in my real project, I have no easy method to read frames from one process and consume them from other processes.