Hi, what would be the best way to capture video from multiple cameras to run inference on them and also to save them afterward? I tried saving the video following the instructions for multiple cameras on a single host but the video switches between each camera and only one video gets saved for two cameras.

    Hi @SadiaC

    SadiaC the video switches between each camera

    Seems like a linking/coupling issue. Please add a MRE of the issue.

    Thanks,
    Jaka

    Yeah I think is a linking issue and I am not quite able to figure it out. Now two videos do get created and are shown by cv2 imshow but when i save it nothing gets saved.

    #!/usr/bin/env python3
    
    import cv2
    
    import depthai as dai
    
    import contextlib
    
    def createPipeline():
    # Start defining a pipeline
    
    pipeline = dai.Pipeline()
    
    # Define a source - color camera
    
    camRgb = pipeline.create(dai.node.ColorCamera)
    
    camRgb.setPreviewSize(300, 300)
    
    camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
    
    camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
    
    camRgb.setInterleaved(False)
    
    camRgb.setVideoSize(1920, 1080)
    
    # Create output
    
    xoutVideo = pipeline.create(dai.node.XLinkOut)
    
    xoutVideo.setStreamName("video")
    
    
    
    xoutVideo.input.setBlocking(False)
    
    xoutVideo.input.setQueueSize(1)
    
    # Define the duration (in seconds) of the video capture here
    
    capture_duration = 10
    
    # Linking
    
    camRgb.video.link(xoutVideo.input)
    
    return pipeline
    with contextlib.ExitStack() as stack:
    deviceInfos = dai.Device.getAllAvailableDevices()
    
    usbSpeed = dai.UsbSpeed.SUPER
    
    openVinoVersion = dai.OpenVINO.Version.VERSION_2021_4
    
    qRgbMap = []
    
    devices = []
    
    for deviceInfo in deviceInfos:
    
        deviceInfo: dai.DeviceInfo
    
        device: dai.Device = stack.enter_context(dai.Device(openVinoVersion, deviceInfo, usbSpeed))
    
        devices.append(device)
    
        print("===Connected to ", deviceInfo.getMxId())
    
        mxId = device.getMxId()
    
        cameras = device.getConnectedCameras()
    
        usbSpeed = device.getUsbSpeed()
    
        eepromData = device.readCalibration2().getEepromData()
    
        print("   >>> MXID:", mxId)
    
        print("   >>> Num of cameras:", len(cameras))
    
        print("   >>> USB speed:", usbSpeed)
    
        if eepromData.boardName != "":
    
            print("   >>> Board name:", eepromData.boardName)
    
        if eepromData.productName != "":
    
            print("   >>> Product name:", eepromData.productName)
    
        pipeline = createPipeline()
    
        device.startPipeline(pipeline)
    
        frame_width = int(1920) 
    
        frame_height = int(1080) 
    
    
    
        size = (frame_width, frame_height)
    
        
    
        # Output queue will be used to get the rgb frames from the output defined above
    
        video = device.getOutputQueue(name="video", maxSize=1, blocking=False)
    
        stream_name = "rgb-" + mxId + "-" + eepromData.productName
    
        qRgbMap.append((video, stream_name))
    
        result = cv2.VideoWriter('filename_'+str(mxId)+'.avi',  
    
                         cv2.VideoWriter_fourcc(\*'MJPG'), 
    
                          10, size)
    
       
    
    while True:
    
        for video, stream_name in qRgbMap:         
    
            if video.has():
    
                videoIn = video.get()
    
                result.write(videoIn.getCvFrame())
    
                cv2.imshow(stream_name, videoIn.getCvFrame())
    
        if cv2.waitKey(1) == ord('q'):
    
            break
    
    result.release()     
    
    cv2.destroyAllWindows() 
    
    
    
    print("The video was successfully saved")    

    Hi @SadiaC
    Not really a depthai issue, but here you go:

    #!/usr/bin/env python3
    
    import cv2
    import depthai as dai
    import contextlib
    
    def create_pipeline():
        # Start defining a pipeline
        pipeline = dai.Pipeline()
    
        # Define a source - color camera
        cam_rgb = pipeline.create(dai.node.ColorCamera)
        cam_rgb.setPreviewSize(300, 300)
        cam_rgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
        cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
        cam_rgb.setInterleaved(False)
        cam_rgb.setVideoSize(1920, 1080)
    
        # Create output
        xout_video = pipeline.create(dai.node.XLinkOut)
        xout_video.setStreamName("video")
        xout_video.input.setBlocking(False)
        xout_video.input.setQueueSize(1)
    
        # Linking
        cam_rgb.video.link(xout_video.input)
    
        return pipeline
    
    with contextlib.ExitStack() as stack:
        device_infos = dai.Device.getAllAvailableDevices()
    
        usb_speed = dai.UsbSpeed.SUPER
        openvino_version = dai.OpenVINO.Version.VERSION_2021_4
        q_rgb_map = []
        devices = []
        recordings = []
    
        for device_info in device_infos:
            # Initialize device
            device = stack.enter_context(dai.Device(openvino_version, device_info, usb_speed))
            devices.append(device)
            print(f"===Connected to {device_info.getMxId()}")
            
            mx_id = device.getMxId()
            cameras = device.getConnectedCameras()
            usb_speed = device.getUsbSpeed()
            eeprom_data = device.readCalibration2().getEepromData()
    
            print(f"   >>> MXID: {mx_id}")
            print(f"   >>> Num of cameras: {len(cameras)}")
            print(f"   >>> USB speed: {usb_speed}")
    
            if eeprom_data.boardName:
                print(f"   >>> Board name: {eeprom_data.boardName}")
            if eeprom_data.productName:
                print(f"   >>> Product name: {eeprom_data.productName}")
    
            # Start pipeline
            pipeline = create_pipeline()
            device.startPipeline(pipeline)
    
            # Output queue will be used to get the rgb frames from the output defined above
            video = device.getOutputQueue(name="video", maxSize=1, blocking=False)
            stream_name = f"rgb-{mx_id}-{eeprom_data.productName}"
            recording = (cv2.VideoWriter(f'filename_{mx_id}.avi', cv2.VideoWriter_fourcc(*'MJPG'), 10, (1920, 1080)))
            q_rgb_map.append((video, stream_name, recording))
    
    
        # Capture and display video frames
        while True:
    
            for video, stream_name, recording in q_rgb_map:
                print(f"Getting video from {stream_name}")
                if video.has():
                    video_in = video.get()
                    recording.write(video_in.getCvFrame())
                    cv2.imshow(stream_name, video_in.getCvFrame())
                    
            if cv2.waitKey(1) == ord('q'):
                break
    
        recording.release()
        cv2.destroyAllWindows()
        print("The video was successfully saved")

    Thanks,
    Jaka