MMPose的介绍及安装参考:https://blog.csdn.net/fengbingchun/article/details/126676309,这里给出人体姿势估计的测试代码,论文:《Deep high-resolution representation learning for human pose estimation》:
(1).准备测试图像:原始图像来自网络
image_path = "../../data/image/"
image_name = "human.png"
(2).通过MMDetection模块检测人体框:注,为了去除伪人体框,这里设置了一个阈值,只有大于此阈值的框才作为后面的人体姿势估计
def mmdet_human_detection(device, image, threshold=0.9):
path = "../../data/model/"
checkpoint = "faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
url = "https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
download_checkpoint(path, checkpoint, url)
config = "../../src/mmpose/demo/mmdetection_cfg/faster_rcnn_r50_fpn_coco.py"
model = init_detector(config, path+checkpoint, device)
mmdet_results = inference_detector(model, image)
# print(mmdet_results)
human_results = process_mmdet_results(mmdet_results)
# print(human_results)
filter_results = []
mat = cv2.imread(image)
for result in human_results:
print("result:", result)
if result['bbox'][4] > threshold:
filter_results.append(result)
cv2.rectangle(mat, (int(result['bbox'][0]), int(result['bbox'][1])), (int(result['bbox'][2]), int(result['bbox'][3])), (255, 0, 0), 1)
cv2.imwrite("../../data/result_mmpose_2d_human_detection.png", mat)
cv2.imshow("show", mat)
cv2.waitKey(0)
return filter_results
(3).下载人体姿势估计模型:
def download_checkpoint(path, name, url):
if os.path.isfile(path+name) == False:
print("checkpoint(model) file does not exist, now download ...")
subprocess.run(["wget", "-P", path, url])
path = "../../data/model/"
checkpoint = "hrnet_w48_coco_256x192-b9e0b3ab_20200708.pth"
url = "https://download.openmmlab.com/mmpose/top_down/hrnet/hrnet_w48_coco_256x192-b9e0b3ab_20200708.pth"
download_checkpoint(path, checkpoint, url)
(4).根据配置文件和checkpoint文件构建人体估计模型:
config = "../../src/mmpose/configs/body/2d_kpt_sview_rgb_img/topdown_heatmap/coco/hrnet_w48_coco_256x192.py"
model = init_pose_model(config, path+checkpoint, device)
(5).进行人体姿势估计推理,输入包括检测到的人体框:
pose_results, returned_outputs = inference_top_down_pose_model(model, image, human_bbox_results, bbox_thr=None, format='xyxy')
print(pose_results)
(6).显示及保存结果:
vis_pose_result(model, image, pose_results, radius=1, thickness=1, show=True, out_file="../../data/result_mmpose_2d_human_pose_estimation.png")
执行结果如下图所示:
GitHub: https://github.com/fengbingchun/PyTorch_Test