# Deploying Kendryte Training-Platform Models on RT-Smart

```{attention}
This deployment flow uses the same single-camera dual-channel pattern as the task templates. Refer to [single_model_example.md](./single_model_example.md) for single-model tasks and [double_model_example.md](./double_model_example.md) for OCR detection plus recognition.
```

## Overview

`cloudplat_deploy_code` packages the deployment code for models exported from the Kendryte online training platform. Build the executable under `rtos_sdk`, then deploy the exported model package to the board.

For the firmware build flow, see:

[how_to_build](../../userguide/how_to_build.md)

## Source Description

`cloudplat_deploy_code` implements all task types currently supported by the training platform:

- image classification
- object detection
- semantic segmentation
- OCR detection
- OCR recognition
- OCR detection + recognition
- metric learning
- multi-label classification

The common logic for model inference, preprocessing helpers, config parsing, and result drawing is placed in `common_files`. Each task directory then implements the task-specific inference flow.

### Code Structure

The directory layout is:

```bash
cloudplat_deploy_code_linux
├── common_files
├── classification
├── detection
├── segmentation
├── ocr_detection
├── ocr_recognition
├── ocr
├── metric_learning
├── multilabel_classification
├── utils
│   └── SourceHanSansSC-Normal-Min.ttf
├── libs
├── CMakeLists.txt
└── build.sh
```

Each task directory contains the application entry, task-specific preprocess and postprocess logic, and deployment helpers for that task type.

## Build Notes

### Parameter Configuration

You can configure the display-related parameters in:

```bash
common_files/setting.h
```

The main macros are:

| Macro | Description |
|:-:|:-:|
| `ISP_WIDTH` | ISP output width |
| `ISP_HEIGHT` | ISP output height |
| `DISPLAY_MODE` | Display mode, `0`: `1920 x 1080` LT9611, `1`: `800 x 480` ST7701 |
| `DISPLAY_WIDTH` | Display width |
| `DISPLAY_HEIGHT` | Display height |
| `AI_FRAME_WIDTH` | AI inference frame width |
| `AI_FRAME_HEIGHT` | AI inference frame height |
| `AI_FRAME_CHANNEL` | AI inference frame channel count |
| `USE_OSD` | Whether to enable OSD, `0`: disable, `1`: enable |
| `OSD_WIDTH` | OSD layer width for AI results |
| `OSD_HEIGHT` | OSD layer height for AI results |
| `OSD_CHANNEL` | OSD layer channel count |

These settings are mainly used to configure the board-side display path. As in the other AI guides, the AI frame resolution is the camera-side frame before preprocessing, while the model input resolution is the tensor size after preprocessing.

### Source Build

Enter `src/rtsmart/examples/ai/cloudplat_deploy_code` and run:

```bash
cd cloudplat_deploy_code

./build_app.sh

./build_app.sh classification
./build_app.sh detection
```

If you want to build all task deployment files directly, you can also run:

```bash
make -j
```

The build artifacts are collected in `k230_bin`.

### Board Deployment

Copy the generated `.elf` files, the bundled font file, the exported `kmodel`, `deploy_config.json`, and any test images to one directory on the board. Then run the command for the corresponding task.

```bash
# Classification, video inference. Input q then Enter to exit.
./classification.elf deploy_config.json None 0

# Classification, image inference.
./classification.elf deploy_config.json test.jpg 0

# Detection, video inference.
./detection.elf deploy_config.json None 0

# Detection, image inference.
./detection.elf deploy_config.json test.jpg 0

# Semantic segmentation, video inference.
./segmentation.elf deploy_config.json None 0

# Semantic segmentation, image inference.
./segmentation.elf deploy_config.json test.jpg 0

# OCR detection, video inference.
./ocr_detection.elf deploy_config.json None 0

# OCR detection, image inference.
./ocr_detection.elf deploy_config.json test.jpg 0

# OCR recognition, image inference only.
./ocr_recognition.elf deploy_config.json test.jpg 0

# OCR detection + recognition, video inference.
./ocr.elf ocrdet_deploy_config.json ocrrec_deploy_config.json None 0

# OCR detection + recognition, image inference.
./ocr.elf ocrdet_deploy_config.json ocrrec_deploy_config.json test.jpg 0

# Metric learning, video inference.
./metric_learning.elf deploy_config.json None 0

# Metric learning, image inference.
./metric_learning.elf deploy_config.json test.jpg 0

# Multi-label classification, video inference.
./multilabel_classification.elf deploy_config.json None 0

# Multi-label classification, image inference.
./multilabel_classification.elf deploy_config.json test.jpg 0
```

The last argument is the debug level and typically follows the same convention as the other AI samples:

- `0`: no debug output
- `1`: timing-oriented debug output
- `2`: verbose debug output
