Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/HandPose/handPoseVideo.py
3118 views
1
import cv2
2
import time
3
import numpy as np
4
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
11
threshold = 0.2
12
13
14
input_source = "asl.mp4"
15
cap = cv2.VideoCapture(input_source)
16
hasFrame, frame = cap.read()
17
18
frameWidth = frame.shape[1]
19
frameHeight = frame.shape[0]
20
21
aspect_ratio = frameWidth/frameHeight
22
23
inHeight = 368
24
inWidth = int(((aspect_ratio*inHeight)*8)//8)
25
26
vid_writer = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 15, (frame.shape[1],frame.shape[0]))
27
28
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
29
k = 0
30
while 1:
31
k+=1
32
t = time.time()
33
hasFrame, frame = cap.read()
34
frameCopy = np.copy(frame)
35
if not hasFrame:
36
cv2.waitKey()
37
break
38
39
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
40
(0, 0, 0), swapRB=False, crop=False)
41
42
net.setInput(inpBlob)
43
44
output = net.forward()
45
46
print("forward = {}".format(time.time() - t))
47
48
# Empty list to store the detected keypoints
49
points = []
50
51
for i in range(nPoints):
52
# confidence map of corresponding body's part.
53
probMap = output[0, i, :, :]
54
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
55
56
# Find global maxima of the probMap.
57
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
58
59
if prob > threshold :
60
cv2.circle(frameCopy, (int(point[0]), int(point[1])), 6, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
61
cv2.putText(frameCopy, "{}".format(i), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_SIMPLEX, .8, (0, 0, 255), 2, lineType=cv2.LINE_AA)
62
63
# Add the point to the list if the probability is greater than the threshold
64
points.append((int(point[0]), int(point[1])))
65
else :
66
points.append(None)
67
68
# Draw Skeleton
69
for pair in POSE_PAIRS:
70
partA = pair[0]
71
partB = pair[1]
72
73
if points[partA] and points[partB]:
74
cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2, lineType=cv2.LINE_AA)
75
cv2.circle(frame, points[partA], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
76
cv2.circle(frame, points[partB], 5, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
77
78
print("Time Taken for frame = {}".format(time.time() - t))
79
80
# cv2.putText(frame, "time taken = {:.2f} sec".format(time.time() - t), (50, 50), cv2.FONT_HERSHEY_COMPLEX, .8, (255, 50, 0), 2, lineType=cv2.LINE_AA)
81
# cv2.putText(frame, "Hand Pose using OpenCV", (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 50, 0), 2, lineType=cv2.LINE_AA)
82
cv2.imshow('Output-Skeleton', frame)
83
# cv2.imwrite("video_output/{:03d}.jpg".format(k), frame)
84
key = cv2.waitKey(1)
85
if key == 27:
86
break
87
88
print("total = {}".format(time.time() - t))
89
90
vid_writer.write(frame)
91
92
vid_writer.release()
93
94