树莓派部署YOLOv5目标检测(详细篇) 您所在的位置:网站首页 树莓派图片怎么画的 树莓派部署YOLOv5目标检测(详细篇)

树莓派部署YOLOv5目标检测(详细篇)

2024-07-09 09:25| 来源: 网络整理| 查看: 265

文章目录 前言1、部署过程1.1、下载YOLOv51.2、安装opencv、pytorch和numpy1.2.1、安装opencv1.2.2、安装pytorch&numpy 1.3、YOLOv5安装1.4、运行detect.py 2、开机自动运行脚本3、遇到的问题3.1、pip3下载缓慢3.1.1、国内源3.1.2、单次使用3.1.3、命令行永久修改3.1.4、文档永久修改 3.2、手动安装whl文件失败3.3、树莓派无法检测到摄像头3.3.1、判断是否正确加载摄像头3.3.2、开启摄像头 3.4、Pytorch\TorchVsision安装报错3.5、运行yolov5中的detect.py 如何避免下载Arial.ttf3.6、屏幕无法加载桌面 参考文章

前言

型号:树莓派4B

环境:Python3.7

远程控制软件:VNC Viewer(远程控制时,需要保证都处于同一个局域网中)

1、部署过程 1.1、下载YOLOv5 先去YOLOv5官网下载文件代码,然后在树莓派新建一个YOLOv5文件夹并把下载好的文件传输到里面这里采用YOLOv5-6.0

在这里插入图片描述

3.然后安装依赖:

sudo apt-get install libopenblas-dev libblas-dev m4 cmake cython python3-dev python3-yaml python3-setuptools python3-wheel python3-pillow python3-numpy 1.2、安装opencv、pytorch和numpy 1.2.1、安装opencv sudo apt-get install python3-opencv

建议是否安装成功: 在这里插入图片描述

1.2.2、安装pytorch&numpy

首先查看Python版本对应哪个Torch版本和torchvision版本 在这里插入图片描述

whl文件官网numpy文件官网树莓派常见pytotch包1树莓派常见pytotch包2本文用到的torch和torchvision版本

1、找到对应的torch和torchvision版本后,下载到树莓派中并安装 2、树莓派采用后缀名分别为armv7l和aarch64,后缀为armv7l的,代表是32位,如果是64位的需要选择后缀是aarch64的。cp37则代表是python3.7。

# 环境一: torch 1.8.0 和 torchvision 0.9.0(推荐) pip3 install torch-1.8.0-cp37-cp37m-linux_armv7l.whl pip3 install torchvision-0.9.0-cp37-cp37m-linux_armv7l.whl # 环境二:也可以采用torch1.6.0和torchvision 0.8.0的版本依旧能运行,所以也不一定得按版本对照来,不过能按照版本来就按照版本来 pip3 install torch-1.6.0a0+b31f58d-cp37-cp37m-linux_armv7l.whl pip3 install torchvision-0.8.0a0+10d5a55-cp37-cp37m-linux_armv7l.whl pip3 install numpy==1.18.5 -i https://mirrors.aliyun.com/pypi/simple/ 1.3、YOLOv5安装

去到YOLOv5项目,找到requirements.txt把 numpy、opencv-python、torch和torchvision注释

在这里插入图片描述

cd /home/pi/yolov5-waste #那个pi是我的用户名,注意要填自己的用户名 sudo nano requirements.txt # 按ctrl+o并回车完成写入,按ctrl+x退出。 # YOLOv5其他环境一键安装 pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

安装完成图

在这里插入图片描述

1.4、运行detect.py cd /home/pi/yolov5-waste #那个pi是我的用户名,注意要填自己的用户名 # 确保根目录有yolov5s.pt python3 detect.py # 成功检测! 2、开机自动运行脚本

【方法一】

1、在终端上输入:

sudo vi /etc/rc.local

2、就会出现一个文本编辑器,在文本内容的exit 0 上面添加一行:

python3 /home/pi/picam.py

3、然后保存更改, 重启树莓派:

# 重启树莓派方式1: sudo reboot # 重启树莓派方式2: sudo shutdown -r now

【方法二】 1、进入/home/pi/.config 路径

cd /home/pi/.config

2、 找到autostart 路径,没有就创建一个

mkdir autostart

3、然后进入autostart 路径

cd autostart

4、 在autostart 中创建一个.desktop 尾缀文件,例如test.desktop

touch test.desktop

5、 该文件中输入:

[Desktop Entry] Name=controller Comment=controller Program Encoding=UTF-8 Exec=lxterminal -e bash -c 'python3 /home/pi/yolov5-waste/images_detect.py;$SHELL' Terminal=false MultipleArgs=false Type=Application Categories=Application;Development; StartupNotify=true

其中,Name、Comment、Exec、Icon 分别代表启动项的名称、备注、图标(随便找个png图片)、Exec表示调用的指令,相当于在shell终端执行的指令.,例如sudo python3 /home/pi/yolov5-waste/images_detect.py

6、 最后重启即可观察到该程序自启了。

3、遇到的问题 3.1、pip3下载缓慢

如果你没有换成国内源话,下载速度有时候会很慢,甚至直接报错 Read timed out

解决办法: 使用国内源去下载

3.1.1、国内源 阿里云:https://mirrors.aliyun.com/pypi/simple/清华:https://pypi.tuna.tsinghua.edu.cn/simple中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/华中理工大学:https://pypi.hustunique.com/山东理工大学:https://pypi.sdutlinux.org/豆瓣:https://pypi.douban.com/simple/ 3.1.2、单次使用 pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/pip install 单个包名 -i https://mirrors.aliyun.com/pypi/simple/ 3.1.3、命令行永久修改

设置

$ pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple/ Writing to /root/.config/pip/pip.conf $ pip3 config set install.trusted-host mirrors.aliyun.com Writing to /root/.config/pip/pip.conf

查看效果

$ cat /root/.config/pip/pip.conf [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com $ pip config list global.index-url='http://mirrors.aliyun.com/pypi/simple/' install.trusted-host='mirrors.aliyun.com'

删除设置

pip config unset global.index-url Writing to /root/.config/pip/pip.conf cat /root/.config/pip/pip.conf [install] trusted-host = mirrors.aliyun.com 3.1.4、文档永久修改

管你用的是pip3还是pip,方法都是一样的(也即都是创建pip文件夹):

先创建文件夹,然后创建并打开文件(如果你已经有这个文件夹或者文件,那么执行下面的命令可能会报错,这无所谓,目的是有这个文件夹和文件就行)

mkdir ~/.pip vim ~/.pip/pip.conf

在pip.conf文件中输入以下内容

[global] timeout=100 index-url=https://pypi.tuna.tsinghua.edu.cn/simple/ extra-index-url= http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host= pypi.tuna.tsinghua.edu.cn mirrors.aliyun.com

之后保存退出即可。

3.2、手动安装whl文件失败

将下载好的Twisted-18.4.0-cp36-cp36m-win_amd64.whl放在E盘下面

包名中间的cp36是python3.6的意思,amd64是python的位数。

在安装python的whl包时,出现了以下问题:

在这里插入图片描述

解决办法: 查看当前环境支持包的类型

# 进入python,输入以下代码,查看pip支持的类型 import pip._internal print(pip._internal.pep425tags.get_supported()) 运行结果: [('cp38', 'cp38m', 'win32'), ('cp38', 'none', 'win32'), ('py3', 'none', 'win32'), ('cp38', 'none', 'any'), ('cp3', 'none', 'any'), ('py38', 'none', 'any'), ('py3', 'none', 'any'), ('py37', 'none', 'any'), ('py36', 'none', 'any'), ('py35', 'none', 'any'), ('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')]

可以看到,我们下载的whl包,命名不符合python3.8.0的安装支持,按照(‘cp38’, ‘cp38m’, ‘win32’),将其命名为:Twisted-20.3.0-cp38-cp38m-win32.whl 即可。

3.3、树莓派无法检测到摄像头 3.3.1、判断是否正确加载摄像头

方式1:检测摄像头

vcgencmd get_camera # 输出supported=1 说明支持外设摄像头 # supported=1 detected=0.

方式2:检测video设备

ls /dev/video* #正确连接时,第一个设备会是video0 #/dev/video0 /dev/video1 /dev/video10 /dev/video11 /dev/video12

在这里插入图片描述

3.3.2、开启摄像头

方法一: 系统开启摄像头

sudo raspi-config

将光标移动到摄像头选项( Interface Options)处,并选择启用(Enable)。

在这里插入图片描述

将光标移动到摄像头选项(Camera option)处,并选择启用(Enable)。在退出 raspi-config 时会要求您重新启动。启用选项是为了确保重启后 GPU 固件能够正确运行(包括摄像头驱动和调节电路),并且 GPU 从主内存划分到了足够的内存使摄像头能够正确运行。

在这里插入图片描述

方法二: 加载摄像头驱动

sudo nano /etc/modules # 在编写的文件后面加上 bcm2835-v4l2 # 按ctrl+o并回车完成写入,按ctrl+x退出。

在这里插入图片描述

3.4、Pytorch\TorchVsision安装报错

报错信息:

Traceback (most recent call last): File "setup.py", line 12, in import torch File "/home/efy/mambaforge/envs/yolov5/lib/python3.6/site-packages/torch/__init__.py", line 190, in from torch._C import * ImportError: libopenblas.so.0: cannot open shared object file: No such file or directory

解决方法

sudo apt-get install libopenblas-dev

如果还未解决,更新一下pip3

pip3 install --upgrade pip 3.5、运行yolov5中的detect.py 如何避免下载Arial.ttf

初次检测时会默认去官网下载Arial.ttf字体

解决方法

1、提前下载该字体到指定目录下 2、修改 yolov5/utils/plots.py 文件 在文件yolov5/utils/plots.py中,找到以下代码:

class Annotator: if RANK in (-1, 0): check_font() # download TTF if necessary # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):

修改为:

class Annotator: # 注释下载字体,以及初始化传入 font 字体 #if RANK in (-1, 0): #check_font() # download TTF if necessary # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations def __init__(self, im, line_width=None, font_size=None, font='', pil=False, example='abc'): 3.6、屏幕无法加载桌面

1、不同尺寸屏幕的分辨率各不相同 2、如果采用7寸屏幕,则修改 hdmi_cvt 1024 600 6 0 0 0

尺寸分辨率7寸1024*60010寸1024*60011.6寸1920*108013.3寸1920*1080 参考文章

YOLOv5s网络模型讲解(一看就会)

生活垃圾数据集(YOLO版)

YOLOv5如何训练自己的数据集

双向控制舵机(树莓派版)

----------------------完整代码---------------------------



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有