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