当前位置:   article > 正文

yolov7 姿态识别-人体骨架-实时检测+实例分割_人脸识别、人体骨架识别、手势识别、手势骨架、目标检测 (ssd、yolov7)

人脸识别、人体骨架识别、手势识别、手势骨架、目标检测 (ssd、yolov7)

(15条消息) YOLOv7实例分割_迷途小书童的Note的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/djstavaV/article/details/126357677
(15条消息) yolov7 姿态识别-人体骨架-实时检测_ASURIUS的博客-CSDN博客https://blog.csdn.net/ASURIUS/article/details/127513826

  1. import time
  2. import matplotlib
  3. matplotlib.use('TkAgg')
  4. import matplotlib.pyplot as plt
  5. import torch
  6. import cv2
  7. from torchvision import transforms
  8. import numpy as np
  9. from utils.datasets import letterbox
  10. from utils.general import non_max_suppression_kpt
  11. from utils.plots import output_to_keypoint, plot_skeleton_kpts
  12. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  13. weigths = torch.load('yolov7-w6-pose.pt')
  14. model = weigths['model']
  15. model = model.half().to(device) if device.type != "cpu" else model.float().to(device)
  16. _ = model.eval()
  17. # 读取摄像头画面
  18. print(device)
  19. url = 0
  20. cap = cv2.VideoCapture(url)
  21. while (cap.isOpened()):
  22. ret, image = cap.read()
  23. # image = cv2.imread('xiaolu.jpg')
  24. image = letterbox(image, 960, stride=64, auto=True)[0]
  25. image_ = image.copy()
  26. image = transforms.ToTensor()(image)
  27. image = torch.tensor(np.array([image.numpy()]))
  28. image = image.to(device)
  29. image = image.half() if device.type != "cpu" else image.float()
  30. # 姿势识别
  31. t1 = time.time()
  32. with torch.no_grad():
  33. output, _ = model(image)
  34. output = non_max_suppression_kpt(output, 0.25, 0.65, nc=model.yaml['nc'], nkpt=model.yaml['nkpt'],
  35. kpt_label=True)
  36. output = output_to_keypoint(output)
  37. nimg = image[0].permute(1, 2, 0) * 255
  38. nimg = nimg.cpu().numpy().astype(np.uint8)
  39. nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
  40. for idx in range(output.shape[0]):
  41. plot_skeleton_kpts(nimg, output[idx, 7:].T, 3)
  42. # 打开摄像头
  43. cv2.namedWindow("ning", cv2.WINDOW_NORMAL)
  44. cv2.imshow("ning", nimg)
  45. t2 = time.time()
  46. print(f'Done. ({(1E3 * (t2 - t1)):.1f}ms) Inference')
  47. if cv2.waitKey(1) & 0xFF == ord('q'):
  48. break
  49. cap.release()
  50. cv2.destroyAllWindows()
  51. # image = cv2.imread('bus.jpg')
  52. # image = letterbox(image, 960, stride=64, auto=True)[0]
  53. # image_ = image.copy()
  54. # image = transforms.ToTensor()(image)
  55. # image = torch.tensor(np.array([image.numpy()]))
  56. # image = image.to(device)
  57. # image = image.half()
  58. #
  59. # output, _ = model(image)
  60. #
  61. # output = non_max_suppression_kpt(output, 0.25, 0.65, nc=model.yaml['nc'], nkpt=model.yaml['nkpt'], kpt_label=True)
  62. # with torch.no_grad():
  63. # output = output_to_keypoint(output)
  64. # nimg = image[0].permute(1, 2, 0) * 255
  65. # nimg = nimg.cpu().numpy().astype(np.uint8)
  66. # nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
  67. # for idx in range(output.shape[0]):
  68. # plot_skeleton_kpts(nimg, output[idx, 7:].T, 3)
  69. # plt.figure(figsize=(8, 8))
  70. # plt.axis('off')
  71. # plt.imshow(nimg)
  72. # plt.show()
  73. # cv2.imshow('nimg',nimg)
  74. # cv2.waitKey(100000)

分割的部分依赖于 facebook 的 detectron2,而 detectron2 要求 torch 版本大于 1.8,由于之前我一直用的都是 1.7.1,因此这里需要创建一个新的环境

  1. # 创建新的虚拟环境
  2. conda create -n pytorch1.8 python=3.8
  3. conda activate pytorch1.8
  4. # 安装 torch 1.8.2
  5. pip install torch==1.8.2 torchvision==0.9.2 torchaudio===0.8.2 --extra-index-url https://download.pytorch.org/whl/lts/1.8/cu111
  6. # 修改requirements.txt,将其中的torch和torchvision注释掉
  7. pip install -r requirements.txt
  8. # 安装detectron2
  9. git clone https://github.com/facebookresearch/detectron2
  10. cd detectron2
  11. python setup.py install
  12. cd ..

然后去下载实例分割的模型 https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-mask.pt,将模型放入源码目录中

分割的示例代码在 tools/instance.ipynb,可以在 jupyter notebook 中直接运行

如果需要将 ipynb 文件转成 python 文件,就执行

jupyter nbconvert --to python tools/instance.ipynb


将生成的 tools/instance.py 拷贝到源码根目录下,然后修改文件的最后显示部分为保存结果图片

  1. import matplotlib.pyplot as plt
  2. import torch
  3. import cv2
  4. import yaml
  5. from torchvision import transforms
  6. import numpy as np
  7. from utils.datasets import letterbox
  8. from utils.general import non_max_suppression_mask_conf
  9. from detectron2.modeling.poolers import ROIPooler
  10. from detectron2.structures import Boxes
  11. from detectron2.utils.memory import retry_if_cuda_oom
  12. from detectron2.layers import paste_masks_in_image
  13. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  14. with open('data/hyp.scratch.mask.yaml') as f:
  15. hyp = yaml.load(f, Loader=yaml.FullLoader)
  16. weigths = torch.load('yolov7-mask.pt')
  17. model = weigths['model']
  18. model = model.half().to(device)
  19. _ = model.eval()
  20. image = cv2.imread('inference/images/horses.jpg') # 504x378 image
  21. image = letterbox(image, 640, stride=64, auto=True)[0]
  22. image_ = image.copy()
  23. image = transforms.ToTensor()(image)
  24. image = torch.tensor(np.array([image.numpy()]))
  25. image = image.to(device)
  26. image = image.half()
  27. output = model(image)
  28. inf_out, train_out, attn, mask_iou, bases, sem_output = output['test'], output['bbox_and_cls'], output['attn'], output['mask_iou'], output['bases'], output['sem']
  29. bases = torch.cat([bases, sem_output], dim=1)
  30. nb, _, height, width = image.shape
  31. names = model.names
  32. pooler_scale = model.pooler_scale
  33. pooler = ROIPooler(output_size=hyp['mask_resolution'], scales=(pooler_scale,), sampling_ratio=1, pooler_type='ROIAlignV2', canonical_level=2)
  34. output, output_mask, output_mask_score, output_ac, output_ab = non_max_suppression_mask_conf(inf_out, attn, bases, pooler, hyp, conf_thres=0.25, iou_thres=0.65, merge=False, mask_iou=None)
  35. pred, pred_masks = output[0], output_mask[0]
  36. base = bases[0]
  37. bboxes = Boxes(pred[:, :4])
  38. original_pred_masks = pred_masks.view(-1, hyp['mask_resolution'], hyp['mask_resolution'])
  39. pred_masks = retry_if_cuda_oom(paste_masks_in_image)( original_pred_masks, bboxes, (height, width), threshold=0.5)
  40. pred_masks_np = pred_masks.detach().cpu().numpy()
  41. pred_cls = pred[:, 5].detach().cpu().numpy()
  42. pred_conf = pred[:, 4].detach().cpu().numpy()
  43. nimg = image[0].permute(1, 2, 0) * 255
  44. nimg = nimg.cpu().numpy().astype(np.uint8)
  45. nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
  46. nbboxes = bboxes.tensor.detach().cpu().numpy().astype(np.int)
  47. pnimg = nimg.copy()
  48. for one_mask, bbox, cls, conf in zip(pred_masks_np, nbboxes, pred_cls, pred_conf):
  49. if conf < 0.25:
  50. continue
  51. color = [np.random.randint(255), np.random.randint(255), np.random.randint(255)]
  52. pnimg[one_mask] = pnimg[one_mask] * 0.5 + np.array(color, dtype=np.uint8) * 0.5
  53. pnimg = cv2.rectangle(pnimg, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
  54. #label = '%s %.3f' % (names[int(cls)], conf)
  55. #t_size = cv2.getTextSize(label, 0, fontScale=0.5, thickness=1)[0]
  56. #c2 = bbox[0] + t_size[0], bbox[1] - t_size[1] - 3
  57. #pnimg = cv2.rectangle(pnimg, (bbox[0], bbox[1]), c2, color, -1, cv2.LINE_AA) # filled
  58. #pnimg = cv2.putText(pnimg, label, (bbox[0], bbox[1] - 2), 0, 0.5, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA)
  59. cv2.imwrite("instance_result.jpg", pnimg)

同样的,这里也提供一份视频文件或摄像头检测的代码

  1. import torch
  2. import cv2
  3. import yaml
  4. from torchvision import transforms
  5. import numpy as np
  6. from utils.datasets import letterbox
  7. from utils.general import non_max_suppression_mask_conf
  8. from detectron2.modeling.poolers import ROIPooler
  9. from detectron2.structures import Boxes
  10. from detectron2.utils.memory import retry_if_cuda_oom
  11. from detectron2.layers import paste_masks_in_image
  12. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  13. with open('data/hyp.scratch.mask.yaml') as f:
  14. hyp = yaml.load(f, Loader=yaml.FullLoader)
  15. weigths = torch.load('yolov7-mask.pt')
  16. model = weigths['model']
  17. model = model.half().to(device)
  18. _ = model.eval()
  19. cap = cv2.VideoCapture('vehicle_test.mp4')
  20. if (cap.isOpened() == False):
  21. print('open failed.')
  22. exit(-1)
  23. # 分辨率
  24. frame_width = int(cap.get(3))
  25. frame_height = int(cap.get(4))
  26. # 图片缩放
  27. vid_write_image = letterbox(cap.read()[1], (frame_width), stride=64, auto=True)[0]
  28. resize_height, resize_width = vid_write_image.shape[:2]
  29. # 保存结果视频
  30. out = cv2.VideoWriter("result_instance.mp4",
  31. cv2.VideoWriter_fourcc(*'mp4v'), 30,
  32. (resize_width, resize_height))
  33. while(cap.isOpened):
  34. flag, image = cap.read()
  35. if flag:
  36. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  37. image = letterbox(image, frame_width, stride=64, auto=True)[0]
  38. image_ = image.copy()
  39. image = transforms.ToTensor()(image)
  40. image = torch.tensor(np.array([image.numpy()]))
  41. image = image.to(device)
  42. image = image.half()
  43. with torch.no_grad():
  44. output = model(image)
  45. inf_out, train_out, attn, mask_iou, bases, sem_output = output['test'], output['bbox_and_cls'], output['attn'], output['mask_iou'], output['bases'], output['sem']
  46. bases = torch.cat([bases, sem_output], dim=1)
  47. nb, _, height, width = image.shape
  48. names = model.names
  49. pooler_scale = model.pooler_scale
  50. pooler = ROIPooler(output_size=hyp['mask_resolution'], scales=(pooler_scale,), sampling_ratio=1, pooler_type='ROIAlignV2', canonical_level=2)
  51. output, output_mask, output_mask_score, output_ac, output_ab = non_max_suppression_mask_conf(inf_out, attn, bases, pooler, hyp, conf_thres=0.25, iou_thres=0.65, merge=False, mask_iou=None)
  52. pred, pred_masks = output[0], output_mask[0]
  53. base = bases[0]
  54. bboxes = Boxes(pred[:, :4])
  55. original_pred_masks = pred_masks.view(-1, hyp['mask_resolution'], hyp['mask_resolution'])
  56. pred_masks = retry_if_cuda_oom(paste_masks_in_image)( original_pred_masks, bboxes, (height, width), threshold=0.5)
  57. pred_masks_np = pred_masks.detach().cpu().numpy()
  58. pred_cls = pred[:, 5].detach().cpu().numpy()
  59. pred_conf = pred[:, 4].detach().cpu().numpy()
  60. nimg = image[0].permute(1, 2, 0) * 255
  61. nimg = nimg.cpu().numpy().astype(np.uint8)
  62. nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
  63. nbboxes = bboxes.tensor.detach().cpu().numpy().astype(np.int)
  64. pnimg = nimg.copy()
  65. for one_mask, bbox, cls, conf in zip(pred_masks_np, nbboxes, pred_cls, pred_conf):
  66. if conf < 0.25:
  67. continue
  68. color = [np.random.randint(255), np.random.randint(255), np.random.randint(255)]
  69. pnimg[one_mask] = pnimg[one_mask] * 0.5 + np.array(color, dtype=np.uint8) * 0.5
  70. pnimg = cv2.rectangle(pnimg, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)
  71. cv2.imshow('YOLOv7 mask', pnimg)
  72. out.write(pnimg)
  73. if cv2.waitKey(1) & 0xFF == ord('q'):
  74. break
  75. else:
  76. break
  77. cap.release()
  78. cv2.destroyAllWindows()


 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/275871
推荐阅读
相关标签
  

闽ICP备14008679号