# Linux Deployment of Kanzhi Online Training Platform Model

## Overview

`cloudplat_deploy_code_linux` encapsulates the deployment code for the Kanzhi online training platform model. Users need to compile the executable file under `k230_linux_sdk` to deploy the model obtained from the training platform.

## Source Code Description

The `cloudplat_deploy_code_linux` code implements a total of 8 tasks supported by the training platform: image classification, object detection, semantic segmentation, OCR detection, OCR recognition, dual-model task OCR detection + recognition, metric learning (image featurization), and multi-label classification. The code encapsulates the common parts of model inference, preprocessing utility methods, configuration file parsing, and result drawing, which are placed in the `common_files` directory. Other directories respectively store the inference code for the corresponding tasks.

### Code Structure

The following is the description of code files:

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

## Build Instructions

### Set up the environment and build the firmware

Use the following commands to set up the linux_sdk environment and complete the firmware compilation for the corresponding development board:

```shell
# Download k230_linux_sdk
git clone https://github.com/kendryte/k230_linux_sdk.git

# Download the compilation toolchain
wget https://kendryte-download.canaan-creative.com/k230/downloads/dl/gcc/Xuantie-900-gcc-linux-6.6.0-glibc-x86_64-V3.0.2-20250410.tar.gz

# Extract the toolchain to /opt/toolchain
mkdir -p /opt/toolchain;
tar -zxvf Xuantie-900-gcc-linux-6.6.0-glibc-x86_64-V3.0.2-20250410.tar.gz -C /opt/toolchain;

# Install dependencies
apt-get install -y   git sed make binutils build-essential diffutils gcc  g++ bash patch gzip \
        bzip2 perl  tar cpio unzip rsync file  bc findutils wget  libncurses-dev python3  \
        libssl-dev gawk cmake bison flex  bash-completion parted curl

# Select the corresponding development board configuration file and build the firmware
make CONF=k230_canmv_lckfb_defconfig  BR2_PRIMARY_SITE=https://kendryte-download.canaan-creative.com/k230/downloads/dl/
```

After the firmware is built successfully, flash the firmware onto the development board.

### Build the source code

Enter the `k230_linux_sdk/buildroot-overlay/package/` directory, **build the source code**:

```shell
# Enter the directory
cd cloudplat_deploy_code_linux

# Build the files, all task-compiled elf files will be obtained in the k230_bin directory
./build.sh

# If you only want to build the deployment file for a specific task, you can use ./build.sh <task_name>
./build.sh classification
./build.sh detection
...

```

The build artifacts are in the `k230_bin` directory.

### On-board deployment

Copy the obtained `elf files`, font files, and the `kmodel`, `deploy_config.json` obtained from the Kendryte training platform, along with the test images, to a directory on the development board, and run the commands:

```shell
# Classification - video inference, enter `q` and press Enter to exit video inference
./classification.elf deploy_config.json None 0

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

# Detection - video inference, enter `q` and press Enter to exit video inference
./detection.elf deploy_config.json None 0

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

# Semantic segmentation - video inference, enter `q` and press Enter to exit 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, enter `q` and press Enter to exit 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, this task only supports image inference
./ocr_recognition.elf deploy_config.json test.jpg 0

# OCR - video inference, enter `q` and press Enter to exit video inference
./ocr.elf ocrdet_deploy_config.json ocrrec_deploy_config.json None 0

# OCR - image inference
./ocr.elf ocrdet_deploy_config.json ocrrec_deploy_config.json test.jpg 0

# Metric learning - video inference, enter `q` and press Enter to exit 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, enter `q` and press Enter to exit video inference
./multilabel_classification.elf deploy_config.json None 0

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