# DepthAI Pipeline Graph

The [DepthAI Pipeline Graph](https://github.com/luxonis/depthai_pipeline_graph) tool creates dynamic visualizations of [DepthAI
pipelines](https://docs.luxonis.com/software/depthai-components/pipeline.md). It's an ideal tool for debugging, providing insights
into the pipeline's structure and flow.

## How It Works

In the DepthAI framework, a pipeline consists of various nodes and connections between them. After defining the pipeline in your
code, you typically pass it to the device.

At this point, the pipeline configuration is serialized to JSON and sent to the OAK device. If you set the environment variable
DEPTHAI_LEVEL=debug before running your code, the JSON configuration will be printed to the console:

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

The pipeline_graph tool performs the following steps:

 * Sets DEPTHAI_LEVEL=debug
 * Executes your code
 * Captures the schema dump from the output
 * Parses the schema to create a visual graph using a modified version of [NodeGraphQt](https://github.com/jchanvfx/NodeGraphQt)

## Installation

To install the tool, run:

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

If a Qt binding (e.g., PySide2) is missing, you'll get an error. You can resolve this by installing PySide2:

```bash
pip install PySide2
```

## Running the Tool

Once installed, you can use the tool by typing:

```bash
pipeline_graph -h
```

This will show you the available actions, including run, from_file, and load.

### Available Actions

 * Run: Execute your DepthAI script and generate the pipeline graph in real-time.

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

Use the -dnk option to allow your script to continue running after the schema has been captured:

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

 * From File: Generate a pipeline graph from a log file or JSON schema:

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

 * Load: Load and edit a previously saved pipeline graph:

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

## Customization Options

### Node Naming

By default, nodes are named based on their type (e.g., ColorCamera (0)), with an index indicating the order of creation. If there
are many nodes of the same type, use the -var or --use_variable_names option to replace the index with the variable name from your
code for better clarity.

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

The tool must know the variable name of the pipeline. "pipeline" is the default. Use the -p or --pipeline_name argument to specify
another name.

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

### Interacting with the Graph

Once the graph is displayed, you can perform several actions to customize it:

 * Rearrange Nodes: Simply drag and drop the nodes to adjust their layout if the default positioning doesn't suit your needs.
 * Rename Nodes: Double-click a node's name to edit it directly.
 * Change Node Appearance: Double-click on the node itself (outside the name area) to open a window where you can modify its color
   and name.
 * Context Menu Options: Right-click anywhere on the graph to bring up a menu. From here, you can save the graph as a JSON file,
   load a previously saved graph, or access other options.

Credits

This tool is built using the [NodeGraphQt](https://github.com/jchanvfx/NodeGraphQt) framework by jchanvfx.
