当前位置:   article > 正文

yolov8 pose

yolov8 pose

目录

yolov8 pose

yolov5 pose

fasterrcnn和pose_hrnet

yolov8 pose视频推理代码:


yolov8 pose

1060,视频推理,显存占用250M

s模型25ms,m模型时间50ms

yolov5 pose

1060,视频推理,显存占用250M

s模型20ms,m模型时间50ms

fasterrcnn和pose_hrnet

fasterrcnn和pose_hrnet_w48_384x288.pth 占用显存3G

时间200多ms

yolov8 pose视频推理代码:

参考博文做了简单修改:

YOLOv8-Pose推理详解及部署实现_yolov8 pose-CSDN博客

  1. import os
  2. import time
  3. import cv2
  4. import torch
  5. import numpy as np
  6. from ultralytics.data.augment import LetterBox
  7. from ultralytics.nn.autobackend import AutoBackend
  8. def preprocess_letterbox(image):
  9. letterbox = LetterBox(new_shape=640, stride=32, auto=True)
  10. image = letterbox(image=image)
  11. image = (image[..., ::-1] / 255.0).astype(np.float32) # BGR to RGB, 0 - 255 to 0.0 - 1.0
  12. image = image.transpose(2, 0, 1)[None] # BHWC to BCHW (n, 3, h, w)
  13. image = torch.from_numpy(image)
  14. return image
  15. def preprocess_warpAffine(image, dst_width=640, dst_height=640):
  16. scale = min((dst_width / image.shape[1], dst_height / image.shape[0]))
  17. ox = (dst_width - scale * image.shape[1]) / 2
  18. oy = (dst_height - scale * image.shape[0]) / 2
  19. M = np.array([[scale, 0, ox], [0, scale, oy]], dtype=np.float32)
  20. img_pre = cv2.warpAffine(image, M, (dst_width, dst_height), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(114, 114, 114))
  21. IM = cv2.invertAffineTransform(M)
  22. img_pre = (img_pre[..., ::-1] / 255.0).astype(np.float32)
  23. img_pre = img_pre.transpose(2, 0, 1)[None]
  24. img_pre = torch.from_numpy(img_pre)
  25. return img_pre, IM
  26. def iou(box1, box2):
  27. def area_box(box):
  28. return (box[2] - box[0]) * (box[3] - box[1])
  29. left = max(box1[0], box2[0])
  30. top = max(box1[1], box2[1])
  31. right = min(box1[2], box2[2])
  32. bottom = min(box1[3], box2[3])
  33. cross = max((right - left), 0) * max((bottom - top), 0)
  34. union = area_box(box1) + area_box(box2) - cross
  35. if cross == 0 or union == 0:
  36. return 0
  37. return cross / union
  38. def NMS(boxes, iou_thres):
  39. remove_flags = [False] * len(boxes)
  40. keep_boxes = []
  41. for i, ibox in enumerate(boxes):
  42. if remove_flags[i]:
  43. continue
  44. keep_boxes.append(ibox)
  45. for j in range(i + 1, len(boxes)):
  46. if remove_flags[j]:
  47. continue
  48. jbox = boxes[j]
  49. if iou(ibox, jbox) > iou_thres:
  50. remove_flags[j] = True
  51. return keep_boxes
  52. def postprocess(pred, IM=[], conf_thres=0.25, iou_thres=0.45):
  53. # 输入是模型推理的结果,即8400个预测框
  54. # 1,8400,56 [cx,cy,w,h,conf,17*3]
  55. boxes = []
  56. for img_id, box_id in zip(*np.where(pred[..., 4] > conf_thres)):
  57. item = pred[img_id, box_id]
  58. cx, cy, w, h, conf = item[:5]
  59. left = cx - w * 0.5
  60. top = cy - h * 0.5
  61. right = cx + w * 0.5
  62. bottom = cy + h * 0.5
  63. keypoints = item[5:].reshape(-1, 3)
  64. keypoints[:, 0] = keypoints[:, 0] * IM[0][0] + IM[0][2]
  65. keypoints[:, 1] = keypoints[:, 1] * IM[1][1] + IM[1][2]
  66. boxes.append([left, top, right, bottom, conf, *keypoints.reshape(-1).tolist()])
  67. boxes = np.array(boxes)
  68. lr = boxes[:, [0, 2]]
  69. tb = boxes[:, [1, 3]]
  70. boxes[:, [0, 2]] = IM[0][0] * lr + IM[0][2]
  71. boxes[:, [1, 3]] = IM[1][1] * tb + IM[1][2]
  72. boxes = sorted(boxes.tolist(), key=lambda x: x[4], reverse=True)
  73. return NMS(boxes, iou_thres)
  74. def hsv2bgr(h, s, v):
  75. h_i = int(h * 6)
  76. f = h * 6 - h_i
  77. p = v * (1 - s)
  78. q = v * (1 - f * s)
  79. t = v * (1 - (1 - f) * s)
  80. r, g, b = 0, 0, 0
  81. if h_i == 0:
  82. r, g, b = v, t, p
  83. elif h_i == 1:
  84. r, g, b = q, v, p
  85. elif h_i == 2:
  86. r, g, b = p, v, t
  87. elif h_i == 3:
  88. r, g, b = p, q, v
  89. elif h_i == 4:
  90. r, g, b = t, p, v
  91. elif h_i == 5:
  92. r, g, b = v, p, q
  93. return int(b * 255), int(g * 255), int(r * 255)
  94. def random_color(id):
  95. h_plane = (((id << 2) ^ 0x937151) % 100) / 100.0
  96. s_plane = (((id << 3) ^ 0x315793) % 100) / 100.0
  97. return hsv2bgr(h_plane, s_plane, 1)
  98. skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]
  99. pose_palette = np.array(
  100. [[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255],
  101. [255, 0, 0], [255, 255, 255]], dtype=np.uint8)
  102. kpt_color = pose_palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]]
  103. limb_color = pose_palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]]
  104. if __name__ == "__main__":
  105. model = AutoBackend(weights="yolov8m-pose.pt")
  106. names = model.names
  107. model.eval()
  108. model.model.cuda()
  109. cap=cv2.VideoCapture(r'B:\project\pose\MHFormer-main\demo\video\baitao.mp4')
  110. out_dir='out/'
  111. os.makedirs(out_dir,exist_ok=True)
  112. res_i=0
  113. while True:
  114. ret,img=cap.read()
  115. if not ret:
  116. break
  117. # img = preprocess_letterbox(img)
  118. start=time.time()
  119. img_pre, IM = preprocess_warpAffine(img)
  120. img_pre=img_pre.cuda()
  121. result = model(img_pre)[0].transpose(-1, -2) # 1,8400,56
  122. boxes = postprocess(result.cpu().numpy(), IM,conf_thres=0.4)
  123. print('pose time',time.time()-start)
  124. for box in boxes:
  125. left, top, right, bottom = int(box[0]), int(box[1]), int(box[2]), int(box[3])
  126. confidence = box[4]
  127. label = 0
  128. color = random_color(label)
  129. cv2.rectangle(img, (left, top), (right, bottom), color, 2, cv2.LINE_AA)
  130. caption = f"{names[label]} {confidence:.2f}"
  131. w, h = cv2.getTextSize(caption, 0, 1, 2)[0]
  132. cv2.rectangle(img, (left - 3, top - 33), (left + w + 10, top), color, -1)
  133. cv2.putText(img, caption, (left, top - 5), 0, 1, (0, 0, 0), 2, 16)
  134. keypoints = box[5:]
  135. keypoints = np.array(keypoints).reshape(-1, 3)
  136. for i, keypoint in enumerate(keypoints):
  137. x, y, conf = keypoint
  138. color_k = [int(x) for x in kpt_color[i]]
  139. if conf < 0.5:
  140. continue
  141. if x != 0 and y != 0:
  142. cv2.circle(img, (int(x), int(y)), 5, color_k, -1, lineType=cv2.LINE_AA)
  143. for i, sk in enumerate(skeleton):
  144. pos1 = (int(keypoints[(sk[0] - 1), 0]), int(keypoints[(sk[0] - 1), 1]))
  145. pos2 = (int(keypoints[(sk[1] - 1), 0]), int(keypoints[(sk[1] - 1), 1]))
  146. conf1 = keypoints[(sk[0] - 1), 2]
  147. conf2 = keypoints[(sk[1] - 1), 2]
  148. if conf1 < 0.5 or conf2 < 0.5:
  149. continue
  150. if pos1[0] == 0 or pos1[1] == 0 or pos2[0] == 0 or pos2[1] == 0:
  151. continue
  152. cv2.line(img, pos1, pos2, [int(x) for x in limb_color[i]], thickness=2, lineType=cv2.LINE_AA)
  153. cv2.imshow('img',img)
  154. cv2.waitKey(1)
  155. res_i+=1
  156. cv2.imwrite(out_dir+f"{res_i}.jpg", img)
  157. # print("save done")

yolo类推理,转自:

 YOLOv8-Pose推理详解及部署实现_yolov8 pose-CSDN博客

  1. import cv2
  2. import numpy as np
  3. from ultralytics import YOLO
  4. def hsv2bgr(h, s, v):
  5. h_i = int(h * 6)
  6. f = h * 6 - h_i
  7. p = v * (1 - s)
  8. q = v * (1 - f * s)
  9. t = v * (1 - (1 - f) * s)
  10. r, g, b = 0, 0, 0
  11. if h_i == 0:
  12. r, g, b = v, t, p
  13. elif h_i == 1:
  14. r, g, b = q, v, p
  15. elif h_i == 2:
  16. r, g, b = p, v, t
  17. elif h_i == 3:
  18. r, g, b = p, q, v
  19. elif h_i == 4:
  20. r, g, b = t, p, v
  21. elif h_i == 5:
  22. r, g, b = v, p, q
  23. return int(b * 255), int(g * 255), int(r * 255)
  24. def random_color(id):
  25. h_plane = (((id << 2) ^ 0x937151) % 100) / 100.0
  26. s_plane = (((id << 3) ^ 0x315793) % 100) / 100.0
  27. return hsv2bgr(h_plane, s_plane, 1)
  28. skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]
  29. pose_palette = np.array(
  30. [[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255],
  31. [255, 0, 0], [255, 255, 255]], dtype=np.uint8)
  32. kpt_color = pose_palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]]
  33. limb_color = pose_palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]]
  34. if __name__ == "__main__":
  35. model = YOLO("yolov8s-pose.pt")
  36. # video_path=r'B:\project\pose\MHFormer-main\demo\video\baitao.mp4'
  37. img = cv2.imread("bus.jpg")
  38. results = model(img)[0]
  39. names = results.names
  40. boxes = results.boxes.data.tolist()
  41. # keypoints.data.shape -> n,17,3
  42. keypoints = results.keypoints.cpu().numpy()
  43. # keypoint -> 每个人的关键点
  44. for keypoint in keypoints.data:
  45. for i, (x, y, conf) in enumerate(keypoint):
  46. color_k = [int(x) for x in kpt_color[i]]
  47. if conf < 0.5:
  48. continue
  49. if x != 0 and y != 0:
  50. cv2.circle(img, (int(x), int(y)), 5, color_k, -1, lineType=cv2.LINE_AA)
  51. for i, sk in enumerate(skeleton):
  52. pos1 = (int(keypoint[(sk[0] - 1), 0]), int(keypoint[(sk[0] - 1), 1]))
  53. pos2 = (int(keypoint[(sk[1] - 1), 0]), int(keypoint[(sk[1] - 1), 1]))
  54. conf1 = keypoint[(sk[0] - 1), 2]
  55. conf2 = keypoint[(sk[1] - 1), 2]
  56. if conf1 < 0.5 or conf2 < 0.5:
  57. continue
  58. if pos1[0] == 0 or pos1[1] == 0 or pos2[0] == 0 or pos2[1] == 0:
  59. continue
  60. cv2.line(img, pos1, pos2, [int(x) for x in limb_color[i]], thickness=2, lineType=cv2.LINE_AA)
  61. for obj in boxes:
  62. left, top, right, bottom = int(obj[0]), int(obj[1]), int(obj[2]), int(obj[3])
  63. confidence = obj[4]
  64. label = int(obj[5])
  65. color = random_color(label)
  66. cv2.rectangle(img, (left, top), (right, bottom), color=color, thickness=2, lineType=cv2.LINE_AA)
  67. caption = f"{names[label]} {confidence:.2f}"
  68. w, h = cv2.getTextSize(caption, 0, 1, 2)[0]
  69. cv2.rectangle(img, (left - 3, top - 33), (left + w + 10, top), color, -1)
  70. cv2.putText(img, caption, (left, top - 5), 0, 1, (0, 0, 0), 2, 16)
  71. cv2.imwrite("predict-pose.jpg", img)
  72. print("save done")

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

闽ICP备14008679号