-
Notifications
You must be signed in to change notification settings - Fork 0
/
judge.py
207 lines (163 loc) · 7.07 KB
/
judge.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
# abhi method for corrections
import ast
from math import *
# cords 1 is original perfect dance
# cords 2 is karthik perfect dance
def judge(modelFile, yourFile):
msOne, coordsOne = readFile(modelFile)
msTwo, coordsTwo = readFile(yourFile)
# coordsOne and coordsTwo are lists of coordinates of body parts, where One is ideal and Two is user input
# THESE LISTS SHOULD BE STORED AS [LWrist,RWrist,LElbow,RElbow,LShoulder,RShoulder,LHip,RHip,LKnee,RKnee,LAnkle,RAnkle]
parts = {
2: "Left Elbow",
3: "Right Elbow",
4: "Left Shoulder",
5: "Right Shoulder",
6: "Left Hip",
7: "Right Hip",
8: "Left Knee",
9: "Right Knee",
}
leniency = {
2: [10,0.00001],
3: [10,0.00001],
4: [5,0.00001],
5: [5,0.00001],
6: [1,0.00001],
7: [1,0.00001],
8: [10,0.00001],
9: [10,0.00001],
}
LENIENCE = 10
STOP_LENIENCE = 0.000001
# breaks frames into key frames, only storing important frames (like when you stop raising your hand)
keyFramesOne = []
keyFramesTwo = []
angleOne = []
angleTwo = []
# coordsOne[part][coord][x/y]
for x in parts:
part_key_frames = []
part_angle_one = []
prevAngle = None
curr_state = None
prevDelta = None
acc = None
delta = None
# for each frame, check each part for change in angle
for i in range(len(coordsOne[x])):
curr = calcAngle(coordsOne[x-2][i], coordsOne[x][i], coordsOne[x+2][i])
if prevAngle is not None:
delta = abs(curr - prevAngle)
if (acc is not None and (delta < leniency[x][1] and acc > 1) or delta > leniency[x][0]):
curr_state = curr - prevAngle > 0
if prevOpen is None or curr_state != prevOpen:
part_key_frames.append(i)
part_angle_one.append(curr - prevAngle)
if prevDelta is not None:
acc = delta - prevDelta
prevDelta = delta
prevOpen = curr_state
prevAngle = curr
if delta is not None:
prevDelta = delta
keyFramesOne.append(part_key_frames)
angleOne.append(part_angle_one)
b = 0
# for j in parts:
# # for k in range(len(keyFramesOne[b])):
# # print(parts[j] + ": " + ("open" if angleOne[b][k] > 0 else "close") + " (" + str(msOne[keyFramesOne[b][k]]) + ")")
# # b+=1
# print("")
# print("")
for x in parts:
part_key_frames = []
part_angle_two = []
prevAngle = None
curr_state = None
prevDelta = None
acc = None
delta = None
# for each frame, check each part for change in angle
for i in range(len(coordsTwo[x])):
curr = calcAngle(coordsTwo[x-2][i], coordsTwo[x][i], coordsTwo[x+2][i])
if prevAngle is not None:
delta = abs(curr - prevAngle)
if (acc is not None and (delta < leniency[x][1] and acc > 1) or delta > leniency[x][0]):
curr_state = curr - prevAngle > 0
if prevOpen is None or curr_state != prevOpen:
part_key_frames.append(i)
part_angle_two.append(curr - prevAngle)
if prevDelta is not None:
acc = delta - prevDelta
prevDelta = delta
prevOpen = curr_state
prevAngle = curr
if delta is not None:
prevDelta = delta
keyFramesTwo.append(part_key_frames)
angleTwo.append(part_angle_two)
# b = 0
# for j in parts:
# for k in range(len(keyFramesTwo[b])):
# print(parts[j] + ": " + ("open" if angleTwo[b][k] > 0 else "close") + " (" + str(msTwo[keyFramesTwo[b][k]]) + ")")
# b+=1
# Everything before is finiding key features acorss 2 videos and writing them down
for x in range(len(keyFramesOne)):
if len(keyFramesOne[x]) > 0 and len(keyFramesTwo[x]) > 0:
offset = float(msTwo[keyFramesTwo[x][0]]) - float(msOne[keyFramesOne[x][0]])
# print("x: " + str(x))
break
missed = False
b = 0
for j in parts:
strOne = None
for k in range(len(keyFramesOne[b])):
if (len(keyFramesTwo[b]) < k or keyFramesTwo[b] != keyFramesOne[b]) and strOne != None:
# print("You missed the move: " + strOne + "!")
missed = True
timeOne = float(msOne[keyFramesOne[b][k]])
timeTwo = float(float(msTwo[keyFramesOne[b][k]]) + offset)
if(b >= len(angleTwo) or k >= len(angleTwo[b])):
strOne = parts[j] + " " + ("open" if angleOne[b][k] > 0 else "close")
print("missed " + str(strOne) + " at time: " + str(timeTwo - offset))
continue
else:
strOne = parts[j] + " " + ("open" if angleOne[b][k] > 0 else "close")
strTwo = parts[j] + " " + ("open" if angleTwo[b][k] > 0 else "close")
if (strOne != strTwo):
print("You did: " + strTwo + " instead of " + strOne)
elif (timeOne - (timeTwo - offset) > 0.05):
print("You did: " + str(strTwo) + " " + str(timeOne - (timeTwo - offset)) + " ms earlier than supposed to at " + str(timeTwo - offset))
elif (timeOne - (timeTwo - offset) < -0.05):
print("You did: " + str(strTwo) + " " + str( - (timeOne - (timeTwo - offset)) ) + " ms later than supposed to at " + str(timeTwo - offset))
b+=1
if(not missed):
print("Congratulations! You've perfected the form!")
def calcAngle(pointOne, pointTwo, pointThree):
# pointOne is wrist/foot.
# pointTwo is elbow/knee
# pointThree is shoulder/hip
vectorOne = [pointOne[0] - pointTwo[0], pointOne[1] - pointTwo[1]]
vectorTwo = [pointThree[0] - pointTwo[0], pointThree[1] - pointTwo[1]]
dotProduct = vectorOne[0] * vectorTwo[0] + vectorOne[1] * vectorTwo[1]
magnitudeOne = sqrt(vectorOne[0] * vectorOne[0] + vectorOne[1] * vectorOne[1])
magnitudeTwo = sqrt(vectorTwo[0] * vectorTwo[0] + vectorTwo[1] * vectorTwo[1])
return round(degrees(acos(dotProduct/(magnitudeOne * magnitudeTwo))), 3)
def readFile(file):
parts = [[],[],[],[],[],[],[],[],[],[],[],[]]
ms = []
# Read the file line by line
with open(file, 'r') as file:
next(file)
for line in file:
split = line.strip().split(";")
# split will be list of [x, y]s
# for each part coords
ms.append(split[0])
for x in range(1, len(split)):
# each [x, y]
coords = ast.literal_eval(split[x])
parts[x-1].append(coords)
return ms, parts
judge("abhiPipelineModel.txt", "abhiPipelineYour.txt")