This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
products:sbc:edge-2l:npu:rknn-sdk-quick-start [2025/10/21 23:21] william created |
products:sbc:edge-2l:npu:rknn-sdk-quick-start [2026/05/31 23:55] (current) nick [Run RKNN Python demo] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== RKNN SDK Quick Start ====== | + | ====== RKNN SDK quick start ====== |
| ===== Introduction ===== | ===== Introduction ===== | ||
| - | This document provides a detailed introduction for beginners on how to quickly use RKNN-Toolkit2 on a computer to perform model conversion and deploy it to a edg2l board using RKNPU2. The examples used in this document are integrated into the RKNN Model Zoo. | + | This document provides a detailed introduction for beginners on how to quickly use RKNN-Toolkit2 on a computer to perform model conversion and deploy it to a Edge 2L board using RKNPU2. The examples used in this document are integrated into the [[https:// |
| - | ===== Prepare | + | ===== Prepare |
| - | The version information we are using is as follows: | + | The version information we are using is as follows: |
| - | python: 3.8 \\ | + | * Host PC Python: 3.8 |
| - | rknn-toolkit2: | + | |
| - | rknn model zoo: 2.3.2 \\ | + | |
| - | ==== Download RKNN Related Repositories | + | |
| - | It is recommended to create a new directory to store the RKNN repositories. For example, create a folder named "Projects" | + | ==== Download RKNN related repositories - Host PC ==== |
| + | It is recommended to create a new directory to store the RKNN repositories. For example, create a folder named '' | ||
| ```shell | ```shell | ||
| Line 25: | Line 26: | ||
| ``` | ``` | ||
| - | ==== Install the RKNN-Toolkit2 | + | ==== Install the RKNN-Toolkit2 |
| - | === Install Python === | + | === Install Python |
| If the Python 3.8 environment is not installed on your system, or if there are multiple versions of Python installed, it is recommended to use Miniforge Conda to create a new Python 3.8 environment. | If the Python 3.8 environment is not installed on your system, or if there are multiple versions of Python installed, it is recommended to use Miniforge Conda to create a new Python 3.8 environment. | ||
| - | === Install Miniforge Conda === | + | === Install Miniforge Conda - Host PC === |
| Download the Miniforge Conda installer from the following link: | Download the Miniforge Conda installer from the following link: | ||
| Line 36: | Line 37: | ||
| $ wget -c https:// | $ wget -c https:// | ||
| ``` | ``` | ||
| + | |||
| Then, install Miniforge Conda using the following command: | Then, install Miniforge Conda using the following command: | ||
| ```shell | ```shell | ||
| Line 41: | Line 43: | ||
| $ bash Miniforge3-Linux-x86_64.sh | $ bash Miniforge3-Linux-x86_64.sh | ||
| ``` | ``` | ||
| - | === Create Python | + | === Create Python |
| + | In the terminal window on the computer, execute the following command to switch to the Miniforge conda base | ||
| + | environment: | ||
| ```shell | ```shell | ||
| $ source ~/ | $ source ~/ | ||
| ``` | ``` | ||
| + | Create a Python 3.8 environment named '' | ||
| ```shell | ```shell | ||
| $ conda create -n py38Toolkit2.3.2 python=3.8 | $ conda create -n py38Toolkit2.3.2 python=3.8 | ||
| ``` | ``` | ||
| + | Activate the '' | ||
| ```shell | ```shell | ||
| $ conda activate py38Toolkit2.3.2 | $ conda activate py38Toolkit2.3.2 | ||
| ``` | ``` | ||
| + | === Install RKNN-Toolkit2 - Host PC === | ||
| + | Install via local wheel packag. | ||
| + | ```shell | ||
| + | $ cd rknn-toolkit2/ | ||
| + | $ pip3 install -r requirements_cp38-2.3.2.txt | ||
| + | $ pip3 install ./ | ||
| + | ``` | ||
| - | ==== Install | + | === Check if the RKNN-Toolkit2 environment - Host PC === |
| + | Switch to Python interactive mode | ||
| + | ```shell | ||
| + | $ python | ||
| + | ``` | ||
| + | Import the RKNN class | ||
| + | ```shell | ||
| + | $ from rknn.api import RKNN | ||
| + | ``` | ||
| + | |||
| + | ==== Install | ||
| + | === Android - Install NDK for Android system === | ||
| + | Note: This section is applicable to development boards with the Android system. If the board is running on the | ||
| + | Linux system, please skip this section.\\ | ||
| + | Download the Android NDK from the following link: | ||
| + | ```shell | ||
| + | $ wget -c https:// | ||
| + | ``` | ||
| + | |||
| + | Extract Android NDK files | ||
| + | ```shell | ||
| + | $ unzip android-ndk-r19c-linux-x86_64.zip | ||
| + | ``` | ||
| + | |||
| + | ==== Linux - Install GCC Cross-Compiler for Linux system ==== | ||
| + | Note: This section is applicable to development boards with the Linux system. If the board is running on the | ||
| + | Android system, please skip this section.\\ | ||
| + | Download the GCC Cross-Compiler from the following link: | ||
| + | ```shell | ||
| + | $ wget -c https:// | ||
| + | ``` | ||
| + | Extract GCC Cross-Compiler files | ||
| + | ```shell | ||
| + | $ tar -xJvf gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.xz | ||
| + | ``` | ||
| + | |||
| + | ===== Run example programs ===== | ||
| + | |||
| + | ==== Prepare model ==== | ||
| + | Switch to the '' | ||
| + | script will download an available YOLOv5 ONNX model and store it in the current model directory. Refer to the | ||
| + | following commands: | ||
| + | |||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ chmod a+x download_model.sh && ./ | ||
| + | ``` | ||
| + | |||
| + | ==== Model conversion ==== | ||
| + | Switch to the '' | ||
| + | the original ONNX model to the RKNN model. Refer to the following commands: | ||
| + | |||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ chmod a+x convert.py && python convert.py ../ | ||
| + | ``` | ||
| + | |||
| + | ==== Run RKNN Python demo ==== | ||
| + | === Android === | ||
| + | Switch to the '' | ||
| + | YOLOv5 model on the development board using on-board debugging. Refer to the following command: | ||
| + | |||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ chmod a+x yolov5.py && python yolov5.py --model_path ../ | ||
| + | ``` | ||
| + | |||
| + | === Linux === | ||
| + | |||
| + | Running on a Ubuntu 24.04 system which ship the python version of 3.12. | ||
| + | |||
| + | Download the model zoo to your Edge 2L: | ||
| + | |||
| + | ```shell | ||
| + | git clone -b v2.3.2 https:// | ||
| + | ``` | ||
| + | |||
| + | Install the python virtual environment: | ||
| + | |||
| + | ```shell | ||
| + | sudo apt update | ||
| + | sudo apt install python3.12-venv -y | ||
| + | ``` | ||
| + | |||
| + | Create the python virtual environment: | ||
| + | ```shell | ||
| + | python3 -m venv myenv312 | ||
| + | ``` | ||
| + | |||
| + | Activate the python virtual environment: | ||
| + | |||
| + | ```shell | ||
| + | source myenv312/ | ||
| + | (myenv312) khadas@Khadas: | ||
| + | Python 3.12.3 | ||
| + | ``` | ||
| + | |||
| + | Install the python package from '' | ||
| + | |||
| + | ```shell | ||
| + | (myenv312) khadas@Khadas: | ||
| + | ``` | ||
| + | |||
| + | Install the runtime libraries from '' | ||
| + | |||
| + | ```shell | ||
| + | sudo cp librknnrt.so /usr/lib/ | ||
| + | ``` | ||
| + | |||
| + | Install the OpenCV: | ||
| + | |||
| + | ```shell | ||
| + | (myenv312) khadas@Khadas: | ||
| + | ``` | ||
| + | |||
| + | Copy the converted model in [[# | ||
| + | |||
| + | ```shell | ||
| + | adb push examples/ | ||
| + | ``` | ||
| + | |||
| + | Modify the demo code: | ||
| + | |||
| + | ```diff | ||
| + | diff --git a/ | ||
| + | index 21b4132..46b24d4 100644 | ||
| + | --- a/ | ||
| + | +++ b/ | ||
| + | @@ -1,5 +1,5 @@ | ||
| + | -from rknn.api import RKNN | ||
| + | - | ||
| + | +from rknnlite.api import RKNNLite as RKNN | ||
| + | +import numpy as np | ||
| + | |||
| + | class RKNN_model_container(): | ||
| + | def __init__(self, | ||
| + | @@ -9,10 +9,7 @@ class RKNN_model_container(): | ||
| + | | ||
| + | |||
| + | | ||
| + | - if target==None: | ||
| + | - ret = rknn.init_runtime() | ||
| + | - else: | ||
| + | - ret = rknn.init_runtime(target=target, | ||
| + | + ret = rknn.init_runtime() | ||
| + | if ret != 0: | ||
| + | | ||
| + | | ||
| + | @@ -33,10 +30,12 @@ class RKNN_model_container(): | ||
| + | | ||
| + | | ||
| + | |||
| + | + inputs = [np.expand_dims(item, | ||
| + | + | ||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | def release(self): | ||
| + | | ||
| + | - self.rknn = None | ||
| + | \ No newline at end of file | ||
| + | + self.rknn = None | ||
| + | (myenv312) khadas@Khadas: | ||
| + | ``` | ||
| + | |||
| + | Run the Yolov5 demo: | ||
| + | |||
| + | ```shell | ||
| + | (myenv312) khadas@Khadas: | ||
| + | (myenv312) khadas@Khadas: | ||
| + | ``` | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Deactivate the python virtual enviranment: | ||
| + | |||
| + | ```shell | ||
| + | (myenv312) khadas@Khadas: | ||
| + | ``` | ||
| + | |||
| + | |||
| + | ==== Run RKNN C demo ==== | ||
| + | To run a RKNN C demo, you need to first compile the C/C++ source code into an executable file. After that, push | ||
| + | the executable file, model files, input images, and other related files to the development board. Finally, execute | ||
| + | the executable file on the development board. | ||
| + | |||
| + | === Android === | ||
| + | In the '' | ||
| + | |||
| + | ```shell | ||
| + | $ ANDROID_NDK_PATH=Projects/ | ||
| + | ``` | ||
| + | Then, in the '' | ||
| + | |||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ ./ | ||
| + | ``` | ||
| + | |||
| + | Push Files to the Edge 2L board: | ||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ adb root | ||
| + | $ adb push install/ | ||
| + | ``` | ||
| + | |||
| + | Run the Demo on Edge 2L board: | ||
| + | ```shell | ||
| + | $ adb shell | ||
| + | $ cd / | ||
| + | $ export LD_LIBRARY_PATH=./ | ||
| + | $ ./ | ||
| + | ``` | ||
| + | === Linux === | ||
| + | In the '' | ||
| + | |||
| + | ```shell | ||
| + | $ GCC_COMPILER=Projects/ | ||
| + | ``` | ||
| + | Then, in the '' | ||
| + | |||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ ./ | ||
| + | ``` | ||
| + | |||
| + | Push Files to the Edge 2L board: | ||
| + | ```shell | ||
| + | $ cd Projects/ | ||
| + | $ adb push install/ | ||
| + | ``` | ||
| + | |||
| + | Run the Demo on Edge 2L board: | ||
| + | ```shell | ||
| + | $ adb shell | ||
| + | $ cd / | ||
| + | $ export LD_LIBRARY_PATH=./ | ||
| + | $ ./ | ||
| + | ``` | ||
| + | |||
| + | ==== View results ==== | ||
| + | By default, the output image is saved at the path '' | ||
| + | from the board to the local machine. In the local computer terminal, execute the following command: | ||
| + | ```shell | ||
| + | $ adb pull / | ||
| + | ``` | ||
| + | {{: | ||
| - | ==== Install RKNPU2 Environment on the Board ==== | + | ===== See also ===== |
| - | ===== Run Example Programs ===== | + | [[https:// |
| - | ===== Important Note ===== | ||