-
Notifications
You must be signed in to change notification settings - Fork 0
/
apphip.py
111 lines (94 loc) · 3.87 KB
/
apphip.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
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("Gym Rep")
ck.set_appearance_mode("dark")
classLabel = ck.CTkLabel(window, height= 40 , width= 120 , 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 , 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 , 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 , 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 , 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 , 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
counterBox.configure(text=counter)
button = ck.CTkLabel(window, text="RESET", height= 40 , width= 120 , font = ("Arial" , 20) ,text_color="black",fg_color="blue")
button.bind(command= reset_counter)
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 ('hip.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=(0,0,255), thickness=2, circle_radius = 2),
mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius = 2))
try:
# pass
print(current_stage)
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]
# bodylang_prob = np.exp(bodylang_class) / np.sum(np.exp(bodylang_class))
if bodylang_class == "neutral" : # and bodylang_prob[bodylang_prob.argmax()] > 0.5:
current_stage = "neutral"
elif bodylang_class =="narrow": # and bodylang_prob[bodylang_prob.argmax()] > 0.5:
current_stage = "narrow"
elif bodylang_class == "wide" :# and bodylang_prob[bodylang_prob.argmax()] > 0.5:
current_stage = "wide"
# counter += 1
except Exception as e:
print(e)
print("Hello")
type(bodylang_prob)
# pass
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()