Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/HandPose/handPoseImage.py
3118 views
1
from __future__ import division
2
import cv2
3
import time
4
import numpy as np
5
6
protoFile = "hand/pose_deploy.prototxt"
7
weightsFile = "hand/pose_iter_102000.caffemodel"
8
nPoints = 22
9
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] ]
10
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
11
12
frame = cv2.imread("right-frontal.jpg")
13
frameCopy = np.copy(frame)
14
frameWidth = frame.shape[1]
15
frameHeight = frame.shape[0]
16
aspect_ratio = frameWidth/frameHeight
17
18
threshold = 0.1
19
20
t = time.time()
21
# input image dimensions for the network
22
inHeight = 368
23
inWidth = int(((aspect_ratio*inHeight)*8)//8)
24
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
25
26
net.setInput(inpBlob)
27
28
output = net.forward()
29
print("time taken by network : {:.3f}".format(time.time() - t))
30
31
# Empty list to store the detected keypoints
32
points = []
33
34
for i in range(nPoints):
35
# confidence map of corresponding body's part.
36
probMap = output[0, i, :, :]
37
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
38
39
# Find global maxima of the probMap.
40
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
41
42
if prob > threshold :
43
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
44
cv2.putText(frameCopy, "{}".format(i), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
45
46
# Add the point to the list if the probability is greater than the threshold
47
points.append((int(point[0]), int(point[1])))
48
else :
49
points.append(None)
50
51
# Draw Skeleton
52
for pair in POSE_PAIRS:
53
partA = pair[0]
54
partB = pair[1]
55
56
if points[partA] and points[partB]:
57
cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
58
cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
59
cv2.circle(frame, points[partB], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
60
61
62
cv2.imshow('Output-Keypoints', frameCopy)
63
cv2.imshow('Output-Skeleton', frame)
64
65
66
cv2.imwrite('Output-Keypoints.jpg', frameCopy)
67
cv2.imwrite('Output-Skeleton.jpg', frame)
68
69
print("Total time taken : {:.3f}".format(time.time() - t))
70
71
cv2.waitKey(0)
72
73