I had been looking for a solution to a similar problem for a while but couldn’t find anything and almost gave up. Recently, I got back to the project and finally figured it out. Here is the solution: the trick is to use the right Capture API backend. I connected up to three cameras via one USB-C hub, and it worked. The trick is to use “cv2.CAP_ANY”.
import cv2 as cv
import numpy as np
def initialize_cameras():
return [cv.VideoCapture(i, cv.CAP_ANY) for i in range(3)]
def release_cameras(cameras):
for cap in cameras:
cap.release()
def read_frames(cameras):
return [cap.read() for cap in cameras]
def all_frames_valid(frames):
return all(ret for ret, _ in frames)
def concatenate_frames(frames):
v_divider = np.ones((frames[0][1].shape[0], 6, 3), dtype=np.uint8) * 255
combined_frame = np.concatenate([frame for _, frame in frames], axis=1)
return np.concatenate([combined_frame, v_divider], axis=1)
def main():
cameras = initialize_cameras()
run = True
while run:
frames = read_frames(cameras)
if all_frames_valid(frames):
combined_frame = concatenate_frames(frames)
cv.imshow('TEST', combined_frame)
else:
release_cameras(cameras)
cameras = initialize_cameras()
if cv.waitKey(200) & 0xFF == ord('q'):
release_cameras(cameras)
run = False
cv.destroyAllWindows()
if __name__ == "__main__":
main()