当前位置:   article > 正文

树莓派部署YOLOv5模型_树莓派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,-1)
  50. p_y1 = np.expand_dims(c_y-h/2,-1)
  51. p_x2 = np.expand_dims(c_x+w/2,-1)
  52. p_y2 = np.expand_dims(c_y+h/2,-1)
  53. areas = np.concatenate((p_x1,p_y1,p_x2,p_y2),axis=-1)
  54. print(areas.shape)
  55. areas = areas.tolist()
  56. ids = cv2.dnn.NMSBoxes(areas,conf,thred_cond,thred_nms)
  57. return np.array(areas)[ids],np.array(conf)[ids],cls_id[ids]
  58. def infer_image(net,img0,model_h,model_w,thred_nms=0.4,thred_cond=0.5):
  59. img = img0.copy()
  60. img = cv2.resize(img,[model_h,model_w])
  61. blob = cv2.dnn.blobFromImage(img, scalefactor=1/255.0, swapRB=True)
  62. net.setInput(blob)
  63. outs = net.forward()[0]
  64. print(outs[0])
  65. det_boxes,scores,ids = post_process_opencv(outs,model_h,model_w,img0.shape[0],img0.shape[1],thred_nms,thred_cond)
  66. return det_boxes,scores,ids
  67. if __name__=="__main__":
  68. dic_labels= {0: 'person',
  69. 1: 'bicycle',
  70. 2: 'car',
  71. 4: 'airplane',
  72. 5: 'bus',
  73. 6: 'train',
  74. 7: 'truck',
  75. 8: 'boat',
  76. 9: 'traffic light',
  77. 10: 'fire hydrant',
  78. 11: 'stop sign',
  79. 12: 'parking meter',
  80. 13: 'bench',
  81. 14: 'bird',
  82. 15: 'cat',
  83. 16: 'dog',
  84. 17: 'horse',
  85. 18: 'sheep',
  86. 19: 'cow',
  87. 20: 'elephant',
  88. 21: 'bear',
  89. 22: 'zebra',
  90. 23: 'giraffe',
  91. 24: 'backpack',
  92. 25: 'umbrella',
  93. 26: 'handbag',
  94. 27: 'tie',
  95. 28: 'suitcase',
  96. 29: 'frisbee',
  97. 30: 'skis',
  98. 31: 'snowboard',
  99. 32: 'sports ball',
  100. 33: 'kite',
  101. 34: 'baseball bat',
  102. 35: 'baseball glove',
  103. 36: 'skateboard',
  104. 37: 'surfboard',
  105. 38: 'tennis racket',
  106. 39: 'bottle',
  107. 40: 'wine glass',
  108. 41: 'cup',
  109. 42: 'fork',
  110. 43: 'knife',
  111. 44: 'spoon',
  112. 45: 'bowl',
  113. 46: 'banana',
  114. 47: 'apple',
  115. 48: 'sandwich',
  116. 49: 'orange',
  117. 50: 'broccoli',
  118. 51: 'carrot',
  119. 52: 'hot dog',
  120. 53: 'pizza',
  121. 54: 'donut',
  122. 55: 'cake',
  123. 56: 'chair',
  124. 57: 'couch',
  125. 58: 'potted plant',
  126. 59: 'bed',
  127. 60: 'dining table',
  128. 61: 'toilet',
  129. 62: 'tv',
  130. 63: 'laptop',
  131. 64: 'mouse',
  132. 65: 'remote',
  133. 66: 'keyboard',
  134. 67: 'cell phone',
  135. 68: 'microwave',
  136. 69: 'oven',
  137. 70: 'toaster',
  138. 71: 'sink',
  139. 72: 'refrigerator',
  140. 73: 'book',
  141. 74: 'clock',
  142. 75: 'vase',
  143. 76: 'scissors',
  144. 77: 'teddy bear',
  145. 78: 'hair drier',
  146. 79: 'toothbrush'}
  147. model_h = 640
  148. model_w = 640
  149. file_model = 'yolov5s.onnx'
  150. net = cv2.dnn.readNet(file_model)
  151. img0 = cv2.imread('3.jpg')
  152. t1 = time.time()
  153. det_boxes,scores,ids = infer_image(net,img0,model_h,model_w,thred_nms=0.4,thred_cond=0.5)
  154. t2 = time.time()
  155. print("cost time %.2fs"%(t2-t1))
  156. for box,score,id in zip(det_boxes,scores,ids):
  157. label = '%s:%.2f'%(dic_labels[id],score)
  158. plot_one_box(box.astype(np.int16), img0, color=(255,0,0), label=label, line_thickness=None)
  159. cv2.imshow('img',img0)
  160. cv2.waitKey(0)

         下面是视频检测代码,我采用的是树莓派自带的摄像头,如果是其他深度学习摄像头,以下代码需要更改。

  1. # 视频检测
  2. import cv2
  3. import numpy as np
  4. import time
  5. from threading import Thread
  6. def plot_one_box(x, img, color=None, label=None, line_thickness=None):
  7. """
  8. description: Plots one bounding box on image img,
  9. this function comes from YoLov5 project.
  10. param:
  11. x: a box likes [x1,y1,x2,y2]
  12. img: a opencv image object
  13. color: color to draw rectangle, such as (0,255,0)
  14. label: str
  15. line_thickness: int
  16. return:
  17. no return
  18. """
  19. tl = (
  20. line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1
  21. ) # line/font thickness
  22. color = color or [random.randint(0, 255) for _ in range(3)]
  23. c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
  24. cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
  25. if label:
  26. tf = max(tl - 1, 1) # font thickness
  27. t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
  28. c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
  29. cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
  30. cv2.putText(
  31. img,
  32. label,
  33. (c1[0], c1[1] - 2),
  34. 0,
  35. tl / 3,
  36. [225, 255, 255],
  37. thickness=tf,
  38. lineType=cv2.LINE_AA,
  39. )
  40. def post_process_opencv(outputs,model_h,model_w,img_h,img_w,thred_nms,thred_cond):
  41. conf = outputs[:,4].tolist()
  42. c_x = outputs[:,0]/model_w*img_w
  43. c_y = outputs[:,1]/model_h*img_h
  44. w = outputs[:,2]/model_w*img_w
  45. h = outputs[:,3]/model_h*img_h
  46. p_cls = outputs[:,5:]
  47. if len(p_cls.shape)==1:
  48. p_cls = np.expand_dims(p_cls,1)
  49. cls_id = np.argmax(p_cls,axis=1)
  50. p_x1 = np.expand_dims(c_x-w/2,-1)
  51. p_y1 = np.expand_dims(c_y-h/2,-1)
  52. p_x2 = np.expand_dims(c_x+w/2,-1)
  53. p_y2 = np.expand_dims(c_y+h/2,-1)
  54. areas = np.concatenate((p_x1,p_y1,p_x2,p_y2),axis=-1)
  55. # print(areas.shape)
  56. areas = areas.tolist()
  57. ids = cv2.dnn.NMSBoxes(areas,conf,thred_cond,thred_nms)
  58. if len(ids)>0:
  59. return np.array(areas)[ids],np.array(conf)[ids],cls_id[ids]
  60. else:
  61. return [],[],[]
  62. def infer_image(net,img0,model_h,model_w,thred_nms=0.4,thred_cond=0.5):
  63. img = img0.copy()
  64. img = cv2.resize(img,[model_h,model_w])
  65. blob = cv2.dnn.blobFromImage(img, scalefactor=1/255.0, swapRB=True)
  66. net.setInput(blob)
  67. outs = net.forward()[0]
  68. det_boxes,scores,ids = post_process_opencv(outs,model_h,model_w,img0.shape[0],img0.shape[1],thred_nms,thred_cond)
  69. return det_boxes,scores,ids
  70. global det_boxes_show
  71. global scores_show
  72. global ids_show
  73. global FPS_show
  74. def m_detection(net,cap,model_h,model_w):
  75. global det_boxes_show
  76. global scores_show
  77. global ids_show
  78. global FPS_show
  79. while True:
  80. success, img0 = cap.read()
  81. if success:
  82. t1 = time.time()
  83. det_boxes,scores,ids = infer_image(net,img0,model_h,model_w,thred_nms=0.4,thred_cond=0.4)
  84. t2 = time.time()
  85. str_fps = "FPS: %.2f"%(1./(t2-t1))
  86. det_boxes_show = det_boxes
  87. scores_show = scores
  88. ids_show = ids
  89. FPS_show = str_fps
  90. # time.sleep(5)
  91. if __name__=="__main__":
  92. dic_labels = {0: 'person',
  93. 1: 'bicycle',
  94. 2: 'car',
  95. 4: 'airplane',
  96. 5: 'bus',
  97. 6: 'train',
  98. 7: 'truck',
  99. 8: 'boat',
  100. 9: 'traffic light',
  101. 10: 'fire hydrant',
  102. 11: 'stop sign',
  103. 12: 'parking meter',
  104. 13: 'bench',
  105. 14: 'bird',
  106. 15: 'cat',
  107. 16: 'dog',
  108. 17: 'horse',
  109. 18: 'sheep',
  110. 19: 'cow',
  111. 20: 'elephant',
  112. 21: 'bear',
  113. 22: 'zebra',
  114. 23: 'giraffe',
  115. 24: 'backpack',
  116. 25: 'umbrella',
  117. 26: 'handbag',
  118. 27: 'tie',
  119. 28: 'suitcase',
  120. 29: 'frisbee',
  121. 30: 'skis',
  122. 31: 'snowboard',
  123. 32: 'sports ball',
  124. 33: 'kite',
  125. 34: 'baseball bat',
  126. 35: 'baseball glove',
  127. 36: 'skateboard',
  128. 37: 'surfboard',
  129. 38: 'tennis racket',
  130. 39: 'bottle',
  131. 40: 'wine glass',
  132. 41: 'cup',
  133. 42: 'fork',
  134. 43: 'knife',
  135. 44: 'spoon',
  136. 45: 'bowl',
  137. 46: 'banana',
  138. 47: 'apple',
  139. 48: 'sandwich',
  140. 49: 'orange',
  141. 50: 'broccoli',
  142. 51: 'carrot',
  143. 52: 'hot dog',
  144. 53: 'pizza',
  145. 54: 'donut',
  146. 55: 'cake',
  147. 56: 'chair',
  148. 57: 'couch',
  149. 58: 'potted plant',
  150. 59: 'bed',
  151. 60: 'dining table',
  152. 61: 'toilet',
  153. 62: 'tv',
  154. 63: 'laptop',
  155. 64: 'mouse',
  156. 65: 'remote',
  157. 66: 'keyboard',
  158. 67: 'cell phone',
  159. 68: 'microwave',
  160. 69: 'oven',
  161. 70: 'toaster',
  162. 71: 'sink',
  163. 72: 'refrigerator',
  164. 73: 'book',
  165. 74: 'clock',
  166. 75: 'vase',
  167. 76: 'scissors',
  168. 77: 'teddy bear',
  169. 78: 'hair drier',
  170. 79: 'toothbrush'}
  171. model_h = 640
  172. model_w = 640
  173. file_model = 'yolov5n.onnx'
  174. net = cv2.dnn.readNet(file_model)
  175. video = 0
  176. cap = cv2.VideoCapture(video)
  177. m_thread = Thread(target=m_detection, args=([net,cap,model_h,model_w]),daemon=True)
  178. m_thread.start()
  179. global det_boxes_show
  180. global scores_show
  181. global ids_show
  182. global FPS_show
  183. det_boxes_show = []
  184. scores_show = []
  185. ids_show =[]
  186. FPS_show = ""
  187. while True:
  188. success, img0 = cap.read()
  189. if success:
  190. for box,score,id in zip(det_boxes_show,scores_show,ids_show):
  191. label = '%s:%.2f'%(dic_labels[id],score)
  192. plot_one_box(box, img0, color=(255,0,0), label=label, line_thickness=None)
  193. str_FPS = FPS_show
  194. cv2.putText(img0,str_FPS,(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),3)
  195. cv2.imshow("video",img0)
  196. if cv2.waitKey(1) & 0xFF == ord('q'):
  197. break
  198. cap.release()

4. 结语

总体来说,树莓派实现YOLOv5的检测还是比较吃力的,后续可能会试试其他边缘设备,上述仅供参考,欢迎大家多多点赞交流。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/147320
推荐阅读
相关标签
  

闽ICP备14008679号