openpose实操(三)基于OpenCV使用OpenPose进行多个人体姿态估计

本文详细介绍了如何使用OpenCV与OpenPose库进行多个人体姿态估计。通过加载模型、转换图像、网络推理及关键点检测,实现对人体关节的精确识别。最后讨论了如何组合关键点以区分不同人体。
摘要由CSDN通过智能技术生成

OpenPose 的 OpenCV DNN 实现
一,模型加载
1.建模,cv2.dnn.readNetFromCaffe(),需要两个参数,模型结构文件prototxt,及模型权重文件caffemodel。
2.cv2.dnn.blobFromImage用于把image转成网络输入的blob。
3.net.setInput() blob输入网络模型
4.output = net.forward() 模型推理输出

#!/usr/bin/python3
#!--*-- coding: utf-8 --*--
import cv2
import time
import numpy as np
import matplotlib.pyplot as plt

num_points = 18
keypointsMapping = ['Nose', 'Neck',
                    'R-Sho', 'R-Elb', 'R-Wr',
                    'L-Sho', 'L-Elb', 'L-Wr',
                    'R-Hip', 'R-Knee', 'R-Ank',
                    'L-Hip', 'L-Knee', 'L-Ank',
                    'R-Eye', 'L-Eye', 'R-Ear', 'L-Ear']

POSE_PAIRS = [[1,2], [1,5], [2,3], [3,4], [5,6], [6,7],
              [1,8], [8,9], [9,10], [1,11], [11,12], [12,13],
              [1,0], [0,14], [14,16], [0,15], [15,17],
              [2,17], [5,16] ]

# index of pafs correspoding to the POSE_PAIRS
# e.g for POSE_PAIR(1,2), the PAFs are located at indices (31,32) of output, Similarly, (1,5) -> (39,40) and so on.
mapIdx = [[31,32], [39,40], [33,34], [35,36], [41,42], [43,44],
          [19,20], [21,22], [23,24], [25,26], [27,28], [29,30],
          [47,48], [49,50], [53,54], [51,52], [55,56],
          [37,38], [45,46]]

colors = [[0,100,255], [0,100,255],   [0,255,255], 
          [0,100,255], [0,255,255],   [0,100,255],
          [0,255,0],   [255,200,100], [255,0,255], 
          [0,255,0],   [255,200,100], [255,0,255],
          [0,0,255],   [255,0,0],     [200,200,0], 
          [255,0,0],   [200,200,0],   [0,0,0]]

# dnn 加载模型
start = time.time()
prototxt = "./models/pose/coco/pose_deploy_linevec.prototxt"
caffemodel = "./models/pose/coco/pose_iter_440000.caffemodel"
net = cv2.dnn.readNetFromCaffe(prototxt, caffemodel)
print("[INFO]Time Taken in Model Loading: {}".format(time.time() - start))

# test img
img_cv2 = cv2.imread("test_image.jpg")
img_width, img_height = img_cv2.shape[1], img_cv2.shape[0]

# 根据长宽比,固定网路输入的 height,计算网络输入的 width.
net_height = 368
net_width = int((net_height/img_height)*img_width)

start = time.time()
in_blob = cv2.dnn.blobFromImage(
    img_cv2, 
    1.0 / 255, 
    (net_width, net_height),
    (0, 0, 0), 
    swapRB=False, 
    crop=False)

net.setInput(in_blob)
output = net.forward()
print("[INFO]Time Taken in Forward pass: {}".format(time.time() - start))

二,关键点检测
getKeyponts( )函数功能:对 Confidence Map 采用 NMS(Non Maximum Suppression) 来检测关键点.

def getKeypoints(probMap, threshold=0.1):
    mapSmooth = cv2.GaussianBlur(probMap,(3,3),0,0)
    mapMask = np.uint8(mapSmooth>threshold)
    
    keypoints = []
    # 1. 找出对应于关键点的所有区域的轮廓(contours)
    contours, hierarchy = cv2.findContours(
        ma
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值