-
Notifications
You must be signed in to change notification settings - Fork 0
/
Posturedection.py
303 lines (270 loc) · 12.1 KB
/
Posturedection.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import string
from turtle import left
import cv2
import mediapipe as mp
import math
from imutils.video import VideoStream
from imutils.video import FileVideoStream
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
import collections
import pyautogui
class PoseEstimator:
def __init__(self, window_size=8, smoothing_function=savgol_filter):
if(smoothing_function == 'savgol') and ((window_size % 2) == 0):
print('Is Here')
print(window_size)
self.window_size = window_size - 1
print(self.window_size)
else:
self.window_size = window_size
self.smoothing_function = smoothing_function
self.mp_drawing = mp.solutions.drawing_utils
self.mp_pose = mp.solutions.pose
self.pose = self.mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.1)
self.writer = None
self.coords_array = []
def get_pose_coords(self, image):
try:
image_height, image_width, _ = image.shape
results = self.pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if not results.pose_landmarks:
raise ValueError('No poses detected')
get_pose = results.pose_landmarks.landmark
lm = self.mp_pose.PoseLandmark
left_wrist_x = get_pose[lm.LEFT_WRIST].x*image_width
left_wrist_y = get_pose[lm.LEFT_WRIST].y*image_height
left_elbow_x = get_pose[lm.LEFT_ELBOW].x*image_width
left_elbow_y = get_pose[lm.LEFT_ELBOW].y*image_height
left_shoulder_x = get_pose[lm.LEFT_SHOULDER].x*image_width
left_shoulder_y = get_pose[lm.LEFT_SHOULDER].y*image_height
left_hip_x = get_pose[lm.LEFT_HIP].x*image_width
left_hip_y = get_pose[lm.LEFT_HIP].y*image_height
left_knee_x = get_pose[lm.LEFT_KNEE].x*image_width
left_knee_y = get_pose[lm.LEFT_KNEE].y*image_height
left_ankle_x = get_pose[lm.LEFT_ANKLE].x*image_width
left_ankle_y = get_pose[lm.LEFT_ANKLE].y*image_height
right_wrist_x = get_pose[lm.RIGHT_WRIST].x*image_width
right_wrist_y = get_pose[lm.RIGHT_WRIST].y*image_height
right_elbow_x = get_pose[lm.RIGHT_ELBOW].x*image_width
right_elbow_y = get_pose[lm.RIGHT_ELBOW].y*image_height
right_shoulder_x = get_pose[lm.RIGHT_SHOULDER].x*image_width
right_shoulder_y = get_pose[lm.RIGHT_SHOULDER].y*image_height
right_hip_x = get_pose[lm.RIGHT_HIP].x*image_width
right_hip_y = get_pose[lm.RIGHT_HIP].y*image_height
right_knee_x = get_pose[lm.RIGHT_KNEE].x*image_width
right_knee_y = get_pose[lm.RIGHT_KNEE].y*image_height
right_ankle_x = get_pose[lm.RIGHT_ANKLE].x*image_width
right_ankle_y = get_pose[lm.RIGHT_ANKLE].y*image_height
nose_x = get_pose[lm.NOSE].x*image_width
nose_y = get_pose[lm.NOSE].y*image_height
return (left_wrist_x, left_wrist_y, left_elbow_x, left_elbow_y, left_shoulder_x, left_shoulder_y, left_hip_x, left_hip_y, left_knee_x, left_knee_y, left_ankle_x, left_ankle_y,
right_wrist_x, right_wrist_y, right_elbow_x, right_elbow_y, right_shoulder_x, right_shoulder_y, right_hip_x, right_hip_y, right_knee_x, right_knee_y, right_ankle_x, right_ankle_y,
nose_x,nose_y)
except Exception as e:
print(e)
return None
def smoothen_coords(self, pose_coords):
if len(self.coords_array) == self.window_size:
self.coords_array.pop(0)
self.coords_array.append(pose_coords)
if self.smoothing_function == 'mean':
smoothened_coords = np.array(self.coords_array).mean(axis=0)
elif self.smoothing_function == 'savgol':
try:
savgol = lambda arr: savgol_filter(arr, self.window_size, 1)[-1]
coords_np_arr = np.array(self.coords_array)
smoothened_coords = np.apply_along_axis(savgol, 0,
coords_np_arr)
self.coords_array.pop()
self.coords_array.append(smoothened_coords)
except ValueError as ve:
print(ve)
return pose_coords
else:
return pose_coords
return tuple(smoothened_coords)
def get_annotated_image(self, image, pose_coords):
"""
Function to draw and visualize the coordinates in the image.
"""
left_wrist_x, left_wrist_y, left_elbow_x, left_elbow_y, left_shoulder_x, left_shoulder_y, left_hip_x, left_hip_y, left_knee_x, left_knee_y, left_ankle_x, left_ankle_y, right_wrist_x, right_wrist_y, right_elbow_x, right_elbow_y, right_shoulder_x, right_shoulder_y, right_hip_x, right_hip_y, right_knee_x, right_knee_y, right_ankle_x, right_ankle_y, nose_x, nose_y = pose_coords
annotated_image = image.copy()
##Drawing Cirlces
#Nose
cv2.circle(annotated_image,
(int(nose_x), int(nose_y)),
10,(0,0,255),-1)
#Shoulders
cv2.circle(annotated_image,
(int(left_shoulder_x), int(left_shoulder_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_shoulder_x), int(right_shoulder_y)),
10,(0,0,255),-1)
#Elbows
cv2.circle(annotated_image,
(int(left_elbow_x), int(left_elbow_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_elbow_x), int(right_elbow_y)),
10,(0,0,255),-1)
#Wrists
cv2.circle(annotated_image,
(int(left_wrist_x), int(left_wrist_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_wrist_x), int(right_wrist_y)),
10,(0,0,255),-1)
#Hips
cv2.circle(annotated_image,
(int(left_hip_x), int(left_hip_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_hip_x), int(right_hip_y)),
10,(0,0,255),-1)
#Knees
cv2.circle(annotated_image,
(int(left_knee_x), int(left_knee_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_knee_x), int(right_knee_y)),
10,(0,0,255),-1)
#Ankles
cv2.circle(annotated_image,
(int(left_ankle_x), int(left_ankle_y)),
10,(0,0,255),-1)
cv2.circle(annotated_image,
(int(right_ankle_x), int(right_ankle_y)),
10,(0,0,255),-1)
##Drawing Lines
#Nose-Shoulder
cv2.line(annotated_image,
(int(nose_x), int(nose_y)),
(int((left_shoulder_x+right_shoulder_x)/2), int((left_shoulder_y+right_shoulder_y)/2)),
(0,0,255),3)
#Shoulder
cv2.line(annotated_image,
(int(left_shoulder_x), int(left_shoulder_y)),
(int(right_shoulder_x), int(right_shoulder_y)),
(0,0,255),3)
#Shoulder-Elbow
cv2.line(annotated_image,
(int(left_shoulder_x), int(left_shoulder_y)),
(int(left_elbow_x), int(left_elbow_y)),
(0,0,255),3)
cv2.line(annotated_image,
(int(right_shoulder_x), int(right_shoulder_y)),
(int(right_elbow_x), int(right_elbow_y)),
(0,0,255),3)
#Elbow-Wrist
cv2.line(annotated_image,
(int(left_elbow_x), int(left_elbow_y)),
(int(left_wrist_x), int(left_wrist_y)),
(0,0,255),3)
cv2.line(annotated_image,
(int(right_elbow_x), int(right_elbow_y)),
(int(right_wrist_x), int(right_wrist_y)),
(0,0,255),3)
#Shoulder-Hip
cv2.line(annotated_image,
(int(left_shoulder_x), int(left_shoulder_y)),
(int(left_hip_x), int(left_hip_y)),
(0,0,255),3)
cv2.line(annotated_image,
(int(right_shoulder_x), int(right_shoulder_y)),
(int(right_hip_x), int(right_hip_y)),
(0,0,255),3)
#Hip
cv2.line(annotated_image,
(int(left_hip_x), int(left_hip_y)),
(int(right_hip_x), int(right_hip_y)),
(0,0,255),3)
#Hip-Knee
cv2.line(annotated_image,
(int(left_hip_x), int(left_hip_y)),
(int(left_knee_x), int(left_knee_y)),
(0,0,255),3)
cv2.line(annotated_image,
(int(right_hip_x), int(right_hip_y)),
(int(right_knee_x), int(right_knee_y)),
(0,0,255),3)
#Knee-Ankle
cv2.line(annotated_image,
(int(left_knee_x), int(left_knee_y)),
(int(left_ankle_x), int(left_ankle_y)),
(0,0,255),3)
cv2.line(annotated_image,
(int(right_knee_x), int(right_knee_y)),
(int(right_ankle_x), int(right_ankle_y)),
(0,0,255),3)
print(left_shoulder_x,right_shoulder_x)
return annotated_image,left_shoulder_x,right_shoulder_x
def write_image(self, image,l,r):
"""
Function for displaying the image.
"""
if self.writer is None:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
self.writer = cv2.VideoWriter("test6.mp4", fourcc, 25,
(image.shape[1], image.shape[0]), True)
self.writer.write(image)
show = cv2.resize(image, None,
fx=1, fy =1)
show = cv2.flip(image, 1)
if(l<500 or l>=600 or l>=550):
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (50, 50)
# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
l="Alert! change your posture!!!!"
image = cv2.putText(show, l, org, font,
fontScale, color, thickness, cv2.LINE_AA)
else:
image=show
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
return key
def run_estimator(self):
"""
Main Function to run the Pose Estimator.
"""
capture = cv2.VideoCapture(0)
while (capture.isOpened()):
# Read a frame
ret, image = capture.read(0)
if ret:
try:
# Get the pose coordinates in a tuple
pose_coords = self.get_pose_coords(image)
if pose_coords:
# If poses are detected then apply the smoothing filter
# And annotate the image
pose_coords = self.smoothen_coords(pose_coords)
annotated_image,lef,righ = self.get_annotated_image(image, pose_coords)
else:
# If no poses are detected, then just display the frame
pose_coords = None
self.write_image(image,lef,righ)
continue
# Write the annotated image
key = self.write_image(annotated_image,lef,righ)
except ValueError as ve:
print(ve)
key = self.write_image(image)
if key == ord("q"):
break
cv2.destroyAllWindows()
capture.release()
if self.writer is not None:
self.writer.release()
self.pose.close()
s = PoseEstimator(window_size=100)
s.run_estimator()