Capturing video from two cameras in OpenCV at once

前端 未结 7 1163
别跟我提以往
别跟我提以往 2020-11-30 04:27

How do you capture video from two or more cameras at once (or nearly) with OpenCV, using the Python API?

I have three webcams, all capable of video streaming, locate

相关标签:
7条回答
  • 2020-11-30 05:01

    Adding a little to what @TheoreticallyNick posted earlier:

    import cv2
    import threading
    
    class camThread(threading.Thread):
        def __init__(self, previewName, camID):
            threading.Thread.__init__(self)
            self.previewName = previewName
            self.camID = camID
        def run(self):
            print("Starting " + self.previewName)
            camPreview(self.previewName, self.camID)
    
    def camPreview(previewName, camID):
        cv2.namedWindow(previewName)
        cam = cv2.VideoCapture(camID)
        if cam.isOpened():
            rval, frame = cam.read()
        else:
            rval = False
    
        while rval:
            cv2.imshow(previewName, frame)
            rval, frame = cam.read()
            key = cv2.waitKey(20)
            if key == 27:  # exit on ESC
                break
        cv2.destroyWindow(previewName)
    
    # Create threads as follows
    thread1 = camThread("Camera 1", 0)
    thread2 = camThread("Camera 2", 1)
    thread3 = camThread("Camera 3", 2)
    
    thread1.start()
    thread2.start()
    thread3.start()
    print()
    print("Active threads", threading.activeCount())
    

    This will open up a new thread for each webcam you have. In my case, I wanted to open up three different feeds. Tested on Python 3.6. Let me know if you have any issues, also thanks to TheoreticallyNick for the readable/functioning code!

    0 讨论(0)
提交回复
热议问题