# DepthAI 管道图

[DepthAI 管道图](https://github.com/luxonis/depthai_pipeline_graph) 工具可创建 [DepthAI
管道](https://docs.luxonis.com/software/depthai-components/pipeline.md) 的动态可视化。它是调试的理想工具， 可帮助了解管道的结构和流程。

## 工作原理

在 DepthAI 框架中，管道由各种节点以及节点间的连接组成。在代码中定义管道后，通常将其传递给设备。

此时，管道配置会被序列化为 JSON 并发送到 OAK 设备。如果在运行代码前设置环境变量 DEPTHAI_LEVEL=debug，JSON 配置将打印到控制台：

```bash
[2022-06-01 16:47:33.721] [debug] 模式转储：{"connections":[{"node1Id":8,"node1Output":"passthroughDepth","node1OutputGroup":"","node2Id":10,"node2Input":"in","node2InputGroup":""},...]
```

pipeline_graph 工具执行以下步骤：

 * 设置 DEPTHAI_LEVEL=debug
 * 运行你的代码
 * 从输出中捕获模式转储
 * 解析模式，使用修改版的 [NodeGraphQt](https://github.com/jchanvfx/NodeGraphQt) 创建可视化图形

## 安装

要安装该工具，请运行：

```bash
pip install git+https://github.com/luxonis/depthai_pipeline_graph.git
```

如果缺少 Qt 绑定（例如 PySide2），将会出现错误。你可以通过安装 PySide2 来解决：

```bash
pip install PySide2
```

## 运行工具

安装完成后，可以通过输入以下命令来使用该工具：

```bash
pipeline_graph -h
```

这将显示可用操作，包括 run、from_file 和 load。

### 可用操作

 * 运行：执行你的 DepthAI 脚本并实时生成管道图。

```bash
pipeline_graph run "python your_script.py"
```

使用 -dnk 选项可以让脚本在捕获模式后继续运行：

```bash
pipeline_graph run "python your_script.py" -dnk
```

 * 从文件：从日志文件或 JSON 模式生成管道图：

```bash
pipeline_graph from_file path/to/your/log_or_json_file
```

 * 加载：加载并编辑之前保存的管道图：

```bash
pipeline_graph load path/to/your/saved_graph.json
```

## 自定义选项

### 节点命名

默认情况下，节点根据其类型命名（例如 ColorCamera (0)），索引表示创建顺序。 如果同一类型的节点很多，可以使用 -var 或 --use_variable_names 选项，将索引替换为代码中的变量名，以提高清晰度。

```bash
pipeline_graph run "python your_script.py" -var
```

工具必须知道管道的变量名。默认是 "pipeline"。使用 -p 或 --pipeline_name 参数指定其他名称。

```bash
pipeline_graph run "python your_script.py" -var -p custom_pipeline_name
```

### 与图形交互

显示图形后，你可以执行以下操作来自定义它：

 * 重新排列节点：只需拖放节点来调整布局，如果默认位置不满足需求。
 * 重命名节点：双击节点名称直接编辑。
 * 更改节点外观：双击节点本身（不在名称区域）打开窗口，可以修改其颜色和名称。
 * 右键菜单选项：在图形上任意位置右键单击弹出菜单。可以在此保存图形为 JSON 文件、加载之前保存的图形或访问其他选项。

致谢

该工具基于 jchanvfx 的 [NodeGraphQt](https://github.com/jchanvfx/NodeGraphQt) 框架构建。
