> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用自定义训练模型运行示例应用程序

> 将自定义模型与示例应用程序集成。

本指南说明如何修改现有的 Qualcomm IM SDK 参考应用程序,使其与自定义训练的模型配合工作。本文以自定义训练的 YOLOv8 模型为例。

## 使用自定义训练的 YOLOv8 LiteRT 模型

Qualcomm IM SDK 参考应用程序使用 YOLOv8 模型进行目标检测。本示例说明如何在当前参考应用程序中运行自定义训练的 YOLOv8 变体。

要运行您自己的自定义训练 YOLOv8 模型,请完成以下步骤:

1. 在参考应用程序中,用您的新模型替换现有模型。
2. 使用您的自定义标签更新标签文件。
3. 使用更新后的模型运行参考应用程序。

### 更新标签文件

Qualcomm IM SDK 参考应用程序要求标签采用 JSON 格式。请更新标签文件中每个条目的 `id`、`color` 和 `label` 值。

每个标签条目的格式为:

```json theme={null}
[
  {"id": 0, "color": "0x00FF00FF", "label": "person"}
]
```

例如:

```json theme={null}
[
  {"id": 0, "color": "0x00FF00FF", "label": "person"},
  {"id": 1, "color": "0x00FF00FF", "label": "bicycle"},
  {"id": 2, "color": "0x0000FFFF", "label": "car"},
  {"id": 3, "color": "0x00FF00FF", "label": "motorcycle"}
]
```

### 使用自定义模型运行目标检测

要使用您的自定义模型和标签文件通过 LiteRT 运行时运行目标检测,请完成以下步骤:

<Steps>
  <Step title="在主机上设置用户环境变量">
    ```shell theme={null}
    export USER=root
    ```
  </Step>

  <Step title="创建工件目录">
    ```shell theme={null}
    ssh $USER@<IP_ADDRESS_OF_TARGET_DEVICE>
    ```

    ```shell theme={null}
    mkdir -p ${HOME}/Downloads/qimsdk_samples/media ${HOME}/Downloads/qimsdk_samples/labels ${HOME}/Downloads/qimsdk_samples/models
    ```
  </Step>

  <Step title="将模型复制到设备">
    ```shell theme={null}
    scp yolov8_custom.tflite $USER@<IP_ADDRESS_OF_TARGET_DEVICE>:~/Downloads/qimsdk_samples/models/
    ```
  </Step>

  <Step title="将标签文件复制到设备">
    ```shell theme={null}
    scp yolov8_custom.json $USER@<IP_ADDRESS_OF_TARGET_DEVICE>:~/Downloads/qimsdk_samples/labels/
    ```
  </Step>

  <Step title="使用 SSH 登录设备">
    ```shell theme={null}
    ssh $USER@<IP_ADDRESS_OF_TARGET_DEVICE>
    ```
  </Step>

  <Step title="使用示例配置文件运行目标检测应用程序">
    ```shell theme={null}
    gst-ai-object-detection --config-file=/etc/configs/config_detection.json
    ```

    此命令使用 `/etc/configs/config_detection.json` 中的示例配置运行应用程序。

    要停止应用程序,请按 `Ctrl+C`。
  </Step>

  <Step title="(可选)使用自定义配置文件运行目标检测应用程序">
    您可以通过 `--config-file` 指定路径来使用自定义配置文件:

    ```bash theme={null}
    gst-ai-object-detection --config-file=/path/to/config_detection.json
    ```

    如果指定了 `--config-file`,应用程序将使用该文件。否则,它会按以下顺序搜索 `config_detection.json`:

    | 优先级 | 位置 | 描述 |
    | :-: | :- | :- |
    | 1 | 通过 `--config-file` 提供的路径。 | 使用在命令行中指定的配置文件。 |
    | 2 | `$XDG_CONFIG_HOME/qimsdk_samples/config_detection.json` | 设置了该环境变量时,使用用户自定义的配置目录。 |
    | 3 | `$HOME/.config/qimsdk_samples/config_detection.json` | 在 `XDG_CONFIG_HOME` 中未找到文件时,使用默认的用户配置目录。 |
    | 4 | `/etc/configs/config_detection.json` | 使用系统级配置文件作为最终回退。 |

    要创建用户专属配置,请将示例文件复制到用户配置目录:

    ```bash theme={null}
    export XDG_CONFIG_HOME="$HOME/.config"
    mkdir -p "$XDG_CONFIG_HOME/qimsdk_samples"
    cp /etc/configs/config_detection.json "$XDG_CONFIG_HOME/qimsdk_samples/config_detection.json"
    ```

    要查看可用的命令行选项,请运行:

    ```bash theme={null}
    gst-ai-object-detection -h
    ```

    编辑 `config_detection.json` 配置文件

    ```json theme={null}
    {
       "file-path": "video.mp4",
       "ml-framework": "tflite",
       "yolo-model-type": "yolox",
       "model": "yolox_quantized.tflite",
       "labels": "yolox.json",
       "threshold": 40,
       "runtime": "dsp",
       "output-type": "waylandsink"
    }

    ```

    <Note>
      应用程序使用 `$HOME/Downloads/qimsdk_samples` 作为其工件目录。指定相对路径或文件名时,会相对于下方对应的子目录进行解析。绝对路径则按原样使用。

      | 配置字段 | 相对路径的解析结果 |
      | :- | :- |
      | `file-path` | `$HOME/Downloads/qimsdk_samples/media/video.mp4` |
      | `model` | `$HOME/Downloads/qimsdk_samples/models/yolov8_custom.tflite` |
      | `labels` | `$HOME/Downloads/qimsdk_samples/labels/yolov8_custom.json` |
    </Note>

    运行应用程序:

    ```bash theme={null}
    gst-ai-object-detection
    ```
  </Step>
</Steps>

运行时,应用程序会在连接的显示器上显示视频流,并在每一帧上叠加边界框结果。
