基于OpenPose的人体姿态检测

一、概述

  • OpenPose最开始由卡内基梅隆大学提出,其主要基于先后发表的几篇文章中提出的模型中进行实现:
    • CVPR 2016: Convolutional Pose Machine(CPM)
    • CVPR2017 : realtime multi-person pose estimation
    • CVPR2017 : Hand Keypoint Detection in Single Images using Multiview Bootstrapping
  • 但运行计算量非常大,通常得在GPU上运行,并且帧率较低(低于5fps),在此后也陆续出现了一些改进版
  • 改进版主要在模型上进行了一些改进或裁剪,另外移动端(如各种尬舞app) 为能够跑通OpenPose,在改网络结构的同时,对算法本身也进行了优化,减少了计算量,但与此同时准确性也有相应地降低。

二、简化版OpenPose实现代码

  • 代码来源GitHub:human-pose-estimation-opencv
  • 其代码较为简单,模型(较小:7.8M)已经训练好在graph_opt.pb文件中,其中全部实现代码在openpose.py文件中,下面是实现代码及测试效果:
# To use Inference Engine backend, specify location of plugins:
# export LD_LIBRARY_PATH=/opt/intel/deeplearning_deploymenttoolkit/deployment_tools/external/mklml_lnx/lib:$LD_LIBRARY_PATH
import cv2 as cv
import numpy as np
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
parser.add_argument('--thr', default=0.2, type=float, help='Threshold value for pose parts heat map')
parser.add_argument('--width', default=368, type=int, help='Resize input to specific width.')
parser.add_argument('--height', default=368, type=int, help='Resize input to specific height.')

args = parser.parse_args()

BODY_PARTS = {
    "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
               "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
               "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
               "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }

POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
               ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
               ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
               ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
               ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]

inWidth = args.width
inHeight = args.height

net = cv.dnn.readNetFromTensorflow("graph_opt.pb")

cap = cv.VideoCapture(args.input if args.input else 0)

while cv.waitKey(1) < 0:
    hasFrame, frame = cap.read()
    if not hasFrame:
        cv.waitKey()
        break

    frameWidth = frame.shape[1]
    frameHeight = frame.shape[0]
    
    net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
    out = net.forward()
    out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements

    assert(len(BODY_PARTS) == out.shape[1])

    points = []
    for i in range(len(BODY_PARTS)):
        # Slice heatmap of corresponging body's part.
        heatMap = out[0, i, :, :]

        # Originally, we try to find all the local maximums. To simplify a sample
        # we just find a global one. However only a single pose at the same time
        # could be detected this way.
        _, conf, _, point = cv.minMaxLoc(heatMap)
        x = (frameWidth * point[0]) / out.shape[3]
        y = (frameHeight * point[1]) / out.shape[2]
        # Add a point if it's confidence is higher than threshold.
        points.append((int(x), int(y)) if conf > args.thr else None)

    for pair in POSE_PAIRS:
        partFrom = pair[0]
        partTo = pair[1]
        assert(partFrom in BODY_PARTS)
        assert(partTo in BODY_PARTS)

        idFrom = BODY_PARTS[partFrom]
        idTo = BODY_PARTS[partTo]

        if points[idFrom] and points[idTo]:
            cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
            cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)

    t, _ = net.getPerfProfile()
    freq = cv.getTickFrequency() / 10
基于openpose人体姿态估计代码是一种利用计算机视觉技术来检测人体姿态的方法。openpose是一种开源的深度学习框架,可以通过分析图像或视频中的人体关节位置来实现人体姿态估计。 该代码基于卷积神经网络和目标检测算法,首先对输入的图像进行预处理,如将其转换为灰度图像或进行归一化处理。然后,使用卷积神经网络提取图像特征,并通过目标检测算法定位人体关节点。 在代码中,需要通过训练数据集来训练模型。训练数据集中包含了大量的标注数据,即图像中人体关节点的位置。通过多次迭代训练,模型可以学习到关节点的特征和位置信息。 在测试阶段,将输入图像传入已经训练好的模型中,模型会输出预测的人体姿态结果。这些结果包括人体的骨骼结构和每个关节点的位置信息。通过对关节点的位置进行分析和处理,可以实现多种应用,比如动作识别人体跟踪和手势识别等。 基于openpose人体姿态估计代码可以应用于各种领域,如体育训练、医学研究和虚拟现实等。它可以帮助教练监控运动员的动作,帮助医生诊断疾病,也可以用于虚拟现实游戏和应用中,提供更加真实的人机交互体验。 总之,基于openpose人体姿态估计代码是一种先进的计算机视觉技术,可以准确、快速地识别人体姿态。它在多个领域都有广泛的应用前景,为我们带来了更多的可能性。
评论 63
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值