AI实战营:MMPose开源算法库

该文详细展示了如何运用MMPose库的预训练模型RTMPose和HRNet进行人体关键点检测,包括命令行预测单张图片和视频,以及使用官方的visualizer工具进行可视化。同时,文中还演示了通过PythonAPI进行模型预测和结果处理的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 目录

    RTMPose关键点检测全流程

    MMPose官方可视化工具visualizer

    代码实现:


  • RTMPose关键点检测全流程

  • MMPose预训练模型预测-命令行
    • 预测单张图
      • # HRNet
        python .\demo\topdown_demo_with_mmdet.py .\demo\mmdetection_cfg\faster_rcnn_r50_fpn_coco.py https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth .\configs\body_2d_keypoint\topdown_heatmap\coco\td-hm_hrnet-w32_8xb64-210e_coco-256x192.py https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth --input .\data\test\multi-person.jpeg --output-root .\outputs\B1_HRNet_1 --dev cuda:0 --bbox-thr 0.5 --kpt-thr 0.2 --nms-thr 0.3 --radius 8 --thickness 4 --draw-bbox --draw-heatmap --show-kpt-idx
      • Loads checkpoint by http backend from path: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
        Loads checkpoint by http backend from path: https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth
        06/03 20:47:33 - mmengine - WARNING - `Visualizer` backend is not initialized because save_dir is None.  
      • #RTMPose
        python .\demo\topdown_demo_with_mmdet.py .\demo\mmdetection_cfg\faster_rcnn_r50_fpn_coco.py https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth .\projects\rtmpose\rtmpose\body_2d_keypoint\rtmpose-s_8xb256-420e_coco-256x192.py https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmpose-s_simcc-aic-coco_pt-aic-coco_
        420e-256x192-fcb2599b_20230126.pth --input .\data\test\multi-person.jpeg --output-root .\outputs\B1_RTM_1 --d
        evice cuda:0 --bbox-thr 0.5 --kpt-thr 0.5 --nms-thr 0.3 --radius 8 --thickness 4 --draw-bbox --draw-heatmap --show-kpt-idx
        
      • Loads checkpoint by http backend from path: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
        Loads checkpoint by http backend from path: https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmpose-s_simcc-aic-coco_pt-aic-coco_420e-256x192-fcb2599b_20230126.pth
        Downloading: "https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmpose-s_simcc-aic-coco_pt-aic-coco_420e-256x192-fcb2599b_20230126.pth" to C:\Users\16798/.cache\torch\hub\checkpoints\rtmpose-s_simcc-aic-coco_pt-aic-coco_420e-256x192-fcb2599b_20230126.pth
        100%|██████████████████████████████████████████████████████████████████| 60.8M/60.8M [00:37<00:00, 1.71MB/s]
        06/03 23:04:00 - mmengine - WARNING - `Visualizer` backend is not initialized because save_dir is None.
        d:\workspace\.git\mmpose\mmpose\models\heads\coord_cls_heads\rtmcc_head.py:217: UserWarning: The predicted si
        mcc values are normalized for visualization. This may cause discrepancy between the keypoint scores and the 1D heatmaps.
          warnings.warn('The predicted simcc values are normalized for '
    • 预测视频:直接将--input 换成视频路径即可
      • #HRNet 
        python .\demo\topdown_demo_with_mmdet.py .\demo\mmdetection_cfg\faster_rcnn_r50_fpn_coco.py https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth .\configs\body_2d_keypoint\topdown_heatmap\coco\td-hm_hrnet-w32_8xb64-210e_coco-256x192.py https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth --input .\data\test\mother_wx.mp4 --output-root .\outputs\B1_HRNet_2 --device cuda:0 --bbox-thr 0.5 --kpt-thr 0.2 --nms-thr 0.3 --radius 5 --thickness 2 --draw-bbox --draw-heatmap --show-kpt-idx
        
      • Loads checkpoint by http backend from path: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
        Loads checkpoint by http backend from path: https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth
        06/03 23:11:03 - mmengine - WARNING - `Visualizer` backend is not initialized because save_dir is None.
         
      • #TRMPose
        python .\demo\topdown_demo_with_mmdet.py .\demo\mmdetection_cfg\faster_rcnn_r50_fpn_coco.py https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth projects/rtmpose/rtmpose/body_2d_keypoint/rtmpose-s_8xb256-420e_coco-256x192.py https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmpose-s_simcc-aic-coco_pt-aic-coco_420e-256x192-fcb2599b_20230126.pth --input data/test/mother_wx.mp4 --output-root outputs/B1_RTM_2 --device cuda:0 --bbox-thr 0.5 --kpt-thr 0.5 --nms-thr 0.3 --radius 5 --thickness 2 --draw-bbox --draw-heatmap  --show-kpt-idx
        
      • Loads checkpoint by http backend from path: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
        Loads checkpoint by http backend from path: https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmpose-s_simcc-aic-coco_pt-aic-coco_420e-256x192-fcb2599b_20230126.pth
        06/03 23:14:39 - mmengine - WARNING - `Visualizer` backend is not initialized because save_dir is None.
        d:\workspace\.git\mmpose\mmpose\models\heads\coord_cls_heads\rtmcc_head.py:217: UserWarning: The predicted si
        mcc values are normalized for visualization. This may cause discrepancy between the keypoint scores and the 1D heatmaps.
          warnings.warn('The predicted simcc values are normalized for '
  • MMPose预测训练模型预测-Python API
    • import cv2
      import numpy as np
      from PIL import Image
      import matplotlib.pyplot as plt
      import torch
      import mmcv
      from mmcv import imread
      import mmengine
      from mmengine.registry import init_default_scope
      from mmpose.apis import inference_topdown
      from mmpose.apis import init_model as init_pose_estimator
      from mmpose.evaluation.functional import nms
      from mmpose.registry import VISUALIZERS
      from mmpose.structures import merge_data_samples
      from mmdet.apis import inference_detector, init_detector
      
      # 设备
      device = torch.device('cuda') if torch.cuda.is_available() else torch.device(
          'cpu')
      print('device:', device)
      # 载入待测图片
      img_path = 'data/test/multi-person.jpeg'
      # 打开图片
      Image.open(img_path)
      # 构建目标检测模型
      # Faster RCNN
      detector = init_detector(
          'demo/mmdetection_cfg/faster_rcnn_r50_fpn_coco.py',
          'https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth',
          device=device)
      # RTMPose-Tiny
      detector = init_detector(
          'projects/rtmpose/rtmdet/person/rtmdet_m_640-8xb32_coco-person.py',
          'https://download.openmmlab.com/mmpose/v1/projects/rtmpose/rtmdet_m_8xb32-100e_coco-obj365-person-235e8209.pth',
          device=device)
      # 构建人体姿态估计模型
      pose_estimator = init_pose_estimator(
          'configs/body_2d_keypoint/topdown_heatmap/coco/td-hm_hrnet-w32_8xb64-210e_coco-256x192.py',
          'https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w32_coco_256x192-c78dce93_20200708.pth',
          device=device,
          cfg_options={'model': {
              'test_cfg': {
                  'output_heatmaps': True
              }
          }})
      # 预测-目标检测
      init_default_scope(detector.cfg.get('default_scope', 'mmdet'))
      #获取目标检测预测结果
      detect_result = inference_detector(detector, img_path)
      detect_result.keys()
      # 预测类别
      print(detect_result.pred_instances.labels)
      # 置信度
      print(detect_result.pred_instances.scores)
      # 框坐标:左上角X坐标、左上角Y坐标、右下角X坐标、右下角Y坐标
      print(detect_result.pred_instances.bboxes)
      # 置信度阈值过滤,获得最终目标检测预测结果
      # 置信度阈值
      CONF_THRES = 0.5
      pred_instance = detect_result.pred_instances.cpu().numpy()
      bboxes = np.concatenate((pred_instance.bboxes, pred_instance.scores[:, None]), axis=1)
      bboxes = bboxes[np.logical_and(pred_instance.labels == 0, pred_instance.scores > CONF_THRES)]
      bboxes = bboxes[nms(bboxes, 0.3)][:, :4]
      print(bboxes)
      # 预测-关键点
      # 获取每个 bbox 的关键点预测结果
      pose_results = inference_topdown(pose_estimator, img_path, bboxes)
      print(len(pose_results))
      # 把多个bbox的pose结果打包到一起
      data_samples = merge_data_samples(pose_results)
      print(data_samples.keys())
      # 预测结果-关键点坐标
      print(data_samples.pred_instances.keypoints.shape)
      # 索引为 0 的人,每个关键点的坐标
      print(data_samples.pred_instances.keypoints[0,:,:])
      # 预测结果-关键点热力图
      # 每一类关键点的预测热力图
      print(data_samples.pred_fields.heatmaps.shape)
      idx_point = 13
      heatmap = data_samples.pred_fields.heatmaps[idx_point,:,:]
      print(heatmap.shape)
      # 索引为 idx 的关键点,在全图上的预测热力图
      plt.imshow(heatmap)
      plt.show()

    • MMPose官方可视化工具visualizer

      • # 半径
        pose_estimator.cfg.visualizer.radius = 10
        # 线宽
        pose_estimator.cfg.visualizer.line_width = 8
        visualizer = VISUALIZERS.build(pose_estimator.cfg.visualizer)
        # 元数据
        visualizer.set_dataset_meta(pose_estimator.dataset_meta)
        # 可视化
        img = mmcv.imread(img_path)
        img = mmcv.imconvert(img, 'bgr', 'rgb')
        
        img_output = visualizer.add_datasample(
            'result',
            img,
            data_sample=data_samples,
            draw_gt=False,
            draw_heatmap=True,
            draw_bbox=True,
            show_kpt_idx=True,
            show=False,
            wait_time=0,
            out_file='outputs/B2.jpg')
        print(img_output.shape)
        plt.figure(figsize=(10, 10))
        plt.imshow(img_output)
        plt.show()

  • 多人检测问题

  • 代码实现:

<think>好的,我现在需要解决用户遇到的conda环境找不到的问题,具体是找不到名为mmpose3d的环境。用户之前引用了几个关于tensorflow环境问题的解决案例,我需要根据这些信息来推导解决方案。 首先,用户的问题提示“Could not find conda environment: mmpose3d”,这和引用中提到的tensorflow环境问题类似。根据引用[1]和[3],这可能是因为环境未被正确创建或者激活时名称错误。我需要先确认用户是否已经创建了该环境。如果尚未创建,用户需要按照引用[4]中的步骤使用conda create命令来创建。例如,conda create -n mmpose3d python=版本号。 接着,用户可能需要检查现有的环境列表。根据引用[1]和[4],使用conda info --envs或conda env list可以列出所有环境,确认mmpose3d是否存在。如果不存在,就需要创建;如果存在但无法激活,可能是路径问题或者环境损坏。 如果环境存在但激活失败,引用[5]提到可以直接通过路径激活,比如conda activate 环境路径。这可能适用于环境在非默认目录的情况。此外,用户的环境可能被安装在其他位置,需要确认是否正确指定了路径。 另外,引用[4]中的步骤提到可能需要以管理员身份运行Anaconda Prompt,尤其是在Windows系统下,权限问题可能导致环境无法识别。因此,建议用户尝试以管理员身份运行终端,再进行激活操作。 如果环境存在但激活仍然失败,可能需要检查环境是否完整。有时候安装过程中断可能导致环境损坏,此时可能需要重新创建环境。用户也可以尝试更新conda工具本身,确保版本是最新的,避免已知的bug。 最后,用户可能需要安装mmpose3d相关的包,这可能需要特定的渠道或者依赖。例如,使用conda install命令时指定正确的频道,或者使用pip安装,如果conda仓库中没有该包的话。需要提醒用户在激活环境后安装所需的包,以确保包被正确安装到目标环境中。 总结步骤应该是:检查环境是否存在→不存在则创建→存在则尝试路径激活→检查权限问题→更新conda→重新安装环境→安装所需包。同时,引用相关的解决方案作为支持,比如引用[4]中的创建环境和引用[5]的路径激活方法。</think>### 解决“找不到名为mmpose3d的conda环境”的步骤 #### 1. 确认环境是否存在 首先检查是否已创建该环境: ```bash conda info --envs ``` 在输出列表中查找`mmpose3d`。若未找到,说明环境未创建,需执行下一步[^1][^4]。 #### 2. 创建新环境 若环境未创建,使用以下命令创建(指定Python版本): ```bash conda create -n mmpose3d python=3.8 # 以Python 3.8为例,按需调整版本 ``` 按提示输入`y`确认安装依赖。 #### 3. 激活环境 创建成功后,尝试激活: ```bash conda activate mmpose3d ``` 若仍报错,尝试通过完整路径激活(需从`conda info --envs`中获取环境路径)[^5]: ```bash conda activate /your/env/path/mmpose3d ``` #### 4. 检查权限问题(Windows系统) 在Windows系统中,**以管理员身份运行Anaconda Prompt**再执行激活命令,避免权限不足导致失败。 #### 5. 环境修复或重建 若环境已存在但损坏,可尝试: ```bash conda remove -n mmpose3d --all # 删除旧环境 conda create -n mmpose3d python=3.8 # 重新创建 ``` #### 6. 安装mmpose3d相关包 激活环境后安装所需工具: ```bash conda install package_name # 通过conda安装 # 或使用pip(若conda仓库无对应包) pip install mmpose3d ``` --- ### 关键操作示例 ```bash # 创建环境 conda create -n mmpose3d python=3.8 conda activate mmpose3d # 安装依赖 pip install torch torchvision pip install mmpose mmcv-full ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值