forked from dippodahippo/Asana-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyogapose.py
141 lines (124 loc) · 4.74 KB
/
yogapose.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
import mediapipe as mp
import cv2
import numpy as np
import tensorflow as tf
import pyduinointegr.pyduino_connection as pyd
def main():
pyd.show_ports()
port = pyd.select_port(6)
pyd.open_port(port)
model = tf.keras.models.load_model("yoga_pose_model")
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
pose = mp_pose.Pose(min_detection_confidence=0.5)
smoothing_factor = 0.2
prev_prediction = np.zeros(21)
def getpose(image):
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pose.process(image_rgb)
if results.pose_landmarks is not None:
keypoints = np.zeros((len(results.pose_landmarks.landmark), 2))
for i, landmark in enumerate(results.pose_landmarks.landmark):
keypoints[i] = [landmark.x, landmark.y]
return keypoints, results.pose_landmarks
else:
return None, None
def predpose(keypoints):
keypoints = keypoints / 640
prediction = model.predict(np.expand_dims(keypoints, axis=0))[0]
return prediction
def all_keypoints_visible(keypoints):
return keypoints is not None and len(keypoints) == 33
def smooth_predictions(current_prediction):
global prev_prediction
smoothed_prediction = smoothing_factor * current_prediction + (1 - smoothing_factor) * prev_prediction
prev_prediction = smoothed_prediction
return smoothed_prediction
def get_custom_label(index):
if index == 0:
return "Anjaneyasana"
elif index == 1:
return "Adho Mukha Svasana"
elif index == 2:
return "ardha chakrasana"
elif index == 3:
return "bhujangasana"
elif index == 4:
return "chakrasana"
elif index == 5:
return "Dhanurasana"
elif index == 6:
return "malasana"
elif index == 7:
return "Naukasana"
elif index == 8:
return "paschimottasana"
elif index == 9:
return "shavasana"
elif index == 10:
return "Setu Bandha Sarvagasana"
elif index == 11:
return "tadasana"
elif index == 12:
return "trikonasana"
elif index == 13:
return "uttanasana"
elif index == 14:
return "ustrasana"
elif index == 15:
return "Utkatasana"
elif index == 16:
return "vajrasana"
elif index == 17:
return "Virabhadrasan 1"
elif index == 18:
return "Virabhadrasan 2"
elif index == 19:
return "Virabhadrasan 3"
elif index == 20:
return "vrikshasana"
else:
return "Unknown Label"
def detectpose(image):
keypoints, landmarks = getpose(image)
if keypoints is None:
error_text = "Error: Person out of frame"
cv2.putText(image, error_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
return image
image_height, image_width, _ = image.shape
for landmark in landmarks.landmark:
x = int(landmark.x * image_width)
y = int(landmark.y * image_height)
cv2.circle(image, (x, y), 5, (255, 0, 0), -1)
prediction = predpose(keypoints)
smoothed_prediction = smooth_predictions(prediction)
max_confidence = np.max(smoothed_prediction)
if max_confidence > 0.95:
pyd.send_data(1)
else:
pyd.send_data(0)
selected_indices = [3,6,11,12,13,17,20]
line_spacing = 30
for i, confidence in enumerate(smoothed_prediction):
if i in selected_indices:
label = get_custom_label(i)
accuracy = (confidence / max_confidence) * 100
accuracy_text = f"{label}: {round(accuracy, 2)}%"
y_coordinate = 30 + selected_indices.index(i) * line_spacing
cv2.putText(image, accuracy_text, (10, y_coordinate), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
return image
def main():
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
processed_frame = detectpose(frame)
cv2.imshow('Yoga Pose Detection', processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pyd.close_port()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()