-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoga-detection.py
222 lines (189 loc) · 6.28 KB
/
yoga-detection.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
import cv2
from ultralytics import YOLO
import cvzone
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
yoga_poses = [
"adho mukha svanasana",
"adho mukha vriksasana",
"agnistambhasana",
"ananda balasana",
"anantasana",
"anjaneyasana",
"ardha bhekasana",
"ardha chandrasana",
"ardha matsyendrasana",
"ardha pincha mayurasana",
"ardha uttanasana",
"ashtanga namaskara",
"astavakrasana",
"baddha konasana",
"bakasana",
"balasana",
"bhairavasana",
"bharadvajasana i",
"bhekasana",
"bhujangasana",
"bhujapidasana",
"bitilasana",
"camatkarasana",
"chakravakasana",
"chaturanga dandasana",
"dandasana",
"dhanurasana",
"durvasasana",
"dwi pada viparita dandasana",
"eka pada koundinyanasana i",
"eka pada koundinyanasana ii",
"eka pada rajakapotasana",
"eka pada rajakapotasana ii",
"ganda bherundasana",
"garbha pindasana",
"garudasana",
"gomukhasana",
"halasana",
"hanumanasana",
"janu sirsasana",
"kapotasana",
"krounchasana",
"kurmasana",
"lolasana",
"makara adho mukha svanasana",
"makarasana",
"malasana",
"marichyasana i",
"marichyasana iii",
"marjaryasana",
"matsyasana",
"mayurasana",
"natarajasana",
"padangusthasana",
"padmasana",
"parighasana",
"paripurna navasana",
"parivrtta janu sirsasana",
"parivrtta parsvakonasana",
"parivrtta trikonasana",
"parsva bakasana",
"parsvottanasana",
"pasasana",
"paschimottanasana",
"phalakasana",
"pincha mayurasana",
"prasarita padottanasana",
"purvottanasana",
"salabhasana",
"salamba bhujangasana",
"salamba sarvangasana",
"salamba sirsasana",
"savasana",
"setu bandha sarvangasana",
"simhasana",
"sukhasana",
"supta baddha konasana",
"supta matsyendrasana",
"supta padangusthasana",
"supta virasana",
"tadasana",
"tittibhasana",
"tolasana",
"tulasana",
"upavistha konasana",
"urdhva dhanurasana",
"urdhva hastasana",
"urdhva mukha svanasana",
"urdhva prasarita eka padasana",
"ustrasana",
"utkatasana",
"uttana shishosana",
"uttanasana",
"utthita ashwa sanchalanasana",
"utthita hasta padangustasana",
"utthita parsvakonasana",
"utthita trikonasana",
"vajrasana",
"vasisthasana",
"viparita karani",
"virabhadrasana i",
"virabhadrasana ii",
"virabhadrasana iii",
"virasana",
"vriksasana",
"vrischikasana",
"yoganidrasana"
]
classNames = [
"person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup","fork",
"knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli","carrot", "hot dog",
"pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed","diningtable", "toilet",
"tvmonitor", "laptop", "mouse", "remote", "keyboard", "mobile phone","microwave","oven","toaster",
"sink", "refrigerator", "book", "clock", "vase", "scissors","teddy bear", "hair drier", "toothbrush"
]
def imgProcess(image):
target_size=(64, 64)
img = cv2.resize(image, target_size)
img = img / 255.0
img = np.expand_dims(img, axis=0)
output=predict(img)
return output
def predict(test_inp):
yoga_model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, kernel_size=(3, 3), padding='valid', activation='relu', input_shape=(64, 64, 3)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='valid'),
tf.keras.layers.Conv2D(32, kernel_size=(3, 3), padding='valid', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='valid'),
tf.keras.layers.Conv2D(32, kernel_size=(3, 3), padding='valid', activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='valid'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(107, activation='softmax')
])
yoga_model.load_weights('yoga-model.h5')
prediction=yoga_model.predict(test_inp)
predicted_class_index = np.argmax(prediction)
return predicted_class_index
def yogaPoseDetect():
model=YOLO('yolov8n.pt')
cap=cv2.VideoCapture(0)
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# fps = cap.get(cv2.CAP_PROP_FPS)
# out = cv2.VideoWriter('sample/demo.avi', fourcc, fps, (1280, 720))
cap.set(3,1280)
cap.set(4,720)
while True:
success, img=cap.read()
results=model(img,stream=True)
if not success:
break
for r in results:
boxes=r.boxes
for box in boxes:
x1,y1,x2,y2=box.xyxy[0]
x1,y1,x2,y2=int(x1),int(y1),int(x2),int(y2)
conf=round(float(box.conf[0]),2)
id=int(box.cls[0])
class_name = classNames[id]
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
if class_name == "person":
cropped_img = img[y1:y2, x1:x2]
predicted_pose = imgProcess(cropped_img)
cvzone.putTextRect(img,f'{yoga_poses[predicted_pose]}',(max(0,x1),max(40,y1)))
# out.write(img)
cv2.imshow("Cam footage. Press 'Q' to exit.",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
# out.release()
cv2.destroyAllWindows()
yogaPoseDetect()