参考:Python+OpenCV+OpenPose实现人体姿态估计(人体关键点检测)
应用opencv的神经网络模块加载关键点检测网络进行人体的关键点检测。
值得注意的是openpose检测关键点速度很慢,无法做到实时检测,更不要说在移动设备上运行了。
图片来自参考博客
两个分支最后得到的是关节置信度分布图和关节亲和度分布图(个人理解)
图片来自参考博客
前十层为VGG19的前十层,进行特征提取。
后面分为两个分支:
第一个分支得到的是人体各个关节的置信度,用于标记关键点。值得注意的是,输出为2d,如果图片中人体有重叠预测结果往往不太好。
第二个分支得到的是第一个分支得到的各个关节间的亲和度。
两个分支输出都为t个纬度。
值得注意的是,下面的代码只用到了第一个分支得到的各个关节的置信度,通过键值对的方式手动连接关键点。
处理速度比较慢
import cv2
import os
import glob
# 关键点名称
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"]]
def detect_key_point(model_path, image_path, out_dir, inWidth= 368, inHight= 368, threshhold= 0.2):
# 读取关键点检测网络
net = cv2.dnn.readNetFromTensorflow(model_path)
# frame = image_path
# 读取图片
frame = cv2.imread(image_path)
# 打印图片的形状
print('frame.shape:')
print(frame.shape)
frameWidth = frame.shape[1]
frameHight = frame.shape[0]
# 缩放大小
scalefactor = 2.0
# 进行图片预处理后输入网络
net.setInput(cv2.dnn.blobFromImage(frame, scalefactor= scalefactor,
size= (frameHight, frameWidth), mean= (127.5,127.5,127.5), swapRB= True, crop= False))
# 获取输出
out = net.forward()
print('out.shape:')
print(out.shape)
out = out[:, :19, :, :]
# 判断输出是否正常
assert len(BODY_PARTS) == out.shape[1]
# 定义关键点保存缓存
points = []
# 获取所有关键点的位置
for i in range(len(BODY_PARTS)):
# 应为图像的预处理操作将图像缩放过,所以这里就有100*100个点的置信度
heatMap = out[0, i , :, :]
print('heatMap.shape:')
print(heatMap.shape)
# 获取最大值和最大值索引
# 通过这种方式去找到最大置信度的关节
# 一个一个地找要查找的关节
_, conf, _, point = cv2.minMaxLoc(heatMap)
print('conf:')
print(conf)
print('point:')
print(point)
x = (frameWidth * point[0]) / out.shape[3]
print(x)
y = (frameHight * point[1]) / out.shape[2]
print(y)
# 置信度必须大于阈值则认为是所需关节
points.append((int(x), int(y)) if conf > threshhold else None)
# 找到了19个关键点
print(len(points))
# 根据各个关键点的连接关系来进行关键点的链接
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]:
cv2.line(frame, points[idFrom], points[idTo], (0,255,0), thickness= 5)
cv2.circle(frame, points[idFrom], 4, (255,0,0), thickness= 4)
cv2.circle(frame, points[idTo], 4, (255, 0, 0), thickness=4)
t, _ = net.getPerfProfile()
# 获取时钟频率,函数运行的时间
freq = cv2.getTickFrequency() / 100
print('freq')
print(freq)
cv2.putText(frame, '%.2fms' % (t / freq), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
#cv2.imwrite(os.path.join(out_dir, os.path.basename(image_path)), frame)
cv2.imshow('OpenPose using OpenCV', frame)
cv2.waitKey(0)
detect_key_point('./graph_opt.pb', image_path= './image.jpg', out_dir= 1)
最后结果,不咋准。。。
摄像头检测,更不准,不知道哪里出了问题了。
链接:https://pan.baidu.com/s/1wfEBHA7sBgOstUyS7onHEg
提取码:w93b
姿态估计各种方法对比结果
如有错误,还请评论指出。