# 多设备设置

您可以在[此处找到演示脚本](https://github.com/luxonis/oak-examples/tree/main/tutorials/multiple-devices)。了解如何发现连接到系统的多个OAK摄像头，并单独使用它们。

## 发现OAK摄像头

您可以使用DepthAI发现所有连接的OAK摄像头，无论是通过USB还是局域网（OAK POE摄像头）。下面的代码片段会查找所有OAK摄像头并打印它们的DeviceIDs（唯一标识符）和XLink state。

```python
import depthai
for device in depthai.Device.getAllAvailableDevices():
    print(f"{device.getDeviceId()} {device.state}")
```

系统中3个DepthAI的示例结果：

```bash
14442C10D13EABCE00 XLinkDeviceState.X_LINK_UNBOOTED
14442C1071659ACD00 XLinkDeviceState.X_LINK_UNBOOTED
3604808376 XLinkDeviceState.X_LINK_GATE
```

## 选择要使用的特定DepthAI设备

从上面检测到的设备中，使用以下代码选择您希望与管道一起使用的设备。例如，如果第一个设备符合要求，请使用以下代码：

```python
# 指定设备ID、IP地址或USB路径
device_info = depthai.DeviceInfo("14442C108144F1D000") # 设备ID
#device_info = depthai.DeviceInfo("192.168.1.44") # IP地址
#device_info = depthai.DeviceInfo("3.3.3") # USB端口名称
with depthai.Device(device_info) as device:
    # ...
```

您可以将此代码作为您自己用例的基础，例如在不同OAK型号上运行不同的神经网络模型。

## 指定要使用的POE设备

您也可以通过IP地址指定要使用的POE设备，如上面的代码片段所示。

现在使用任意数量的OAK摄像头！由于DepthAI承担了所有繁重的工作，您通常可以在对主机负担很小的情况下使用多个摄像头。

## 时间戳同步

时间戳同步，也称为消息同步，涉及对齐来自各种传感器（包括帧、IMU数据包、ToF数据等）的消息。

> DepthAI 2.24引入了Sync节点，可用于同步来自不同流或不同传感器（例如IMU和彩色帧）的消息。有关更多详细信息，请参见Sync节点。Sync节点目前不支持多设备同步，因此如果您想同步来自多个设备的消息，应使用手动方法。

有关时间戳同步的更多信息，请参阅[帧同步页面](https://docs.luxonis.com/hardware/platform/deploy/frame-sync.md)。

## 多摄像头演示

```python
#!/usr/bin/env python3

import cv2
import depthai as dai
import contextlib

def createPipeline(pipeline):
    camRgb = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
    output = camRgb.requestOutput((1280, 800), dai.ImgFrame.Type.NV12 ,dai.ImgResizeMode.CROP, 20).createOutputQueue()
    return pipeline, output

with contextlib.ExitStack() as stack:
    deviceInfos = dai.Device.getAllAvailableDevices()
    print("=== Found devices: ", deviceInfos)
    queues = []
    pipelines = []

    for deviceInfo in deviceInfos:
        pipeline = stack.enter_context(dai.Pipeline())
        device = pipeline.getDefaultDevice()
        
        print("===Connected to ", deviceInfo.getDeviceId())
        mxId = device.getDeviceId()
        cameras = device.getConnectedCameras()
        usbSpeed = device.getUsbSpeed()
        eepromData = device.readCalibration2().getEepromData()
        print("   >>> Device ID:", mxId)
        print("   >>> Num of cameras:", len(cameras))
        if eepromData.boardName != "":
            print("   >>> Board name:", eepromData.boardName)
        if eepromData.productName != "":
            print("   >>> Product name:", eepromData.productName)
        
        pipeline, output = createPipeline(pipeline)
        pipeline.start()
        pipelines.append(pipeline)

        queues.append(output)

    while True:
        for i, stream in enumerate(queues):
            videoIn = stream.get()
            assert isinstance(videoIn, dai.ImgFrame)
            cv2.imshow(f"video_device{i}", videoIn.getCvFrame())
        if cv2.waitKey(1) == ord('q'):
            break
```

## 多摄像头标定

此示例演示如何计算多个摄像头的外部参数（摄像头姿态）。它提供了如何确定多摄像头设置中不同摄像头的相对位置和方向的实用说明。通过准确估计外部参数，我们可以确保每个摄像头捕获的图像正确对齐，并可以有效地组合以进行进一步处理和分析。

### GitHub上的多摄像头标定

[GitHub上的多摄像头标定](https://github.com/luxonis/oak-examples/tree/main/tutorials/multiple-devices/multi-cam-calibration)
