当前位置:   article > 正文

YOLOv8 onnx 文件推理多线程加速视频流_onnx加速

onnx加速

运行环境:

  • MacOS:14.0
  • Python 3.9
  • Pytorch2.1
  • onnx 运行时

模型文件:

https://wwxd.lanzouu.com/iBqiA1g49pbc
密码:f40v

  • 下载 best.apk后将后缀名修改为 onnx 即可
  • 模型在英伟达 T4GPU 使用 coco128 训练了 200 轮
  • 如遇下载不了可私信获取

代码:

  1. import copy
  2. import time
  3. import onnxruntime as rt
  4. import numpy as np
  5. import cv2
  6. import concurrent.futures
  7. # 前处理
  8. def resize_image(image, size, letterbox_image):
  9. """
  10. 对输入图像进行resize
  11. Args:
  12. size:目标尺寸
  13. letterbox_image: bool 是否进行letterbox变换
  14. Returns:指定尺寸的图像
  15. """
  16. ih, iw, _ = image.shape
  17. h, w = size
  18. # letterbox_image = False
  19. if letterbox_image:
  20. scale = min(w / iw, h / ih)
  21. nw = int(iw * scale)
  22. nh = int(ih * scale)
  23. image = cv2.resize(image, (nw, nh), interpolation=cv2.INTER_LINEAR)
  24. image_back = np.ones((h, w, 3), dtype=np.uint8) * 128
  25. image_back[(h - nh) // 2: (h - nh) // 2 + nh, (w - nw) // 2:(w - nw) // 2 + nw, :] = image
  26. else:
  27. image_back = image
  28. return image_back
  29. def img2input(img):
  30. img = np.transpose(img, (2, 0, 1))
  31. img = img / 255
  32. return np.expand_dims(img, axis=0).astype(np.float32)
  33. def std_output(pred):
  34. """
  35. 将(1,84,8400)处理成(8400, 85) 85= box:4 conf:1 cls:80
  36. """
  37. pred = np.squeeze(pred)
  38. pred = np.transpose(pred, (1, 0))
  39. pred_class = pred[..., 4:]
  40. pred_conf = np.max(pred_class, axis=-1)
  41. pred = np.insert(pred, 4, pred_conf, axis=-1)
  42. return pred
  43. def xywh2xyxy(*box):
  44. """
  45. 将xywh转换为左上角点和左下角点
  46. Args:
  47. box:
  48. Returns: x1y1x2y2
  49. """
  50. ret = [box[0] - box[2] // 2, box[1] - box[3] // 2, \
  51. box[0] + box[2] // 2, box[1] + box[3] // 2]
  52. return ret
  53. def get_inter(box1, box2):
  54. """
  55. 计算相交部分面积
  56. Args:
  57. box1: 第一个框
  58. box2: 第二个狂
  59. Returns: 相交部分的面积
  60. """
  61. x1, y1, x2, y2 = xywh2xyxy(*box1)
  62. x3, y3, x4, y4 = xywh2xyxy(*box2)
  63. # 验证是否存在交集
  64. if x1 >= x4 or x2 <= x3:
  65. return 0
  66. if y1 >= y4 or y2 <= y3:
  67. return 0
  68. # 将x1,x2,x3,x4排序,因为已经验证了两个框相交,所以x3-x2就是交集的宽
  69. x_list = sorted([x1, x2, x3, x4])
  70. x_inter = x_list[2] - x_list[1]
  71. # 将y1,y2,y3,y4排序,因为已经验证了两个框相交,所以y3-y2就是交集的宽
  72. y_list = sorted([y1, y2, y3, y4])
  73. y_inter = y_list[2] - y_list[1]
  74. # 计算交集的面积
  75. inter = x_inter * y_inter
  76. return inter
  77. def get_iou(box1, box2):
  78. """
  79. 计算交并比: (A n B)/(A + B - A n B)
  80. Args:
  81. box1: 第一个框
  82. box2: 第二个框
  83. Returns: # 返回交并比的值
  84. """
  85. box1_area = box1[2] * box1[3] # 计算第一个框的面积
  86. box2_area = box2[2] * box2[3] # 计算第二个框的面积
  87. inter_area = get_inter(box1, box2)
  88. union = box1_area + box2_area - inter_area # (A n B)/(A + B - A n B)
  89. iou = inter_area / union
  90. return iou
  91. def nms(pred, conf_thres, iou_thres):
  92. """
  93. 非极大值抑制nms
  94. Args:
  95. pred: 模型输出特征图
  96. conf_thres: 置信度阈值
  97. iou_thres: iou阈值
  98. Returns: 输出后的结果
  99. """
  100. box = pred[pred[..., 4] > conf_thres] # 置信度筛选
  101. cls_conf = box[..., 5:]
  102. cls = []
  103. for i in range(len(cls_conf)):
  104. cls.append(int(np.argmax(cls_conf[i])))
  105. total_cls = list(set(cls)) # 记录图像内共出现几种物体
  106. output_box = []
  107. # 每个预测类别分开考虑
  108. for i in range(len(total_cls)):
  109. clss = total_cls[i]
  110. cls_box = []
  111. temp = box[:, :6]
  112. for j in range(len(cls)):
  113. # 记录[x,y,w,h,conf(最大类别概率),class]值
  114. if cls[j] == clss:
  115. temp[j][5] = clss
  116. cls_box.append(temp[j][:6])
  117. # cls_box 里面是[x,y,w,h,conf(最大类别概率),class]
  118. cls_box = np.array(cls_box)
  119. sort_cls_box = sorted(cls_box, key=lambda x: -x[4]) # 将cls_box按置信度从大到小排序
  120. # box_conf_sort = np.argsort(-box_conf)
  121. # 得到置信度最大的预测框
  122. max_conf_box = sort_cls_box[0]
  123. output_box.append(max_conf_box)
  124. sort_cls_box = np.delete(sort_cls_box, 0, 0)
  125. # 对除max_conf_box外其他的框进行非极大值抑制
  126. while len(sort_cls_box) > 0:
  127. # 得到当前最大的框
  128. max_conf_box = output_box[-1]
  129. del_index = []
  130. for j in range(len(sort_cls_box)):
  131. current_box = sort_cls_box[j]
  132. iou = get_iou(max_conf_box, current_box)
  133. if iou > iou_thres:
  134. # 筛选出与当前最大框Iou大于阈值的框的索引
  135. del_index.append(j)
  136. # 删除这些索引
  137. sort_cls_box = np.delete(sort_cls_box, del_index, 0)
  138. if len(sort_cls_box) > 0:
  139. # 我认为这里需要将clas_box先按置信度排序, 才能每次取第一个
  140. output_box.append(sort_cls_box[0])
  141. sort_cls_box = np.delete(sort_cls_box, 0, 0)
  142. return output_box
  143. def cod_trf(result, pre, after):
  144. """
  145. 因为预测框是在经过letterbox后的图像上做预测所以需要将预测框的坐标映射回原图像上
  146. Args:
  147. result: [x,y,w,h,conf(最大类别概率),class]
  148. pre: 原尺寸图像
  149. after: 经过letterbox处理后的图像
  150. Returns: 坐标变换后的结果,
  151. """
  152. res = np.array(result)
  153. x, y, w, h, conf, cls = res.transpose((1, 0))
  154. x1, y1, x2, y2 = xywh2xyxy(x, y, w, h) # 左上角点和右下角的点
  155. h_pre, w_pre, _ = pre.shape
  156. h_after, w_after, _ = after.shape
  157. scale = max(w_pre / w_after, h_pre / h_after) # 缩放比例
  158. h_pre, w_pre = h_pre / scale, w_pre / scale # 计算原图在等比例缩放后的尺寸
  159. x_move, y_move = abs(w_pre - w_after) // 2, abs(h_pre - h_after) // 2 # 计算平移的量
  160. ret_x1, ret_x2 = (x1 - x_move) * scale, (x2 - x_move) * scale
  161. ret_y1, ret_y2 = (y1 - y_move) * scale, (y2 - y_move) * scale
  162. ret = np.array([ret_x1, ret_y1, ret_x2, ret_y2, conf, cls]).transpose((1, 0))
  163. return ret
  164. def draw(res, image, cls):
  165. """
  166. 将预测框绘制在image上
  167. Args:
  168. res: 预测框数据
  169. image: 原图
  170. cls: 类别列表,类似["apple", "banana", "people"] 可以自己设计或者通过数据集的yaml文件获取
  171. Returns:
  172. """
  173. for r in res:
  174. # 画框
  175. image = cv2.rectangle(image, (int(r[0]), int(r[1])), (int(r[2]), int(r[3])), (255, 0, 0), 1)
  176. # 表明类别
  177. text = "{}:{}".format(cls[int(r[5])], round(float(r[4]), 2))
  178. h, w = int(r[3]) - int(r[1]), int(r[2]) - int(r[0]) # 计算预测框的长宽
  179. font_size = min(h / 640, w / 640) * 3 # 计算字体大小(随框大小调整)
  180. image = cv2.putText(image, text, (max(10, int(r[0])), max(20, int(r[1]))), cv2.FONT_HERSHEY_COMPLEX,
  181. max(font_size, 0.3), (0, 0, 255), 1) # max()为了确保字体不过界
  182. return image
  183. def display_fps(frame, start_time):
  184. global global_fps
  185. end_time = time.time()
  186. elapsed_time = end_time - start_time
  187. global_fps = 1 / elapsed_time
  188. # 在图像上显示帧率
  189. cv2.putText(frame, f"FPS: {global_fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
  190. global_fps = 0.0
  191. if __name__ == '__main__':
  192. cap = cv2.VideoCapture(0)
  193. sess = rt.InferenceSession('./best.onnx')
  194. cv2.namedWindow('Video Stream', cv2.WINDOW_NORMAL)
  195. names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
  196. 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
  197. 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
  198. 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
  199. 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
  200. 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
  201. 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
  202. 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
  203. 'teddy bear', 'hair drier', 'toothbrush']
  204. def inference_task(frame):
  205. class_list = list(names)
  206. std_h, std_w = 640, 640
  207. img_after = resize_image(frame, (std_w, std_h), True)
  208. data = img2input(img_after)
  209. input_name = sess.get_inputs()[0].name
  210. label_name = sess.get_outputs()[0].name
  211. pred = sess.run([label_name], {input_name: data})[0]
  212. pred = std_output(pred)
  213. result = nms(pred, 0.6, 0.4)
  214. result = cod_trf(result, frame, img_after)
  215. image = draw(result, frame, class_list)
  216. return image
  217. with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
  218. while True:
  219. start_time = time.time()
  220. # 读取一帧
  221. ret, frame = cap.read()
  222. if not ret:
  223. print("无法读取帧")
  224. break
  225. # 提交任务并获取 Future 对象
  226. future = executor.submit(inference_task, frame)
  227. display_fps(frame, start_time)
  228. # 获取结果
  229. try:
  230. image = future.result()
  231. # 显示窗口
  232. cv2.imshow('Video Stream', image)
  233. cv2.waitKey(1)
  234. except Exception as e:
  235. cv2.imshow('Video Stream', frame)
  236. cv2.waitKey(1)
  237. # 释放资源
  238. cap.release()
  239. cv2.destroyAllWindows()

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

闽ICP备14008679号