-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoseEstimation.py
238 lines (188 loc) · 5.93 KB
/
PoseEstimation.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
#'Lank', = 27
# 'Lelb', = 13
# 'Lhip', = 23
# 'Lkne', = 25
# 'Lsho', = 11
# 'Lwri', = 15
# 'Rank', = 28
# 'Relb', = 14
# 'Rhip', = 24
# 'Rkne', = 26
# 'Rsho', = 12
# 'Rwri', = 16
#go to mediapipe website to see which point corresponds to which number
import warnings
warnings.filterwarnings('ignore')
try:
import ujson as json
except ImportError:
try:
import simplejson as json
except ImportError:
import json
import pickle
import numpy as np
from ipykernel import kernelapp as app
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import signal
from scipy import stats
from scipy.integrate import simps
from scipy.spatial import ConvexHull
import sklearn
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
sns.set(font_scale=1.2)
import cv2 as cv
import mediapipe as mp
import time
import numpy as np
import pickle
class poseDetector():
def __init__(self, mode = False, upBody = False, smooth = True, detectionCon = 0.5, trackCon = 0.5):
self.mpDraw = mp.solutions.drawing_utils
self.mpPose = mp.solutions.pose
self.pose = self.mpPose.Pose()
def findPose(self, img, draw = True):
imgRGB = cv.cvtColor(img, cv.COLOR_BGR2RGB)
self.results = self.pose.process(imgRGB)
blank = np.zeros(img.shape, dtype='uint8')
if self.results.pose_landmarks:
if draw:
self.mpDraw.draw_landmarks(blank, self.results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
return blank
def findPosition(self, img, draw = True):
lmList = []
if self.results.pose_landmarks:
for id, lm in enumerate(self.results.pose_landmarks.landmark):
h, w, c = img.shape
# print(id, lm)
cx, cy = int(lm.x * w), int(lm.y * h) #might be able to get z-values and visibility values if you want
lmList.append([id, cx, cy])
if draw:
cv.circle(img, (cx, cy), 5, (255, 0, 0), cv.FILLED)
return lmList
def main():
filename = "Untitled Folder/dancing.mp4"
cap = cv.VideoCapture(filename)
pTime = 0
detector = poseDetector()
Lank = []
Lelb = []
Lhip = []
Lkne = []
Lsho = []
Lwri = []
Rank = []
Relb = []
Rhip = []
Rkne = []
Rsho = []
Rwri = []
while True:
success, img = cap.read()
img = detector.findPose(img)
lmList = detector.findPosition(img, draw=False)
average = [0, 0] # change with mean
try:
arr = lmList[27]
arr = arr[1:]
Lank.append(arr)
except:
Lank.append(average)
try:
arr = lmList[28]
arr = arr[1:]
Rank.append(arr)
except:
Rank.append(average)
try:
arr = lmList[13]
arr = arr[1:]
Lelb.append(arr)
except:
Lelb.append(average)
try:
arr = lmList[14]
arr = arr[1:]
Relb.append(arr)
except:
Relb.append(average)
try:
arr = lmList[23]
arr = arr[1:]
Lhip.append(arr)
except:
Lhip.append(average)
try:
arr = lmList[24]
arr = arr[1:]
Rhip.append(arr)
except:
Rhip.append(average)
try:
arr = lmList[25]
arr = arr[1:]
Lkne.append(arr)
except:
Lkne.append(average)
try:
arr = lmList[26]
arr = arr[1:]
Rkne.append(arr)
except:
Rkne.append(average)
try:
arr = lmList[11]
arr = arr[1:]
Lsho.append(arr)
except:
Lsho.append(average)
try:
arr = lmList[12]
arr = arr[1:]
Rsho.append(arr)
except:
Rsho.append(average)
try:
arr = lmList[15]
arr = arr[1:]
Lwri.append(arr)
except:
Lwri.append(average)
try:
arr = lmList[16]
arr = arr[1:]
Rwri.append(arr)
except:
Rwri.append(average)
#print(Rsho) #this gives you all the coordinates at point 14 given in mediapipe website
cTime = time.time()
fps = 1/(cTime - pTime)
pTime = cTime
cv.putText(img, str(int(fps)), (70,50), cv.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)
cv.imshow("Image", img)
if cv.waitKey(1) & 0xFF == ord('q') or success == False:
break
position = {"Lank" : Lank, "Lelb" : Lelb, "Lhip" : Lhip, "Lkne" : Lkne, "Lsho" : Lwri, "Lwri" : Lwri, "Rank" : Rank, "Relb" : Relb, "Rhip" : Rhip, "Rkne" : Rkne, "Rsho" : Rwri, "Rwri" : Rwri}
loaded_model = pickle.load(open('Untitled Folder\Lleg_decision_tree_model.sav', 'rb'))
result = loaded_model.score(position)
print(result)
# f = open("data.txt", "w")
# # d = {"01" : {'position' : position}}
# f.write(str(d))
# f.close()
# print(position)
# import csv
# header = ["Lank", "Lelb", "Lhip", "Lkne", "Lsho", "Lwri", "Rank", "Relb", "Rhip", "Rkne", "Rsho", "Rwri"]
# data = [Lank, Lelb, Lhip, Lkne, Lsho, Lwri, Rank, Relb, Rhip, Rkne, Rsho, Rwri]
# with open('text_copy.csv', 'w', encoding='UTF8') as f:
# writer = csv.writer(f)
# # write the header
# writer.writerow(header)
# # write the data
# writer.writerow(data)
if __name__ == "__main__":
main()