forked from thillai-c/AI-Fitness-trainer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
157 lines (123 loc) · 4.84 KB
/
utils.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
import mediapipe as mp
import pandas as pd
import numpy as np
import cv2
from types_of_exercise import TypeOfExercise
from types_of_moves import TypeOfMove
mp_pose = mp.solutions.pose
accuracy_him = 0
accuracy_goat = 1
accuracy_legend = 2
accuracy_allstar = 3
accuracy_league = 4
enable_angles_view = False
def calculate_angle(a, b, c):
a = np.array(a)
b = np.array(b)
c = np.array(c)
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
def detection_body_part(landmarks, body_part_name):
val = mp_pose.PoseLandmark[body_part_name].value
if not landmarks[val]:
return [0, 0, False]
return [
landmarks[val].x,
landmarks[val].y,
landmarks[val].visibility
]
def detection_body_parts(landmarks):
body_parts = pd.DataFrame(columns=["body_part", "x", "y"])
for i, lndmrk in enumerate(mp_pose.PoseLandmark):
lndmrk = str(lndmrk).split(".")[1]
cord = detection_body_part(landmarks, lndmrk)
body_parts.loc[i] = lndmrk, cord[0], cord[1]
return body_parts
def determine_accuracy_name(index):
if index == accuracy_allstar:
return "Allstar"
if index == accuracy_legend:
return "Legend"
if index == accuracy_league:
return "League"
if index == accuracy_goat:
return "Goat"
if index == accuracy_him:
return "Him"
# Just get good
return "Bum"
def score_table(move_type, action_type, frame, context):
height = 65
cv2.putText(frame, "Move Type : " + move_type.replace("-", " "),
(10, height), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2,
cv2.LINE_AA)
height += 35
cv2.putText(frame, "Exercise Type : " + action_type.replace("-", " "),
(10, height), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2,
cv2.LINE_AA)
if "counter" in context:
height += 35
counter = context['counter']
cv2.putText(frame, "Counter : " + str(counter), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
if "status" in context:
height += 35
status = context['status']
cv2.putText(frame, "Status : " + str(status), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
if "accuracy" in context:
height += 35
status = context['accuracy']
cv2.putText(frame, "Accuracy : " + str(status), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
if "accuracy_rating" in context:
height += 35
accuracy_num = determine_accuracy_name(context['accuracy_rating'])
cv2.putText(frame, "Accuracy Status: " + str(accuracy_num), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
if "r_area_arms" in context and enable_angles_view:
height += 35
r_area_arms_angle = determine_accuracy_name(context["r_area_arms"])
cv2.putText(frame, "Right Area Arms Angle: " + str(r_area_arms_angle), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
if "r_area_side" in context and enable_angles_view:
height += 35
r_area_side = determine_accuracy_name(context["r_area_side"])
cv2.putText(frame, "Right Area Sides Angle: " + str(r_area_side), (10, height),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
return frame
def determine_movement_type(argument: str, landmarks):
if argument == 'basketball-move':
return TypeOfMove(landmarks)
return TypeOfExercise(landmarks)
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
if image is not None:
(h, w) = image.shape[:2]
else:
(h, w) = (1920, 1080)
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation=inter)
# return the resized image
return resized