在树莓派4B上部署yolov8环境完成高帧率检测任务
目录
前言
在我前面的文章里讲了如何用yolov8从0开始训练自己的模型,现在这篇文章将教大家如何在树莓派上部署yolov8的环境,以及如何实现高帧率的检测任务。
从上图可知,NCNN模型是最快的,我们可以看下官网是如何说的:
Out of all the model export formats supported by Ultralytics, NCNN delivers the best inference performance when working with Raspberry Pi devices because NCNN is highly optimized for mobile/ embedded platforms (such as ARM architecture). Therefor our recommendation is to use NCNN with Raspberry Pi.
NCNN在使用Raspberry Pi设备时提供了最佳的推理性能,因为NCNN针对移动/嵌入式平台(如ARM架构)进行了高度优化。
在树莓派上需要装的有:opencv-python,torch,torchvision,Ultralytics。opencv的安装非常简单,在本文中不出现安装教程。
在树莓派上安装Pytorch
以下操作均要根据自己对应的版本下载,切勿直接复制运行。
首先检查raspberry的Liunx架构,在终端输入:
uname -a
查看到我的raspberry的Liunx是armv7l,打开网址:Releases · KumaTea/pytorch-arm (github.com)https://github.com/KumaTea/pytorch-arm/releases然后找到对应架构以及对应Python版本的wheel文件,复制链接进行下载torch:
pip3 install https://github.com/KumaTea/pytorch-arm/releases/download/v1.8.1/torch-1.8.1-cp39-cp39-linux_armv7l.whl
然后我们可以测试一下Pytorch是否以及装好了,如下:
为后面的计算机视觉任务做准备,我们还需要单独装一下torchvision,同上操作:
pip3 install https://github.com/KumaTea/pytorch-arm/releases/download/v1.8.1/torchvision-0.9.1-cp39-cp39-linux_armv7l.whl
出现Successfully installed就说明下载完成了。
在树莓派上安装Ultralytics环境
Raspberry Pi - Ultralytics YOLO DocsLearn how to deploy Ultralytics YOLOv8 on Raspberry Pi with our comprehensive guide. Get performance benchmarks, setup instructions, and best practices.https://docs.ultralytics.com/guides/raspberry-pi/我们参考官方文档,更新软件包列表,安装 pip 并升级到最新版本:
sudo apt update
sudo apt install python3-pip -y
pip install -U pip
然后安装带有可选依赖项的Ultralytics:
pip3 install ultralytics
然后就是等待一下漫长的下载过程~
在树莓派上初步测试模型
下载完成后我们简单的测试一下原先训练的best.pt模型,虽然检测的速度很慢,但是可以检测出来,说明我们前面的步骤都是正确无误的。
import cv2
from ultralytics import YOLO
from cv2 import getTickCount, getTickFrequency
# 加载 YOLOv8 模型
model = YOLO("best.pt") # 这里选择你训练的模型
# 获取摄像头内容,参数 0 表示使用默认的摄像头
cap = cv2.VideoCapture(0)
while cap.isOpened():
loop_start = getTickCount()
success, frame = cap.read() # 读取摄像头的一帧图像
if success:
results = model.predict(source=frame) # 对当前帧进行目标检测并显示结果
annotated_frame = results[0].plot()
# 中间放自己的显示程序
loop_time = getTickCount() - loop_start
total_time = loop_time / (getTickFrequency())
FPS = int(1 / total_time)
# 在图像左上角添加FPS文本
fps_text = f"FPS: {FPS:.2f}"
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
font_thickness = 2
text_color = (0, 0, 255) # 红色
text_position = (10, 30) # 左上角位置
cv2.putText(annotated_frame, fps_text, text_position, font, font_scale, text_color, font_thickness)
cv2.imshow('img', annotated_frame)
# 通过按下 'q' 键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # 释放摄像头资源
cv2.destroyAllWindows() # 关闭OpenCV窗口
在树莓派上安装NCNN
用NCNN模型实现高帧率检测
我下午先去游泳一下,回来补充完全~
原文地址:https://blog.csdn.net/Duanwxq/article/details/140585222
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!