How to capture Multi camera with Opencv Multi threading (python) [closed]

asked 2020-05-05 06:08:54 -0600

agalvilaku gravatar image

updated 2020-05-05 13:23:42 -0600

supra56 gravatar image

I am trying to achieve to get the streams from multiple ip cameras and display it. I have two ipcamera with urls : rtsp and rtsp1

When i run the following program, Only first window loads and works fine until the second thread kicks in.

Things works fine with multiple streams in the following methods :

  • Works without threads. i.e


cam0 = cv2.VideoCapture(rtsp)
cam1 = cv2.VideoCapture(rtsp1)
ret, frame0 =
ret, frame1 =
show imshow(frame0) & (frame1)

But this method is primitive and get lags. Moreover not scalable with more cameras.

  • If i run only one thread, the video streaming works fine.
  • if i run it as two different python program targeting each rtsp stream, i have no issue. Again not practical for more cameras.

But the objective was to run multiple ipcamera from one single program. Any help to highlight the mistake i made would be highly appreciated.

    import cv2
    import os
    import time
    import threading

    os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
    rtsp1 = 'rtsp://userid:[email protected]:8080/h264_ulaw.sdp'
    rtsp='rtsp://userid:[email protected]:10554/udp/av0_0'

    def getPicture(url):      

    cam = cv2.VideoCapture(url)
    start_frame_number = 20
    cam.set(cv2.CAP_PROP_POS_FRAMES, start_frame_number)


    while True:        
        ret, frame =
        small_frame = cv2.resize(frame, (0, 0), fx=.50, fy=.50)
        small_frame = cv2.cvtColor(small_frame,cv2.COLOR_BGR2GRAY)

        key = cv2.waitKey(20)
        if key == 27:  # exit on ESC

    # When everything done, release the video capture object

        # Closes all the frames

   if __name__ == '__main__':

    t1 = threading.Thread(target=getPicture, args=(rtsp,))
    t2 = threading.Thread(target=getPicture, args=(rtsp1,))

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by agalvilaku
close date 2020-05-06 05:46:01.845949



you probably DONT need multithreading at all for this.

but IF SO, -- you have to keep all gui related code (imshow(),waitKey(),etc) on the main thread

berak gravatar imageberak ( 2020-05-05 07:10:37 -0600 )edit

As @berak, said that you don't need threading. OpenCV will take care for you. You indentation isn't correct in in getPicture function.

if __name__ == '__main__':
supra56 gravatar imagesupra56 ( 2020-05-05 13:29:41 -0600 )edit

@supra56. The indentation is not an issue. It could be due to cut copy paste. I am still able to get into the method. I will try @berak method.

agalvilaku gravatar imageagalvilaku ( 2020-05-05 22:10:49 -0600 )edit

@berak Noted thanks.As you suggested moved the imshow to the main method and implemented threads to process the face recognition and works absolutely fine.

agalvilaku gravatar imageagalvilaku ( 2020-05-06 05:45:23 -0600 )edit

Could you link to the final code please?

RCAV gravatar imageRCAV ( 2020-07-08 23:47:55 -0600 )edit