-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
101 lines (83 loc) · 3.5 KB
/
app.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
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("480x700")
window.title("Exercise_Tracker")
ck.set_appearance_mode("dark")
classLabel = ck.CTkLabel(window, height=40, width=120, text_font=("Times New Roman", 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=("Times New Roman", 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=("Times New Roman", 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=("Times New Roman", 20), text_color="white", fg_color="purple")
classBox.place(x=10, y=41)
classBox.configure(text='0')
counterBox = ck.CTkLabel(window, height=40, width=120, text_font=("Times New Roman", 20), text_color="white", fg_color="purple")
counterBox.place(x=160, y=41)
counterBox.configure(text='0')
probBox = ck.CTkLabel(window, height=40, width=120, text_font=("Times New Roman", 20), text_color="white", fg_color="purple")
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=("Times New Roman", 20), text_color="white", fg_color="Purple")
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=(50,130,17), thickness=3, circle_radius = 3),
mp_drawing.DrawingSpec(color=(255,130,0), thickness=4, circle_radius = 8))
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)
window.mainloop()