Search code examples
pythonopencvvideostreamcapture

Opencv videostream of two cameras in the same window


I am trying to stream two cameras simultaneously into one window using opencv. Any idea how can I do it? I can do it using different windows and threading but I wanted to incorporate the two streams into one window.

NOTE: The cameras have the same resolution


Solution

  • this should work with the below example. please try this.

    #for image
    ret1, img1 = camera1.read()
    ret2, img2 = camera2.read()
    if ret1==False or ret2==False:
          print("could not read from cameras !")
          return
    
    # now, you can do this either vertical (one over the other):
    final = cv2.vconcat([img1, img2])
    
    # or horizontal (next to each other):
    #final = cv2.hconcat([img1, img2])
    
    imshow("I", final)
    waitKey(10)
    

    For further reference please see this https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga4676b1376cdc4e528dab6bd9edc51c1a

    #for video: P.S: It should be like this, but I didn't test it with video.
    
    cap_1= cv2.VideoCapture(CAP_ANY)
    cap_2 = cv2.VideoCapture(CAP_ANY)
      
    while true:
          ret_1, frame_1 = cap_1.read()
          ret_2, frame_2 = cap_2.read()
    
    # now, you can do this either vertical (one over the other):
         final = cv2.vconcat([frame_1, frame_2])
    
    # or horizontal (next to each other):
    #final = cv2.hconcat([img1, img2])
    
           imshow("I", final)
           waitKey(10)