在了解了上篇文章OpenPose笔记——win10下,VS2017和Python API可成功编译OpenPose啦!!!的前提下,我们利用OpenPose已训练的手部关键点检测模型,进行手部姿态的打分。
具体实现
主要针对握拳这个手部动作进行打分,使用DTW算法,根据两个视频序列相似度打分。
1、下载手部关键点检测模型
[1] - hand/pose_deploy.prototxt
[2] - hand/pose_iter_102000.caffemodel
2、关键代码部分
#!/usr/bin/python3
#!--*-- coding: utf-8 --*--
import cv2 as cv
import numpy as np
import argparse
import numpy as np
float_formatter = lambda x: "%.2f" % x
np.set_printoptions(formatter={'float_kind': float_formatter})
def TimeSeriesSimilarity(s1, s2):
l1 = len(s1)
l2 = len(s2)
paths = np.full((l1 + 1, l2 + 1), np.inf) # 全部赋予无穷大
paths[0, 0] = 0
for i in range(l1):
for j in range(l2):
d = s1[i] - s2[j]
cost = d ** 2
paths[i + 1, j + 1] = cost + min(paths[i, j + 1], paths[i + 1, j], paths[i, j])
paths = np.sqrt(paths)
s = paths[l1, l2]
return s, paths.T
parser = argparse.ArgumentParser(
description='This script is used to demonstrate OpenPose human pose estimation network '
'from https://github.com/CMU-Perceptual-Computing-Lab/openpose project using OpenCV. '
'The sample and model are simplified and could be used for a single person on the frame.')
parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
parser.add_argument('--proto', help='Path to .prototxt')
parser.add_argument('--model', help='Path to .caffemodel')
parser.add_argument('--dataset', help='Specify what kind of model was trained. '
'It could be (COCO, HAND) depends on dataset.')
parser.add_argument('--thr', default=0.1, 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.')
parser.add_argument('--scale', default=0.003922, type=float, help='Scale for blob.')
args = parser.parse_args()
if args.dataset == 'COCO':
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"] ]
else:
assert(args.dataset == 'HAND')
BODY_PARTS = { "Wrist": 0,
"Thumbmetacarpal": 1, "ThumbProximal": 2, "ThumbMiddle": 3, "ThumbDistal": 4,
"IndexFingermetacarpal": 5, "IndexFingerProximal": 6, "IndexFingerMiddle": 7, "IndexFingerDistal": 8,
"MiddleFingermetacarpal": 9, "MiddleFingerProximal": 10, "MiddleFingerMiddle": 11, "MiddleFingerDistal": 12,
"RingFingermetacarpal": 13, "RingFingerProximal": 14, "RingFingerMiddle": 15, "RingFingerDistal": 16,
"LittleFingermetacarpal": 17, "LittleFingerProximal": 18, "LittleFingerMiddle": 19, "LittleFingerDistal": 20,
}
POSE_PAIRS = [ ["Wrist", "Thumbmetacarpal"], ["Thumbmetacarpal", "ThumbProximal"],
["ThumbProximal", "ThumbMiddle"], ["ThumbMiddle", "ThumbDistal"],
["Wrist", "IndexFingermetacarpal"], ["IndexFingermetacarpal", "IndexFingerProximal"],
["IndexFingerProximal", "IndexFingerMiddle"], ["IndexFingerMiddle", "IndexFingerDistal"],
["Wrist", "MiddleFingermetacarpal"], ["MiddleFingermetacarpal", "MiddleFingerProximal"],
["MiddleFingerProximal", "MiddleFingerMiddle"], ["MiddleFingerMiddle", "MiddleFingerDistal"],
["Wrist", "RingFingermetacarpal"], ["RingFingermetacarpal", "RingFingerProximal"],
["RingFingerProximal", "RingFingerMiddle"], ["RingFingerMiddle", "RingFingerDistal"],
["Wrist", "LittleFingermetacarpal"], ["LittleFingermetacarpal", "LittleFingerProximal"],
["LittleFingerProximal", "LittleFingerMiddle"], ["LittleFingerMiddle", "LittleFingerDistal"] ]
inWidth = args.width
inHeight = args.height
inScale = args.scale
net = cv.dnn.readNet(cv.samples.findFile(args.proto), cv.samples.findFile(args.model))
s1 = []
videoCapture = cv.VideoCapture("D:/test1.mp4")
success=True
a1=0
n=0
d1=0#第一帧差
handlong =0
hand = True
score=0
first = True
s2=[2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 1.9986702127659575, 2.083776595744681, 1.9986702127659575, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.083776595744681, 2.0638297872340425, 2.0425531914893615, 2.0425531914893615, 2.0425531914893615, 2.0425531914893615, 2.0425531914893615, 1.9800531914893618, 1.9587765957446808, 1.9587765957446808, 1.9587765957446808, 1.9587765957446808, 1.9375, 1.8949468085106382, 1.875, 1.8537234042553192, 1.8324468085106382, 1.8125, 1.8125, 1.7513297872340425, 1.7513297872340425, 1.7513297872340425, 1.7513297872340425, 1.7513297872340425, 1.7513297872340425, 1.7300531914893618, 1.7300531914893618, 1.6050531914893618, 1.6050531914893618, 1.5625, 1.5412234042553192, 1.5412234042553192, 1.5212765957446808, 1.4800531914893618, 1.4800531914893618, 1.4800531914893618, 1.4375, 1.4375, 1.3324468085106382, 1.3962765957446808, 1.375, 1.3949468085106382, 1.375, 1.3337765957446808, 1.3138297872340425, 1.3337765957446808, 1.3125, 1.2912234042553192, 1.2912234042553192, 1.25, 1.2087765957446808, 1.4175531914893618, 1.3949468085106382, 1.3736702127659575, 1.3537234042553192, 1.3324468085106382, 1.2300531914893618, 1.1875, 1.1875, 1.0837765957446808, 1.0837765957446808, 1.0837765957446808, 0.9800531914893617, 0.9800531914893617, 0.9587765957446809, 0.9800531914893617, 0.8763297872340425, 0.9587765957446809, 0.9587765957446809, 0.9800531914893617, 1.0013297872340425, 0.9800531914893617, 0.9800531914893617, 0.9800531914893617, 0.9800531914893617, 1.0013297872340425, 0.9800531914893617, 1.0013297872340425]
while success:
#hasframe, frame = cap.read()
success,frame = videoCapture.read()
if not success:
cv.waitKey()
break
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
#blobFromImage将图像转为blob
inp = cv.dnn.blobFromImage(frame, inScale, (inWidth, inHeight),
(0, 0, 0), swapRB=False, crop=False)
net.setInput(inp)
out = net.forward() # forward实现网络推断
assert(len(BODY_PARTS) <= out.shape[1])
points = []
for i in range(len(BODY_PARTS)):
# Slice heatmap of corresponding 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() / 1000
cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
RingFingerDistal1= points[BODY_PARTS['RingFingerDistal']]
RingFingermetacarpal1 = points[BODY_PARTS['RingFingermetacarpal']]
MiddleFingerDistal1= points[BODY_PARTS['MiddleFingerDistal']]
MiddleFingermetacarpal1= points[BODY_PARTS['MiddleFingermetacarpal']]
IndexFingerDistal1= points[BODY_PARTS['IndexFingerDistal']]
IndexFingermetacarpal1= points[BODY_PARTS['IndexFingermetacarpal']]
LittleFingerDistal1= points[BODY_PARTS['LittleFingerDistal']]
Wrist1= points[BODY_PARTS['Wrist']]
if RingFingerDistal1: #计算一共多少帧
n+=1
if MiddleFingerDistal1:
n+=1
if IndexFingerDistal1:
n+=1
if LittleFingerDistal1:
n+=1
if hand is True:
handlong=MiddleFingermetacarpal1[1]-Wrist1[1] #手掌长度,这是不变的
hand=False
#print(RingFingerDistal1[1])
#除大拇指指尖外,四指指尖与手腕点插值的均值
a1=((RingFingerDistal1[1]-Wrist1[1])+(MiddleFingerDistal1[1]-Wrist1[1])+(IndexFingerDistal1[1]-Wrist1[1])+(LittleFingerDistal1[1]-Wrist1[1]))/(n*handlong)
if first is True:
d1=2.083776595744681-a1
first = False
if Wrist1:
print(a1+d1)
s1.append(a1+d1)
n=0
#相似程度;s1是测试视频序列,s2是模板视频序列
print(s1)
distance, paths = TimeSeriesSimilarity(s1, s2)
print("=" * 60)
print(distance)
print("=" * 60)
print(1/(1+distance))
print("=" * 60)
score=(1/(1+distance))*200
if score>100:
score=100
print(score)
3、实验结果
1)手掌张开
2)1/4握拳
3)1/3握拳
4)握拳
目前上述只是一个简单版本,且存在很多不精确的地方,后期考虑增加手势的深度信息。



