-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPose_Video.py
161 lines (125 loc) · 5.58 KB
/
Pose_Video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import collections
import time
import cv2
import numpy as np
# Model mode Selection
MODE = "MPII"
# Common object in context.
if MODE == "COCO":
protoFile = r"./<path>/pose_deploy_linevec_faster_4_stages.prototxt"
weightsFile = r"./<path>/pose_iter_440000.caffemodel"
nPoints = 18
POSE_PAIRS = [[1, 0], [1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], [9, 10], [1, 11],
[11, 12], [12, 13], [0, 14], [0, 15], [14, 16], [15, 17]]
# Multi-person II.
elif MODE == "MPII":
protoFile = r"./<path>/pose_deploy_linevec.prototxt"
weightsFile = r"./<path>/pose_iter_160000.caffemodel"
nPoints = 15
POSE_PAIRS = [[0, 1], [1, 2], [2, 3], [3, 4], [1, 5], [5, 6], [6, 7], [1, 14], [14, 8], [8, 9], [9, 10],
[14, 11], [11, 12], [12, 13]]
# Taking user input video
userImageInput = cv2.VideoCapture(r"./<path>/d2.mp4")
frame_width = int(userImageInput.get(3))
frame_height = int(userImageInput.get(4))
fps = int(userImageInput.get(5))
# Selecting the background image
img = r"./<path>/White.jpg"
frameWhite = cv2.imread(img)
frameWidth1 = frameWhite.shape[1]
frameHeight1 = frameWhite.shape[0]
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter(r"./<path>/1.avi", fourcc, 30, (frameWidth1, frameHeight1))
out1 = cv2.VideoWriter(r"./<path>/2.avi", fourcc, fps, (frame_width, frame_height))
threshold = 0.1
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
t = time.time()
inWidth = 368
inHeight = 368
while True:
ret, frame = userImageInput.read()
if not ret:
break
img = r"./<path>/White.jpg"
frameWhite = cv2.imread(img)
frameWidth1 = frameWhite.shape[1]
frameHeight1 = frameWhite.shape[0]
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
print("Total time taken by the network : {:.3f}".format(time.time() - t))
H = output.shape[2]
W = output.shape[3]
points = []
points1 = []
for i in range(nPoints):
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
# Scale the point to fit on the original RunningImage
x = (frameWidth * point[0]) / W
y = (frameHeight * point[1]) / H
x1 = (frameWidth1 * point[0]) / W
y1 = (frameHeight1 * point[1]) / H
if prob > threshold:
cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
cv2.circle(frameWhite, (int(x1) + 70, int(y1)), 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
points.append((int(x), int(y)))
points1.append((int(x1) + 70, int(y1)))
else:
points.append(None)
points1.append(None)
dictAngle = {'LeftHand': [5, 6, 7], 'LeftLeg': [11, 12, 13], 'RightHand': [2, 3, 4], 'RightLeg': [8, 9, 10]}
dictAngle = collections.OrderedDict(sorted(dictAngle.items()))
usernameDict = []
userangleDict = []
heightAngle = [350, 370, 390, 410]
j = 0
for i in dictAngle:
dictPoint1 = np.array(points[dictAngle[i][0]])
dictPoint2 = np.array(points[dictAngle[i][1]])
dictPoint3 = np.array(points[dictAngle[i][2]])
if str(dictPoint1) != 'None' and str(dictPoint2) != 'None' and str(dictPoint3) != 'None':
ba = dictPoint1 - dictPoint2
bc = dictPoint3 - dictPoint2
tup1 = points1[dictAngle[i][1]]
if i == 'LeftHand' or i == 'LeftLeg':
pointAngle = (tup1[0] + 15, tup1[1] + 18)
if i == 'RightHand' or i == 'RightLeg':
pointAngle = (tup1[0] - 50, tup1[1])
# print(point)
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
angle = np.arccos(cosine_angle)
ang = str(np.degrees(angle))
angleFloat = float(ang)
ang1 = round(angleFloat, 2)
# print(ang)
cv2.putText(frameWhite, "Right Side", (15, 40), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(frameWhite, "Left Side", (500, 40), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(frameWhite, "User-Image", (700, 420), cv2.FONT_HERSHEY_DUPLEX, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(frameWhite, str(ang1), pointAngle, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frameWhite, str(i + ": "), (15, heightAngle[j]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1,
cv2.LINE_AA)
cv2.putText(frameWhite, str(ang), (90, heightAngle[j]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1,
cv2.LINE_AA)
usernameDict.append(i)
userangleDict.append(ang)
else:
usernameDict.append(i)
userangleDict.append('0')
j += 1
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.line(frameWhite, points1[partA], points1[partB], (0, 255, 255), 2)
userDict = dict(zip(usernameDict, userangleDict))
out1.write(frame)
out.write(frameWhite)
AngleDict = str(
userDict['RightHand'] + "," + userDict['LeftLeg'] + "," + userDict['LeftHand'] + "," + userDict['RightLeg'])
print("Executed Successfully")