1. 程式人生 > 實用技巧 >MMSkeleton 快速開始,使用 WebCam 測試

MMSkeleton 快速開始,使用 WebCam 測試

本文將引導快速使用 MMSkeleton ,介紹用攝像頭測試實時姿態估計。

安裝

首先安裝 MMDetection ,可見 MMDetection 使用

然後安裝 MMSkeleton ,

# 啟用 Python 虛擬環境
conda activate open-mmlab

# 下載 MMSkeleton
git clone https://github.com/open-mmlab/mmskeleton.git
cd mmskeleton

# 安裝 MMSkeleton
python setup.py develop

# 安裝 nms op for person estimation
cd mmskeleton/ops/nms/
python setup_linux.py develop
cd ../../../

現有模型,視訊測試

配置

configs/pose_estimation/pose_demo.yaml:

processor_cfg:
  video_file: resource/data_example/ta_chi.mp4
  detection_cfg:
    model_cfg: ../mmdetection/configs/cascade_rcnn/cascade_rcnn_r50_fpn_1x_coco.py
    checkpoint_file: ../mmdetection/checkpoints/cascade_rcnn_r50_fpn_1x_coco_20200316-3dc56deb.pth
    bbox_thre: 0.8

選用的檢測模型,如下:

執行

# verify that mmskeleton and mmdetection installed correctly
# python mmskl.py pose_demo [--gpus $GPUS]
python mmskl.py pose_demo --gpus 1

結果將會存到 work_dir/pose_demo/ta_chi.mp4

現有模型,WebCam 測試

配置

configs/apis/pose_estimator.cascade_rcnn+hrnet.yaml

:

detection_cfg:
  model_cfg: mmdetection/configs/cascade_rcnn/cascade_rcnn_r50_fpn_1x_coco.py
  checkpoint_file: mmdetection/checkpoints/cascade_rcnn_r50_fpn_1x_coco_20200316-3dc56deb.pth
  bbox_thre: 0.8
estimation_cfg:
  model_cfg: mmskeleton/configs/pose_estimation/hrnet/pose_hrnet_w32_256x192_test.yaml
  checkpoint_file: mmskeleton://pose_estimation/pose_hrnet_w32_256x192
  data_cfg:
    image_size:
      - 192
      - 256
    pixel_std: 200
    image_mean:
      - 0.485
      - 0.456
      - 0.406
    image_std:
      - 0.229
      - 0.224
      - 0.225
    post_process: true

確認 detection_cfg estimation_cfg 的路徑正確。

寫碼

編寫 webcam.py,主要程式碼如下:

def main():
  args = parse_args()

  win_name = args.win_name
  cv.namedWindow(win_name, cv.WINDOW_NORMAL)

  with Camera(args.cam_idx, args.cam_width, args.cam_height, args.cam_fps) as cam:
    cfg = mmcv.Config.fromfile(args.cfg_file)
    detection_cfg = cfg["detection_cfg"]

    print("Loading model ...")
    model = init_pose_estimator(**cfg, device=0)
    print("Loading model done")

    for frame in cam.reads():
      res = inference_pose_estimator(model, frame)

      res_image = pose_demo.render(
          frame, res["joint_preds"], res["person_bbox"],
          detection_cfg.bbox_thre)

      cv.imshow(win_name, res_image)

      key = cv.waitKey(1) & 0xFF
      if key == 27 or key == ord("q"):
        break

  cv.destroyAllWindows()

執行

$ python webcam.py \
--cam_idx 2 --cam_width 640 --cam_height 480 --cam_fps 10 \
--cfg_file configs/apis/pose_estimator.cascade_rcnn+hrnet.yaml
Args
  win_name: webcam
  cam_idx: 2
  cam_width: 640
  cam_height: 480
  cam_fps: 10
  cfg_file: configs/apis/pose_estimator.cascade_rcnn+hrnet.yaml
CAM: 640.0x480.0 10.0
Loading model ...
Loading model done

效果,

攝像頭引數,可見 WebCam 攝像頭使用

更多

GoCoding 個人實踐的經驗分享,可關注公眾號!