# DepthAI V3

面向 OAK 和 OAK4 相机的 DepthAI v3 API 文档：安装、示例以及 Python/C++ 参考。

本文档涵盖 DepthAI v3，该版本同时支持 OAK 和 OAK4 相机。如果您希望使用 DepthAI v2（仅支持 RVC2 设备），请切换到 [DepthAI V2 文档](https://docs.luxonis.com/software.md)。

### DepthAI V2 与 V3 对比

了解 V2 和 V3 API 之间的主要差异。

[V2 vs V3](https://docs.luxonis.com/software-v3/depthai/tutorials/v2-vs-v3.md)

### API 参考

 * [C++](https://docs.luxonis.com/software-v3/depthai/api/cpp.md)
 * [Python](https://docs.luxonis.com/software-v3/depthai/api/python.md)
 * [API 示例](https://docs.luxonis.com/software-v3/depthai/examples.md)

### 示例

要了解 DepthAI 的实际应用，请探索 [v3 OAK 示例](https://github.com/luxonis/oak-examples/tree/main/) 仓库。这些示例展示了完整的管线和应用模式，可供您在自有项目中复用。

[探索 DepthAI 示例](https://github.com/luxonis/oak-examples/tree/main/)

### 安装

#### Linux / MacOS

### 1. Install DepthAI v3

```bash
git clone https://github.com/luxonis/depthai-core.git && cd depthai-core
python3 -m venv venv
source venv/bin/activate
# Installs library and requirements
python3 examples/python/install_requirements.py
```

or via pip:

```bash
pip install depthai --force-reinstall
```

### 2. Run an example

After installing the library, you can run an example, eg. [Detection Network
example](https://docs.luxonis.com/software-v3/depthai/examples/detection_network/detection_network.md) or [Display all
cameras](https://docs.luxonis.com/software-v3/depthai/examples/camera/camera_all.md):

```bash
cd examples/python
# Run YoloV6 detection example
python3 DetectionNetwork/detection_network.py
# Display all camera streams
python3 Camera/camera_all.py
```

#### Windows

### 1. Install DepthAI v3

```bash
git clone https://github.com/luxonis/depthai-core.git
cd depthai-core
# Create and source venv
python -m venv venv
.\venv\Scripts\Activate.ps1 # OR .\venv\Scripts\Activate.bat if you are using CMD
# Install requirements
python examples\python\install_requirements.py
```

or via pip:

```bash
pip install depthai --force-reinstall
```

### 2. Run an example

After installing the library, you can run an example, eg. [Detection Network
example](https://docs.luxonis.com/software-v3/depthai/examples/detection_network/detection_network.md) or [Display all
cameras](https://docs.luxonis.com/software-v3/depthai/examples/camera/camera_all.md):

```bash
cd examples\python
# Run YoloV6 detection example
python DetectionNetwork\detection_network.py
# Display all camera streams
python Camera\camera_all.py
```

### 组件

### DepthAI 组件

 * [节点](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes.md) 代表一个传感器、加速硬件或某项计算功能
 * [管线](https://docs.luxonis.com/software-v3/depthai/depthai-components/pipeline.md) 由链接的节点组成，部署到设备上后在加速硬件模块上运行
 * [消息](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages.md) 用于节点间的通信，包含数据与元数据
 * [设备](https://docs.luxonis.com/software-v3/depthai/depthai-components/device.md) 代表 Luxonis 的设备——OAK 或 OAK4 相机。负责连接与通信
 * [引导加载程序](https://docs.luxonis.com/software-v3/depthai/depthai-components/bootloader.md) 处理
   [RVC2](https://docs.luxonis.com/hardware/platform/rvc/rvc2.md) 设备启动时的逻辑，使其可被连接
 * [Luxonis OS](https://docs.luxonis.com/software-v3/sw-stack/luxonis-os.md) 是针对
   [RVC4](https://docs.luxonis.com/hardware/platform/rvc/rvc4.md) 设备（OAK4）的定制 Linux 发行版

### 部署 AI 模型

#### 预训练模型

[HubAI 模型库](https://models.luxonis.com/) 包含许多可直接部署到 OAK4 设备的预训练模型。除示例外，我们还在
[oak-examples](https://github.com/luxonis/oak-examples/tree/main/neural-networks) 中提供了一些神经网络示例/应用。

#### 自定义模型

您可以通过以下方式转换自定义模型：

 * （推荐）[通过 HubAI 在线转换](https://docs.luxonis.com/cloud/hubai/model-registry/detailed-conversion.md)
 * [通过 ModelConverter 离线转换](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter.md)
   (nn_archive.tar.xz)——HubAI 底层使用此工具，但 HubAI 更易用
 * [直接使用 SNPE 工具](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/snpe.md)
   (.dlc)——ModelConverter 在 RVC4 平台上底层使用 SNPE

如果你使用的是 .dlc，可以通过编辑以下代码片段将其部署到 OAK4 上：[NeuralNetwork
示例](https://docs.luxonis.com/software-v3/depthai/examples/neural_network/neural_network.md)：

```python
nn = pipeline.create(dai.node.NeuralNetwork)
nn.setModelPath('my_model.dlc')
nn.setBackend("snpe") # 指定 SNPE NN 后端。通常会在底层自动设置
# 指定 SNPE (RVC4) 特定设置，如 DSP 运行时和 NN 性能配置文件
nn.setBackendProperties({"runtime": "dsp", "performance_profile": "default"})
```

或者，如果你使用的是 archive.tar.xz，可以用以下代码片段编辑该示例：

```python
cam = pipeline.create(dai.node.Camera).build(socket)
# 如果你的神经网络模型需要 640x640 的输入尺寸（BGR）：
cam_out = cam.requestOutput((640, 640), dai.ImgFrame.Type.BGR888p)

nn_archive = dai.NNArchive('./my_nn_archive.tar.xz')
nn = pipeline.create(dai.node.NeuralNetwork).build(cam_out, nn_archive)
```
