-
Notifications
You must be signed in to change notification settings - Fork 2
/
TouchToes.py
143 lines (111 loc) · 4.62 KB
/
TouchToes.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
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 6 09:27:09 2022
@author: Sahaj
"""
# import cv2
# import mediapipe as mp
# import numpy as np
# mp_drawing = mp.solutions.drawing_utils
# mp_pose = mp.solutions.pose
# cap = cv2.VideoCapture(0)
# with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
# while (cap.isOpened()):
# ret, image = cap.read()
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# image.flags.writeable = False
# results = pose.process(image)
# image.flags.writeable = True
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
# mp_drawing.DrawingSpec(colour=(245,117,66), thickness=3, circle_radius=4),
# mp_drawing.DrawingSpec(colour=(245,66,230), thickness=2, circle_radius=2))
# cv2.imshow("Bend over and touch your toes!!", image)
# if cv2.waitKey(10) & 0xFF == ord('q'):
# break
# cap.release()
# cv2.destroyAllWindows()
import tkinter as tk
import customtkinter as ck
import pandas as pd
import numpy as np
import pickle
import mediapipe as mp
import cv2
from PIL import Image, ImageTk
from landmarks import landmarks
window = tk.Tk()
window.geometry("500x800")
window.title("Bend over and touch your toes!!")
ck.set_appearance_mode("dark")
classLabel = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="black", padx=10)
classLabel.place(x=10, y=1)
classLabel.configure(text='STAGE')
counterLabel = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="black", padx=10)
counterLabel.place(x=160, y=1)
counterLabel.configure(text='REPS')
probLabel = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="black", padx=10)
probLabel.place(x=300, y=1)
probLabel.configure(text='PROB')
classBox = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="white", fg_color="blue")
classBox.place(x=10, y=41)
classBox.configure(text='0')
counterBox = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="white", fg_color="blue")
counterBox.place(x=160, y=41)
counterBox.configure(text='0')
probBox = ck.CTkLabel(window, height=40, width=120, text_font=("Arial", 20), text_color="white", fg_color="blue")
probBox.place(x=300, y=41)
probBox.configure(text='0')
def reset_counter():
global counter
counter = 0
button = ck.CTkButton(window, text='RESET', command=reset_counter, height=40, width=120, text_font=("Arial", 20), text_color="white", fg_color="blue")
button.place(x=10, y=600)
frame = tk.Frame(height=480, width=480)
frame.place(x=10, y=90)
lmain = tk.Label(frame)
lmain.place(x=0, y=0)
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(min_tracking_confidence=0.5, min_detection_confidence=0.5)
with open('sitNreach.pkl', 'rb') as f:
model = pickle.load(f)
cap = cv2.VideoCapture(0)
current_stage = ''
counter = 0
bodylang_prob = np.array([0,0])
bodylang_class = ''
def detect():
global current_stage
global counter
global bodylang_class
global bodylang_prob
ret, frame = cap.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(image)
mp_drawing.draw_landmarks(image , results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(106,13,173), thickness=4, circle_radius = 5),
mp_drawing.DrawingSpec(color=(255,102,0), thickness=5, circle_radius = 10))
try:
row = np.array([[res.x, res.y, res.z, res.visibility] for res in results.pose_landmarks.landmark]).flatten().tolist()
X = pd.DataFrame([row], columns = landmarks)
bodylang_prob = model.predict_proba(X)[0]
bodylang_class = model.predict(X)[0]
if bodylang_class =="down" and bodylang_prob[bodylang_prob.argmax()] > 0.7:
current_stage = "down"
elif current_stage == "down" and bodylang_class == "up" and bodylang_prob[bodylang_prob.argmax()] > 0.7:
current_stage = "up"
counter += 1
except Exception as e:
print(e)
img = image[:, :460, :]
imgarr = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(imgarr)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, detect)
counterBox.configure(text=counter)
probBox.configure(text=bodylang_prob[bodylang_prob.argmax()])
classBox.configure(text=current_stage)
detect()
window.mainloop()