当前位置:   article > 正文

树莓派部署YOLOv5模型

树莓派部署yolov5

本文章是关于树莓派部署YOLOv5s模型,实际测试效果的FPS仅有0.15,不够满足实际检测需要,各位大佬可以参考参考。

1、在树莓派中安装opencv(默认安装好python3)

  1. # 直接安装
  2. # 安装依赖软件
  3. sudo apt-get install -y libopencv-dev python3-opencv
  4. sudo apt-get install libatlas-base-dev
  5. sudo apt-get install libjasper-dev
  6. sudo apt-get install libqtgui4
  7. sudo apt-get install python3-pyqt5
  8. sudo apt install libqt4-test
  9. # 安装Python 包
  10. pip3 install opencv-python

2、导出onnx模型

从YOLOv5官网下载源代码和YOLOv5s.pt文件

YOLOv5官网

YOLOv5s.pt下载

按照作者提示安装环境,使用它自带的export.py将YOLOv5s.pt转为YOLOv5s.onnx,安装好环境后,在终端输入以下命令即可自动生成。

python export.py --weights yolov5s.pt --include onnx

3.测试

        可以先在电脑上测试一下,使用如下代码测试上述转换的模型能否使用,假如成功即可将下述代码和上述生成的YOLOv5s.onnx模型直接移动到树莓派中进行测试。

  1. # 图片检测
  2. import cv2
  3. import numpy as np
  4. import time
  5. def plot_one_box(x, img, color=None, label=None, line_thickness=None):
  6. """
  7. description: Plots one bounding box on image img,
  8. this function comes from YoLov5 project.
  9. param:
  10. x: a box likes [x1,y1,x2,y2]
  11. img: a opencv image object
  12. color: color to draw rectangle, such as (0,255,0)
  13. label: str
  14. line_thickness: int
  15. return:
  16. no return
  17. """
  18. tl = (
  19. line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1
  20. ) # line/font thickness
  21. color = color or [random.randint(0, 255) for _ in range(3)]
  22. c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
  23. cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
  24. if label:
  25. tf = max(tl - 1, 1) # font thickness
  26. t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
  27. c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
  28. cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
  29. cv2.putText(
  30. img,
  31. label,
  32. (c1[0], c1[1] - 2),
  33. 0,
  34. tl / 3,
  35. [225, 255, 255],
  36. thickness=tf,
  37. lineType=cv2.LINE_AA,
  38. )
  39. def post_process_opencv(outputs,model_h,model_w,img_h,img_w,thred_nms,thred_cond):
  40. conf = outputs[:,4].tolist()
  41. c_x = outputs[:,0]/model_w*img_w
  42. c_y = outputs[:,1]/model_h*img_h
  43. w = outputs[:,2]/model_w*img_w
  44. h = outputs[:,3]/model_h*img_h
  45. p_cls = outputs[:,5:]
  46. if len(p_cls.shape)==1:
  47. p_cls = np.expand_dims(p_cls,1)
  48. cls_id = np.argmax(p_cls,axis=1)
  49. p_x1 = np.expand_dims(c_x-w/2,-
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/672246
推荐阅读
相关标签
  

闽ICP备14008679号