本文主要介绍通过opencv-python进行手势识别。
下载模型权值文件:http://posefs1.perception.cs.cmu.edu/OpenPose/models/hand/pose_iter_102000.caffemodel
下载proto file:https://download.csdn.net/download/github_39611196/10979308
下面是示例代码:
import cv2
import time
import numpy as np
protoFile = './hand/pose_deploy.prototxt'
weightFile = './hand/pose_iter_102000.caffemodel'
nPoints = 22
POSE_PAIRS = [[0,1], [1, 2], [2,3],[3,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[10,11],[11,12],[0,13],[13,14],[14,15],[15,16],[0,17],[17,18],[18,19],[19,20]]
net = cv2.dnn.readNetFromCaffe(protoFile, weightFile)
frame = cv2.imread('./right-frontal.jpg')
frameCopy = np.copy(frame)
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
aspect_ratio = frameWidth / frameHeight
threshold = 0.1
t = time.time()
# 网络输入图片的维度
inHeight = 368
# inWidth = 368
inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
inBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop = False)
net.setInput(inBlob)
output = net.forward()
print('time taken by network: {:.3f}'.format(time.time() - t))
import matplotlib.pyplot as plt
%matplotlib inline
for i in range(22):
probMap = output[0, i, :, i]
probMap = cv2.resize(probMap, (frame.shape[1], frame.shape[0]))
plt.figure(figsize=[14, 10])
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
plt.imshow(probMap, alpha=0.6)
plt.colorbar()
plt.axis("off")
# 用于存储检测到的关键点的空列表
points = []
for i in range(nPoints):
# 相应身体部分的置信度图。
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
# 找到置信度图的全局最大
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > threshold:
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 3, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
cv2.putText(frameCopy, "{}".format(i), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_SIMPLEX, .8, (0, 0, 255), 2, lineType=cv2.LINE_AA)
# 如果概率大于阈值,则将该点添加到列表中
points.append((int(point[0]), int(point[1])))
else:
points.append(None)
# 绘制骨架
for pair in POSE_PAIRS:
partA = pair[0]
partB = pair[1]
if points[partA] and points[partB]:
cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
cv2.circle(frame, points[partB], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
plt.figure(figsize=[10,10])
plt.imshow(cv2.cvtColor(frameCopy, cv2.COLOR_BGR2RGB))
plt.figure(figsize=[10,10])
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
cv2.imwrite('Output-Keypoints.jpg', frameCopy)
cv2.imwrite('Output-Skeleton.jpg', frame)
print("Total time taken : {:.3f}".format(time.time() - t))
测试数据:
运行效果: