一,前言
这是一个关于从电脑安装深度学习环境到实现YOLOv8目标检测在RK3588上部署的全过程。
本人配置:
1,一台笔记本
2,一个香橙派5s
二,深度学习环境配置
2.1 安装anaconda
使用清华镜像源下载https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/根据自己的电脑选择下载安装
安装之后把下面几个Anaconda的环境变量添加进系统变量中。
2.2 安装Pycharm
去Pycharm官网下载https://www.jetbrains.com/pycharm/download/,有专业版(收费)和社区版。
2.3 安装CUDA与cuDNN
参考https://blog.youkuaiyun.com/zyq880625/article/details/138086225
2.3.1安装CUDA
win+R打开cmd,输入nvidia-smi来查看自己可以安装的CUDA版本(不能超过12.2)。
nvidia-smi
- 1
CUDA下载地址CUDA Toolkit Archive | NVIDIA Developer,选择一个不高与自己CUDA的版本,然后进入选择自己电脑的配置最后下载安装。
安装过程
1、双击下载的EXE安装包,开始安装;
2、提取安装文件的(临时)存放位置,保持默认,点击OK,等待文件提取完成;
3、等待检查系统兼容性;
4、许可协议,点击同意并继续;
5、如果是第一次安装,选择精简(精简版本是下载好所有组件,并且会覆盖原有驱动),一直点击下一步即可,安装完成关闭即可;
查看是否安装成功
在cmd中输入nvcc -V
nvcc -V
- 1
2.3.2 安装cuDNN
去cuDNN Archive | NVIDIA Developer选择与自己CUDA合适的版本下载,下载后将压缩包解压
将解压后的文件夹中的bin、include、lib 三个文件夹,移动到CUDA Development 安装路径下,
与同名文件夹合并。
CUDA Development 默认安装路径:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0
- 1
添加至系统变量:
往系统环境变量中的 path 添加如下路径(根据自己的路径进行修改)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp
- 1
- 2
- 3
- 4
2.4 安装GPU版本的Pytorch
打开 Anaconda prompt
使用 conda create -n 虚拟环境名字 虚拟环境的python版本 来创建虚拟环境
conda create -n pytorch python=3.9
- 1
创建完虚拟环境后,再激活虚拟环境,,左边括号表示现在所处的环境
conda activate pytorch
- 1
pytorch 的下载链接:download.pytorch.org/whl/torch_stable.html,torch,torchvision的对应关系如下
因为我创建的pytorch环境的python是3.9所以我选择的是torch=2.0.0,torchvision=0.15.1
下载完成后在Anaconda prompt使用下面命令安装
pip install torch-2.0.0+cu118-cp39-cp39-win_amd64.whl
pip install torchvision-0.15.1+cu118-cp39-cp39-win_amd64.whl
- 1
- 2
安装完成后在Anaconda prompt中打开python,使用下面命令查看是否可以使用GPU。
import torch
torch.cuda.is_available()
- 1
- 2
返回True则表示安装成功。
三,训练自己的模型
3.1 YOLO环境配置
下载瑞星微提供的工程https://github.com/airockchip/ultralytics_yolov8.git,不要使用YOLO官方提供的,下载完成后解压
使用Pycharm打开刚才解压的工程文件如下所示,然后点击右下角选择添加解释器
找到自己Anaconda的位置,然后选择自己创建的环境的解释器。
在下方点开终端选择Command Prompt就可以执行Anaconda prompt下的操作
使用下面的命令安装YOLOv8需要的依赖
pip install ultralytics
- 1
3.2 训练自己的数据集
3.2.1 数据集制作
在工程下面新建一个my_dataset文件夹用来存放数据集的图片images和标签labels。train和va和test的比例通常为8:1:1
my_dataset
│
└───images
│ train
│ val
│ test
│
└───labels
│ train
│ val
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
若有其他格式的标签可以网上找代码转换成YOLO格式的,也可以使用labelimg进行数据集进行标注
安装labelimg
pip install labelimg
- 1
在终端输入下面命令直接打开
labelimg
- 1
标记完成后可以打开标签文件查看
3.2.2 修改配置文件
修改ultralytics/cfg/models/v8/yolov8.yaml文件中的nc为自己的类别数
ultralytics_yolov8-main/ultralytics/cfg/datasets下新建my_dataset.yaml,与文件夹下其他yaml内容文件类似指明训练,验证的路径和自己类别的标签
path: E:/multimodal_learning/Model_deployment/yolov8_test/ultralytics_yolov8-main/my_dataset # dataset root dir
train: ./images/train
val: ./images/val
test: ./images/test
# Classes 修改为自己的标签
names:
0: cat
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3.2.3 开始训练
如果想要使用YOLO官网的预训练权重的可以在Detect - Ultralytics YOLO Docs 下载预训练权重,此处下载的是YOLOv8n的权重文件
在根目录下新建一个train.py使用下面的程序,并把下载的预训练权重文件移动过来,鼠标右键后点击训练
from ultralytics import YOLO
if name == ‘main’:
model = YOLO(‘./ultralytics/cfg/models/v8/yolov8.yaml’).load(“./yolov8n.pt”)#创建模型并加载预训练权重
<span class="token comment"># Train the model</span>
model<span class="token punctuation">.</span>train<span class="token punctuation">(</span>epochs<span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">,</span> batch<span class="token operator">=</span><span class="token number">8</span> <span class="token punctuation">,</span>data<span class="token operator">=</span><span class="token string">'./ultralytics/cfg/datasets/my_dataset.yaml'</span><span class="token punctuation">)</span>
<span class="token comment">#epochs表示训练的轮数,batch表示每次加载图片的数量,data是数据集的路径这里使用的是之前创建的yaml文件</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
训练结束R结果保存在 runs\detect\train下面,可以点开验证图片进行查看,weights保存着best.pt是我们训练出来的最好的权重文件
可以新建一个predict.py来测试,加载自己训练的权重文件对测试图片进行预测,保存路径会打印出来
from ultralytics import YOLO
model = YOLO(“./runs/detect/train/weights/best.pt”) # load a custom model
# Predict with the model
results = model(“./my_dataset/images/test/000000000650.jpg”,save=True)
- 1
- 2
- 3
- 4
- 5
- 6
3.3 导出onnx模型
新建一个py文件运行后会在 runs\detect\train\weights下生成best.onnx文件,
from ultralytics import YOLO
# Load a model
model = YOLO(‘./runs/detect/train/weights/best.pt’)
# Export the model
model.export(format=‘rknn’,opset=12)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
可以使用Netron来查看自己的模型,这里查看下导出的onnx文件。
四,模型转换
4.1 转换环境安装
把onnx模型转换成rknn模型需要一个linux环境的电脑,如果没有可以在电脑上安装虚拟机,这里使用一台Ubuntu环境的服务器。
在服务器中也需要安装Anaconda和Pycharm与前面2.1节和2.2节类似,这里不再赘述。
先创建一个rk3588的虚拟环境
conda create -n rk3588 python=3.9
- 1
进入rk3588环境
conda activate rk3588
- 1
下载rknn-toolkit2https://github.com/airockchip/rknn-toolkit2.git我选择的1.6.0的版本
解压后进入rknn-toolkit2-1.6.0/rknn-toolkit2/packages/中
安装依赖
pip install -r requirements_cp39-1.6.0.txt -i https://mirror.baidu.com/pypi/simple
- 1
安装rknn_toolkit2
pip install rknn_toolkit2-1.6.0+81f21f4d-cp39-cp39-linux_x86_64.whl
- 1
安装完成后进入python,使用下面命令没有报错,则安装成功。
from rknn.api import RKNN
- 1
4.2 转换成rknn模型
新建一个yolov8_test的文件夹来存放我们的文件
best.onnx是我们3.3导出的onnx模型,dataset下面放一张测试图片000000000650.jpg和dataset.txt,txt中写的是测试图片的名字。
dataset-1下面放的是我们要测试的图片,测试的结果会保存在dataset-2下。
.
├── best.onnx
├── dataset
│ ├── 000000000650.jpg
│ └── dataset.txt
├── dataset-1
│ └── 000000000650.jpg
├── dataset-2
└── inference_test-2.py
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
inference_test-2.py内容如下,包含模型转换和python部署的后处理
import os
import cv2
from rknn.api import RKNN
import numpy as np
IMG_FOLDER = “dataset-1”
RESULT_PATH = ‘./dataset-2’
#修改为自己的类别
CLASSES = [‘cat’]
OBJ_THRESH = 0.45
NMS_THRESH = 0.45
MODEL_SIZE = (640, 640)
color_palette = np.random.uniform(0, 255, size=(len(CLASSES), 3))
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def letter_box(im, new_shape, pad_color=(0, 0, 0), info_need=False):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
<span class="token comment"># Scale ratio</span>
r <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>new_shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">/</span> shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> new_shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">/</span> shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># Compute padding</span>
ratio <span class="token operator">=</span> r <span class="token comment"># width, height ratios</span>
new_unpad <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> r<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> r<span class="token punctuation">)</span><span class="token punctuation">)</span>
dw<span class="token punctuation">,</span> dh <span class="token operator">=</span> new_shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> new_unpad<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> new_shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> new_unpad<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment"># wh padding</span>
dw <span class="token operator">/=</span> <span class="token number">2</span> <span class="token comment"># divide padding into 2 sides</span>
dh <span class="token operator">/=</span> <span class="token number">2</span>
<span class="token keyword">if</span> shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> new_unpad<span class="token punctuation">:</span> <span class="token comment"># resize</span>
im <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>im<span class="token punctuation">,</span> new_unpad<span class="token punctuation">,</span> interpolation<span class="token operator">=</span>cv2<span class="token punctuation">.</span>INTER_LINEAR<span class="token punctuation">)</span>
top<span class="token punctuation">,</span> bottom <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dh <span class="token operator">-</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dh <span class="token operator">+</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
left<span class="token punctuation">,</span> right <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dw <span class="token operator">-</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dw <span class="token operator">+</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
im <span class="token operator">=</span> cv2<span class="token punctuation">.</span>copyMakeBorder<span class="token punctuation">(</span>im<span class="token punctuation">,</span> top<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> left<span class="token punctuation">,</span> right<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>BORDER_CONSTANT<span class="token punctuation">,</span> value<span class="token operator">=</span>pad_color<span class="token punctuation">)</span> <span class="token comment"># add border</span>
<span class="token keyword">if</span> info_need <span class="token keyword">is</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> im<span class="token punctuation">,</span> ratio<span class="token punctuation">,</span> <span class="token punctuation">(</span>dw<span class="token punctuation">,</span> dh<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> im
def filter_boxes(boxes, box_confidences, box_class_probs):
“”“Filter boxes with object threshold.
“””
box_confidences = box_confidences.reshape(-1)
candidate, class_num = box_class_probs.shape
class_max_score <span class="token operator">=</span> np<span class="token punctuation">.</span><span class="token builtin">max</span><span class="token punctuation">(</span>box_class_probs<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
classes <span class="token operator">=</span> np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>box_class_probs<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
_class_pos <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>class_max_score <span class="token operator">*</span> box_confidences <span class="token operator">>=</span> OBJ_THRESH<span class="token punctuation">)</span>
scores <span class="token operator">=</span> <span class="token punctuation">(</span>class_max_score <span class="token operator">*</span> box_confidences<span class="token punctuation">)</span><span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
boxes <span class="token operator">=</span> boxes<span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
classes <span class="token operator">=</span> classes<span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
<span class="token keyword">return</span> boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores
def nms_boxes(boxes, scores):
“”“Suppress non-maximal boxes.
# Returns
keep: ndarray, index of effective boxes.
“””
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas <span class="token operator">=</span> w <span class="token operator">*</span> h
order <span class="token operator">=</span> scores<span class="token punctuation">.</span>argsort<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
keep <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">while</span> order<span class="token punctuation">.</span>size <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">:</span>
i <span class="token operator">=</span> order<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
keep<span class="token punctuation">.</span>append<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
xx1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>x<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> x<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
yy1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>y<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
xx2 <span class="token operator">=</span> np<span class="token punctuation">.</span>minimum<span class="token punctuation">(</span>x<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> x<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
yy2 <span class="token operator">=</span> np<span class="token punctuation">.</span>minimum<span class="token punctuation">(</span>y<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> h<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> h<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
w1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> xx2 <span class="token operator">-</span> xx1 <span class="token operator">+</span> <span class="token number">0.00001</span><span class="token punctuation">)</span>
h1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> yy2 <span class="token operator">-</span> yy1 <span class="token operator">+</span> <span class="token number">0.00001</span><span class="token punctuation">)</span>
inter <span class="token operator">=</span> w1 <span class="token operator">*</span> h1
ovr <span class="token operator">=</span> inter <span class="token operator">/</span> <span class="token punctuation">(</span>areas<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> areas<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">-</span> inter<span class="token punctuation">)</span>
inds <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>ovr <span class="token operator"><=</span> NMS_THRESH<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
order <span class="token operator">=</span> order<span class="token punctuation">[</span>inds <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span>
keep <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>keep<span class="token punctuation">)</span>
<span class="token keyword">return</span> keep
def softmax(x, axis=None):
x = x - x.max(axis=axis, keepdims=True)
y = np.exp(x)
return y / y.sum(axis=axis, keepdims=True)
def dfl(position):
# Distribution Focal Loss (DFL)
n, c, h, w = position.shape
p_num = 4
mc = c // p_num
y = position.reshape(n, p_num, mc, h, w)
y = softmax(y, 2)
acc_metrix = np.array(range(mc), dtype=float).reshape(1, 1, mc, 1, 1)
y = (y * acc_metrix).sum(2)
return y
def box_process(position):
grid_h, grid_w = position.shape[2:4]
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
col = col.reshape(1, 1, grid_h, grid_w)
row = row.reshape(1, 1, grid_h, grid_w)
grid = np.concatenate((col, row), axis=1)
stride = np.array([MODEL_SIZE[1] // grid_h, MODEL_SIZE[0] // grid_w]).reshape(1, 2, 1, 1)
position <span class="token operator">=</span> dfl<span class="token punctuation">(</span>position<span class="token punctuation">)</span>
box_xy <span class="token operator">=</span> grid <span class="token operator">+</span> <span class="token number">0.5</span> <span class="token operator">-</span> position<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
box_xy2 <span class="token operator">=</span> grid <span class="token operator">+</span> <span class="token number">0.5</span> <span class="token operator">+</span> position<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
xyxy <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span><span class="token punctuation">(</span>box_xy <span class="token operator">*</span> stride<span class="token punctuation">,</span> box_xy2 <span class="token operator">*</span> stride<span class="token punctuation">)</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> xyxy
def post_process(input_data):
boxes, scores, classes_conf = [], [], []
defualt_branch = 3
pair_per_branch = len(input_data) // defualt_branch
# Python 忽略 score_sum 输出
for i in range(defualt_branch):
boxes.append(box_process(input_data[pair_per_branch i]))
classes_conf.append(input_data[pair_per_branch i + 1])
scores.append(np.ones_like(input_data[pair_per_branch * i + 1][:, :1, :, :], dtype=np.float32))
<span class="token keyword">def</span> <span class="token function">sp_flatten</span><span class="token punctuation">(</span>_in<span class="token punctuation">)</span><span class="token punctuation">:</span>
ch <span class="token operator">=</span> _in<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
_in <span class="token operator">=</span> _in<span class="token punctuation">.</span>transpose<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> _in<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ch<span class="token punctuation">)</span>
boxes <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> boxes<span class="token punctuation">]</span>
classes_conf <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> classes_conf<span class="token punctuation">]</span>
scores <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> scores<span class="token punctuation">]</span>
boxes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>boxes<span class="token punctuation">)</span>
classes_conf <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>classes_conf<span class="token punctuation">)</span>
scores <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>scores<span class="token punctuation">)</span>
<span class="token comment"># filter according to threshold</span>
boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores <span class="token operator">=</span> filter_boxes<span class="token punctuation">(</span>boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes_conf<span class="token punctuation">)</span>
<span class="token comment"># nms</span>
nboxes<span class="token punctuation">,</span> nclasses<span class="token punctuation">,</span> nscores <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> <span class="token builtin">set</span><span class="token punctuation">(</span>classes<span class="token punctuation">)</span><span class="token punctuation">:</span>
inds <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>classes <span class="token operator">==</span> c<span class="token punctuation">)</span>
b <span class="token operator">=</span> boxes<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
c <span class="token operator">=</span> classes<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
s <span class="token operator">=</span> scores<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
keep <span class="token operator">=</span> nms_boxes<span class="token punctuation">(</span>b<span class="token punctuation">,</span> s<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>keep<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
nboxes<span class="token punctuation">.</span>append<span class="token punctuation">(</span>b<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
nclasses<span class="token punctuation">.</span>append<span class="token punctuation">(</span>c<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
nscores<span class="token punctuation">.</span>append<span class="token punctuation">(</span>s<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> nclasses <span class="token keyword">and</span> <span class="token keyword">not</span> nscores<span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span>
boxes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nboxes<span class="token punctuation">)</span>
classes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nclasses<span class="token punctuation">)</span>
scores <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nscores<span class="token punctuation">)</span>
<span class="token keyword">return</span> boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores
def draw_detections(img, left, top, right, bottom, score, class_id):
“”"
Draws bounding boxes and labels on the input image based on the detected objects.
Args:
img: The input image to draw detections on.
box: Detected bounding box.
score: Corresponding detection score.
class_id: Class ID for the detected object.
Returns:
None
“”"
<span class="token comment"># Retrieve the color for the class ID</span>
color <span class="token operator">=</span> color_palette<span class="token punctuation">[</span>class_id<span class="token punctuation">]</span>
<span class="token comment"># Draw the bounding box on the image</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>top<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>right<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>bottom<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> color<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment"># Create the label text with class name and score</span>
label <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f"</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>CLASSES<span class="token punctuation">[</span>class_id<span class="token punctuation">]</span><span class="token punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>score<span class="token punctuation">:</span><span class="token format-spec">.2f</span><span class="token punctuation">}</span></span><span class="token string">"</span></span>
<span class="token comment"># Calculate the dimensions of the label text</span>
<span class="token punctuation">(</span>label_width<span class="token punctuation">,</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> _ <span class="token operator">=</span> cv2<span class="token punctuation">.</span>getTextSize<span class="token punctuation">(</span>label<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment"># Calculate the position of the label text</span>
label_x <span class="token operator">=</span> left
label_y <span class="token operator">=</span> top <span class="token operator">-</span> <span class="token number">10</span> <span class="token keyword">if</span> top <span class="token operator">-</span> <span class="token number">10</span> <span class="token operator">></span> label_height <span class="token keyword">else</span> top <span class="token operator">+</span> <span class="token number">10</span>
<span class="token comment"># Draw a filled rectangle as the background for the label text</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x<span class="token punctuation">,</span> label_y <span class="token operator">-</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x <span class="token operator">+</span> label_width<span class="token punctuation">,</span> label_y <span class="token operator">+</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> color<span class="token punctuation">,</span>
cv2<span class="token punctuation">.</span>FILLED<span class="token punctuation">)</span>
<span class="token comment"># Draw the label text on the image</span>
cv2<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>img<span class="token punctuation">,</span> label<span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x<span class="token punctuation">,</span> label_y<span class="token punctuation">)</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>LINE_AA<span class="token punctuation">)</span>
def draw(image, boxes, scores, classes):
img_h, img_w = image.shape[:2]
# Calculate scaling factors for bounding box coordinates
x_factor = img_w / MODEL_SIZE[0]
y_factor = img_h / MODEL_SIZE[1]
<span class="token keyword">for</span> box<span class="token punctuation">,</span> score<span class="token punctuation">,</span> cl <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes<span class="token punctuation">)</span><span class="token punctuation">:</span>
x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> x2<span class="token punctuation">,</span> y2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">(</span>_b<span class="token punctuation">)</span> <span class="token keyword">for</span> _b <span class="token keyword">in</span> box<span class="token punctuation">]</span>
left <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>x1 <span class="token operator">*</span> x_factor<span class="token punctuation">)</span>
top <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>y1 <span class="token operator">*</span> y_factor<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">10</span>
right <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>x2 <span class="token operator">*</span> x_factor<span class="token punctuation">)</span>
bottom <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>y2 <span class="token operator">*</span> y_factor<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'class: {}, score: {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>CLASSES<span class="token punctuation">[</span>cl<span class="token punctuation">]</span><span class="token punctuation">,</span> score<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'box coordinate left,top,right,down: [{}, {}, {}, {}]'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span> top<span class="token punctuation">,</span> right<span class="token punctuation">,</span> bottom<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># Retrieve the color for the class ID</span>
draw_detections<span class="token punctuation">(</span>image<span class="token punctuation">,</span> left<span class="token punctuation">,</span> top<span class="token punctuation">,</span> right<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> score<span class="token punctuation">,</span> cl<span class="token punctuation">)</span>
<span class="token comment"># cv2.rectangle(image, (left, top), (right, bottom), color, 2)</span>
<span class="token comment"># cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),</span>
<span class="token comment"># (left, top - 6),</span>
<span class="token comment"># cv2.FONT_HERSHEY_SIMPLEX,</span>
<span class="token comment"># 0.6, (0, 0, 255), 2)</span>
if name == ‘main’:
<span class="token comment"># 确定目标设备</span>
target <span class="token operator">=</span> <span class="token string">'RK3588'</span>
<span class="token comment"># 创建RKNN对象</span>
rknn <span class="token operator">=</span> RKNN<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 配置RKNN模型</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> config model'</span><span class="token punctuation">)</span>
rknn<span class="token punctuation">.</span>config<span class="token punctuation">(</span>
mean_values<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
std_values<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
target_platform<span class="token operator">=</span>target<span class="token punctuation">,</span>
<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 加载 .onnx模型</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> loading model'</span><span class="token punctuation">)</span>
ret <span class="token operator">=</span> rknn<span class="token punctuation">.</span>load_onnx<span class="token punctuation">(</span>model<span class="token operator">=</span><span class="token string">"./best.onnx"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"load model failed!"</span><span class="token punctuation">)</span>
rknn<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 构建RKNN模型</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> building model'</span><span class="token punctuation">)</span>
ret <span class="token operator">=</span> rknn<span class="token punctuation">.</span>build<span class="token punctuation">(</span>do_quantization<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> dataset<span class="token operator">=</span><span class="token string">"./dataset/dataset.txt"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"build model failed!"</span><span class="token punctuation">)</span>
rknn<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 导出RKNN模型</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'-->export RKNN model'</span><span class="token punctuation">)</span>
ret <span class="token operator">=</span> rknn<span class="token punctuation">.</span>export_rknn<span class="token punctuation">(</span><span class="token string">'./yolov8.rknn'</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'export RKNN model failed'</span><span class="token punctuation">)</span>
rknn<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token comment"># 初始化 runtime 环境</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> Init runtime environment'</span><span class="token punctuation">)</span>
<span class="token comment"># run on RK356x/RK3588 with Debian OS, do not need specify target.</span>
<span class="token comment">#ret = rknn.init_runtime(target='rk3588', device_id='48c122b87375ccbc')</span>
<span class="token comment"># 如果使用电脑进行模拟测试</span>
ret <span class="token operator">=</span> rknn<span class="token punctuation">.</span>init_runtime<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Init runtime environment failed!'</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 数据处理</span>
img_list <span class="token operator">=</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>IMG_FOLDER<span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>img_list<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
img_name <span class="token operator">=</span> img_list<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
img_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>IMG_FOLDER<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>img_path<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} is not found"</span><span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
<span class="token keyword">continue</span>
img_src <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span>img_path<span class="token punctuation">)</span>
<span class="token keyword">if</span> img_src <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"文件不存在\n"</span><span class="token punctuation">)</span>
<span class="token comment"># Due to rga init with (0,0,0), we using pad_color (0,0,0) instead of (114, 114, 114)</span>
pad_color <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
img <span class="token operator">=</span> letter_box<span class="token punctuation">(</span>im<span class="token operator">=</span>img_src<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> new_shape<span class="token operator">=</span><span class="token punctuation">(</span>MODEL_SIZE<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> MODEL_SIZE<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pad_color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># img = cv2.resize(img_src, (640, 512), interpolation=cv2.INTER_LINEAR) # direct resize</span>
<span class="token builtin">input</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>expand_dims<span class="token punctuation">(</span>img<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
outputs <span class="token operator">=</span> rknn<span class="token punctuation">.</span>inference<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">input</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores <span class="token operator">=</span> post_process<span class="token punctuation">(</span>outputs<span class="token punctuation">)</span>
img_p <span class="token operator">=</span> img_src<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> boxes <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
draw<span class="token punctuation">(</span>img_p<span class="token punctuation">,</span> boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes<span class="token punctuation">)</span>
<span class="token comment"># 保存结果</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">)</span><span class="token punctuation">:</span>
os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">)</span>
result_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imwrite<span class="token punctuation">(</span>result_path<span class="token punctuation">,</span> img_p<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Detection result save to {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>result_path<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">pass</span>
rknn<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
运行inference_test-2.py 文件,会在yolov8_test文件夹下生成yolov8.rknn,测试结果的图片保存在dataset-2文件夹下。
python inference_test-2.py
- 1
五,模型部署
5.1 香橙派(RK3588)环境安装
首先参考Orange Pi - Orangepi官网安装Ubuntu系统
然后打开时ssh服务,我这里使用的MobaXterm进行连接的
sudo apt-get install openssh-server
- 1
首先安装Miniconda — Anaconda documentation
下载后使用命令来安装Miniconda3
./Miniconda3-latest-Linux-aarch64.sh
- 1
更新环境变量
source /home/topeet/.bashrc
- 1
创建环境
conda create -n rknn python=3.9
- 1
激活环境
conda activate rknn
- 1
安装rknn_toolkit_lite2
在4.1下载的rknn-toolkit2-1.6.0/rknn_toolkit_lite2/packages中有rknn_toolkit_lite2-1.6.0-cp39-cp39-linux_aarch64.whl,把它移动到开发板上然后使用下面的命令安装
pip install rknn_toolkit_lite2-1.6.0-cp39-cp39-linux_aarch64.whl -i https://pypi.mirrors.ustc.edu.cn/simple/
- 1
安装opencv
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
- 1
5.2 python部署
在开发板上新建一个yolov8_test来存放文件,dataset-1是存放的测试图片,dataset-2是保存测试结果的位置,yolov8.rknn是我们的rknn模型,rk3588_test-2.py是我们的测试脚本
.
├── dataset-1
│ └── 000000000650.jpg
├── dataset-2
├── rk3588_test-2.py
└── yolov8.rknn
- 1
- 2
- 3
- 4
- 5
- 6
rk3588_test-2.py文件如下所示
import os
import cv2
from rknnlite.api import RKNNLite
import numpy as np
RKNN_MODEL = “./yolov8.rknn”
IMG_FOLDER = “dataset-1”
RESULT_PATH = ‘./dataset-2’
CLASSES = [‘car’]
OBJ_THRESH = 0.45
NMS_THRESH = 0.45
MODEL_SIZE = (640, 640)
color_palette = np.random.uniform(0, 255, size=(len(CLASSES), 3))
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def letter_box(im, new_shape, pad_color=(0, 0, 0), info_need=False):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
<span class="token comment"># Scale ratio</span>
r <span class="token operator">=</span> <span class="token builtin">min</span><span class="token punctuation">(</span>new_shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">/</span> shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> new_shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">/</span> shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># Compute padding</span>
ratio <span class="token operator">=</span> r <span class="token comment"># width, height ratios</span>
new_unpad <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> r<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> r<span class="token punctuation">)</span><span class="token punctuation">)</span>
dw<span class="token punctuation">,</span> dh <span class="token operator">=</span> new_shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> new_unpad<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> new_shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> new_unpad<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment"># wh padding</span>
dw <span class="token operator">/=</span> <span class="token number">2</span> <span class="token comment"># divide padding into 2 sides</span>
dh <span class="token operator">/=</span> <span class="token number">2</span>
<span class="token keyword">if</span> shape<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">!=</span> new_unpad<span class="token punctuation">:</span> <span class="token comment"># resize</span>
im <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>im<span class="token punctuation">,</span> new_unpad<span class="token punctuation">,</span> interpolation<span class="token operator">=</span>cv2<span class="token punctuation">.</span>INTER_LINEAR<span class="token punctuation">)</span>
top<span class="token punctuation">,</span> bottom <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dh <span class="token operator">-</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dh <span class="token operator">+</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
left<span class="token punctuation">,</span> right <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dw <span class="token operator">-</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">round</span><span class="token punctuation">(</span>dw <span class="token operator">+</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
im <span class="token operator">=</span> cv2<span class="token punctuation">.</span>copyMakeBorder<span class="token punctuation">(</span>im<span class="token punctuation">,</span> top<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> left<span class="token punctuation">,</span> right<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>BORDER_CONSTANT<span class="token punctuation">,</span> value<span class="token operator">=</span>pad_color<span class="token punctuation">)</span> <span class="token comment"># add border</span>
<span class="token keyword">if</span> info_need <span class="token keyword">is</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> im<span class="token punctuation">,</span> ratio<span class="token punctuation">,</span> <span class="token punctuation">(</span>dw<span class="token punctuation">,</span> dh<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> im
def filter_boxes(boxes, box_confidences, box_class_probs):
“”“Filter boxes with object threshold.
“””
box_confidences = box_confidences.reshape(-1)
candidate, class_num = box_class_probs.shape
class_max_score <span class="token operator">=</span> np<span class="token punctuation">.</span><span class="token builtin">max</span><span class="token punctuation">(</span>box_class_probs<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
classes <span class="token operator">=</span> np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>box_class_probs<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
_class_pos <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>class_max_score <span class="token operator">*</span> box_confidences <span class="token operator">>=</span> OBJ_THRESH<span class="token punctuation">)</span>
scores <span class="token operator">=</span> <span class="token punctuation">(</span>class_max_score <span class="token operator">*</span> box_confidences<span class="token punctuation">)</span><span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
boxes <span class="token operator">=</span> boxes<span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
classes <span class="token operator">=</span> classes<span class="token punctuation">[</span>_class_pos<span class="token punctuation">]</span>
<span class="token keyword">return</span> boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores
def nms_boxes(boxes, scores):
“”“Suppress non-maximal boxes.
# Returns
keep: ndarray, index of effective boxes.
“””
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas <span class="token operator">=</span> w <span class="token operator">*</span> h
order <span class="token operator">=</span> scores<span class="token punctuation">.</span>argsort<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
keep <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">while</span> order<span class="token punctuation">.</span>size <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">:</span>
i <span class="token operator">=</span> order<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
keep<span class="token punctuation">.</span>append<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
xx1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>x<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> x<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
yy1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span>y<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
xx2 <span class="token operator">=</span> np<span class="token punctuation">.</span>minimum<span class="token punctuation">(</span>x<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> x<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> w<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
yy2 <span class="token operator">=</span> np<span class="token punctuation">.</span>minimum<span class="token punctuation">(</span>y<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> h<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> y<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+</span> h<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
w1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> xx2 <span class="token operator">-</span> xx1 <span class="token operator">+</span> <span class="token number">0.00001</span><span class="token punctuation">)</span>
h1 <span class="token operator">=</span> np<span class="token punctuation">.</span>maximum<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> yy2 <span class="token operator">-</span> yy1 <span class="token operator">+</span> <span class="token number">0.00001</span><span class="token punctuation">)</span>
inter <span class="token operator">=</span> w1 <span class="token operator">*</span> h1
ovr <span class="token operator">=</span> inter <span class="token operator">/</span> <span class="token punctuation">(</span>areas<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> areas<span class="token punctuation">[</span>order<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">-</span> inter<span class="token punctuation">)</span>
inds <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>ovr <span class="token operator"><=</span> NMS_THRESH<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
order <span class="token operator">=</span> order<span class="token punctuation">[</span>inds <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span>
keep <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>keep<span class="token punctuation">)</span>
<span class="token keyword">return</span> keep
def softmax(x, axis=None):
x = x - x.max(axis=axis, keepdims=True)
y = np.exp(x)
return y / y.sum(axis=axis, keepdims=True)
def dfl(position):
# Distribution Focal Loss (DFL)
n, c, h, w = position.shape
p_num = 4
mc = c // p_num
y = position.reshape(n, p_num, mc, h, w)
y = softmax(y, 2)
acc_metrix = np.array(range(mc), dtype=float).reshape(1, 1, mc, 1, 1)
y = (y * acc_metrix).sum(2)
return y
def box_process(position):
grid_h, grid_w = position.shape[2:4]
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
col = col.reshape(1, 1, grid_h, grid_w)
row = row.reshape(1, 1, grid_h, grid_w)
grid = np.concatenate((col, row), axis=1)
stride = np.array([MODEL_SIZE[1] // grid_h, MODEL_SIZE[0] // grid_w]).reshape(1, 2, 1, 1)
position <span class="token operator">=</span> dfl<span class="token punctuation">(</span>position<span class="token punctuation">)</span>
box_xy <span class="token operator">=</span> grid <span class="token operator">+</span> <span class="token number">0.5</span> <span class="token operator">-</span> position<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
box_xy2 <span class="token operator">=</span> grid <span class="token operator">+</span> <span class="token number">0.5</span> <span class="token operator">+</span> position<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span>
xyxy <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span><span class="token punctuation">(</span>box_xy <span class="token operator">*</span> stride<span class="token punctuation">,</span> box_xy2 <span class="token operator">*</span> stride<span class="token punctuation">)</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> xyxy
def post_process(input_data):
boxes, scores, classes_conf = [], [], []
defualt_branch = 3
pair_per_branch = len(input_data) // defualt_branch
# Python 忽略 score_sum 输出
for i in range(defualt_branch):
boxes.append(box_process(input_data[pair_per_branch i]))
classes_conf.append(input_data[pair_per_branch i + 1])
scores.append(np.ones_like(input_data[pair_per_branch * i + 1][:, :1, :, :], dtype=np.float32))
<span class="token keyword">def</span> <span class="token function">sp_flatten</span><span class="token punctuation">(</span>_in<span class="token punctuation">)</span><span class="token punctuation">:</span>
ch <span class="token operator">=</span> _in<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
_in <span class="token operator">=</span> _in<span class="token punctuation">.</span>transpose<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> _in<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> ch<span class="token punctuation">)</span>
boxes <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> boxes<span class="token punctuation">]</span>
classes_conf <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> classes_conf<span class="token punctuation">]</span>
scores <span class="token operator">=</span> <span class="token punctuation">[</span>sp_flatten<span class="token punctuation">(</span>_v<span class="token punctuation">)</span> <span class="token keyword">for</span> _v <span class="token keyword">in</span> scores<span class="token punctuation">]</span>
boxes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>boxes<span class="token punctuation">)</span>
classes_conf <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>classes_conf<span class="token punctuation">)</span>
scores <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>scores<span class="token punctuation">)</span>
<span class="token comment"># filter according to threshold</span>
boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores <span class="token operator">=</span> filter_boxes<span class="token punctuation">(</span>boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes_conf<span class="token punctuation">)</span>
<span class="token comment"># nms</span>
nboxes<span class="token punctuation">,</span> nclasses<span class="token punctuation">,</span> nscores <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> <span class="token builtin">set</span><span class="token punctuation">(</span>classes<span class="token punctuation">)</span><span class="token punctuation">:</span>
inds <span class="token operator">=</span> np<span class="token punctuation">.</span>where<span class="token punctuation">(</span>classes <span class="token operator">==</span> c<span class="token punctuation">)</span>
b <span class="token operator">=</span> boxes<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
c <span class="token operator">=</span> classes<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
s <span class="token operator">=</span> scores<span class="token punctuation">[</span>inds<span class="token punctuation">]</span>
keep <span class="token operator">=</span> nms_boxes<span class="token punctuation">(</span>b<span class="token punctuation">,</span> s<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>keep<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
nboxes<span class="token punctuation">.</span>append<span class="token punctuation">(</span>b<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
nclasses<span class="token punctuation">.</span>append<span class="token punctuation">(</span>c<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
nscores<span class="token punctuation">.</span>append<span class="token punctuation">(</span>s<span class="token punctuation">[</span>keep<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> nclasses <span class="token keyword">and</span> <span class="token keyword">not</span> nscores<span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token boolean">None</span>
boxes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nboxes<span class="token punctuation">)</span>
classes <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nclasses<span class="token punctuation">)</span>
scores <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span>nscores<span class="token punctuation">)</span>
<span class="token keyword">return</span> boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores
def draw_detections(img, left, top, right, bottom, score, class_id):
“”"
Draws bounding boxes and labels on the input image based on the detected objects.
Args:
img: The input image to draw detections on.
box: Detected bounding box.
score: Corresponding detection score.
class_id: Class ID for the detected object.
Returns:
None
“”"
<span class="token comment"># Retrieve the color for the class ID</span>
color <span class="token operator">=</span> color_palette<span class="token punctuation">[</span>class_id<span class="token punctuation">]</span>
<span class="token comment"># Draw the bounding box on the image</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>top<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>right<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">(</span>bottom<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> color<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment"># Create the label text with class name and score</span>
label <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f"</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>CLASSES<span class="token punctuation">[</span>class_id<span class="token punctuation">]</span><span class="token punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>score<span class="token punctuation">:</span><span class="token format-spec">.2f</span><span class="token punctuation">}</span></span><span class="token string">"</span></span>
<span class="token comment"># Calculate the dimensions of the label text</span>
<span class="token punctuation">(</span>label_width<span class="token punctuation">,</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> _ <span class="token operator">=</span> cv2<span class="token punctuation">.</span>getTextSize<span class="token punctuation">(</span>label<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment"># Calculate the position of the label text</span>
label_x <span class="token operator">=</span> left
label_y <span class="token operator">=</span> top <span class="token operator">-</span> <span class="token number">10</span> <span class="token keyword">if</span> top <span class="token operator">-</span> <span class="token number">10</span> <span class="token operator">></span> label_height <span class="token keyword">else</span> top <span class="token operator">+</span> <span class="token number">10</span>
<span class="token comment"># Draw a filled rectangle as the background for the label text</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x<span class="token punctuation">,</span> label_y <span class="token operator">-</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x <span class="token operator">+</span> label_width<span class="token punctuation">,</span> label_y <span class="token operator">+</span> label_height<span class="token punctuation">)</span><span class="token punctuation">,</span> color<span class="token punctuation">,</span>
cv2<span class="token punctuation">.</span>FILLED<span class="token punctuation">)</span>
<span class="token comment"># Draw the label text on the image</span>
cv2<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>img<span class="token punctuation">,</span> label<span class="token punctuation">,</span> <span class="token punctuation">(</span>label_x<span class="token punctuation">,</span> label_y<span class="token punctuation">)</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>LINE_AA<span class="token punctuation">)</span>
def draw(image, boxes, scores, classes):
img_h, img_w = image.shape[:2]
# Calculate scaling factors for bounding box coordinates
x_factor = img_w / MODEL_SIZE[0]
y_factor = img_h / MODEL_SIZE[1]
<span class="token keyword">for</span> box<span class="token punctuation">,</span> score<span class="token punctuation">,</span> cl <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes<span class="token punctuation">)</span><span class="token punctuation">:</span>
x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> x2<span class="token punctuation">,</span> y2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">(</span>_b<span class="token punctuation">)</span> <span class="token keyword">for</span> _b <span class="token keyword">in</span> box<span class="token punctuation">]</span>
left <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>x1 <span class="token operator">*</span> x_factor<span class="token punctuation">)</span>
top <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>y1 <span class="token operator">*</span> y_factor<span class="token punctuation">)</span>
right <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>x2 <span class="token operator">*</span> x_factor<span class="token punctuation">)</span>
bottom <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>y2 <span class="token operator">*</span> y_factor<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'class: {}, score: {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>CLASSES<span class="token punctuation">[</span>cl<span class="token punctuation">]</span><span class="token punctuation">,</span> score<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'box coordinate left,top,right,down: [{}, {}, {}, {}]'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span> top<span class="token punctuation">,</span> right<span class="token punctuation">,</span> bottom<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># Retrieve the color for the class ID</span>
draw_detections<span class="token punctuation">(</span>image<span class="token punctuation">,</span> left<span class="token punctuation">,</span> top<span class="token punctuation">,</span> right<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> score<span class="token punctuation">,</span> cl<span class="token punctuation">)</span>
<span class="token comment"># cv2.rectangle(image, (left, top), (right, bottom), color, 2)</span>
<span class="token comment"># cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),</span>
<span class="token comment"># (left, top - 6),</span>
<span class="token comment"># cv2.FONT_HERSHEY_SIMPLEX,</span>
<span class="token comment"># 0.6, (0, 0, 255), 2)</span>
if name == ‘main’:
<span class="token comment"># 创建RKNN对象</span>
rknn_lite <span class="token operator">=</span> RKNNLite<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 加载RKNN模型</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> Load RKNN model'</span><span class="token punctuation">)</span>
ret <span class="token operator">=</span> rknn_lite<span class="token punctuation">.</span>load_rknn<span class="token punctuation">(</span>RKNN_MODEL<span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Load RKNN model failed'</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 初始化 runtime 环境</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'--> Init runtime environment'</span><span class="token punctuation">)</span>
<span class="token comment"># run on RK356x/RK3588 with Debian OS, do not need specify target.</span>
ret <span class="token operator">=</span> rknn_lite<span class="token punctuation">.</span>init_runtime<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> ret <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Init runtime environment failed!'</span><span class="token punctuation">)</span>
exit<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'done'</span><span class="token punctuation">)</span>
<span class="token comment"># 数据处理</span>
img_list <span class="token operator">=</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>IMG_FOLDER<span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>img_list<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
img_name <span class="token operator">=</span> img_list<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
img_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>IMG_FOLDER<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>img_path<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"{} is not found"</span><span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
<span class="token keyword">continue</span>
img_src <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span>img_path<span class="token punctuation">)</span>
<span class="token keyword">if</span> img_src <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"文件不存在\n"</span><span class="token punctuation">)</span>
<span class="token comment"># Due to rga init with (0,0,0), we using pad_color (0,0,0) instead of (114, 114, 114)</span>
pad_color <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
img <span class="token operator">=</span> letter_box<span class="token punctuation">(</span>im<span class="token operator">=</span>img_src<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> new_shape<span class="token operator">=</span><span class="token punctuation">(</span>MODEL_SIZE<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> MODEL_SIZE<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> pad_color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># img = cv2.resize(img_src, (640, 512), interpolation=cv2.INTER_LINEAR) # direct resize</span>
<span class="token builtin">input</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>expand_dims<span class="token punctuation">(</span>img<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
outputs <span class="token operator">=</span> rknn_lite<span class="token punctuation">.</span>inference<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">input</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
boxes<span class="token punctuation">,</span> classes<span class="token punctuation">,</span> scores <span class="token operator">=</span> post_process<span class="token punctuation">(</span>outputs<span class="token punctuation">)</span>
img_p <span class="token operator">=</span> img_src<span class="token punctuation">.</span>copy<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> boxes <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
draw<span class="token punctuation">(</span>img_p<span class="token punctuation">,</span> boxes<span class="token punctuation">,</span> scores<span class="token punctuation">,</span> classes<span class="token punctuation">)</span>
<span class="token comment"># 保存结果</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">)</span><span class="token punctuation">:</span>
os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">)</span>
result_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>RESULT_PATH<span class="token punctuation">,</span> img_name<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imwrite<span class="token punctuation">(</span>result_path<span class="token punctuation">,</span> img_p<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Detection result save to {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>result_path<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">pass</span>
<span class="token comment"># cv2.imshow("full post process result", img_p)</span>
rknn_lite<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
使用下面命令来执行
python rk3588_test-2.py
- 1
会输出检测的结果如下图所示
!