forked from hooniverse/SelfFitnessManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
97 lines (74 loc) · 3.36 KB
/
test.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
import cv2
import mediapipe as mp
from exercise import pullup, pushup, squat
import method
import time
from graph import write_csv, test_graph
import matplotlib.pyplot as plt
import os
import datetime
class Test:
def __init__(self, type, goal_time, goal_number):
self.type = type
self.goal_time = goal_time
self.goal_number = goal_number
if self.type == ['Push-Up']:
self.exercise_type = pushup.Pushup()
elif self.type == ['Pull-Up']:
self.exercise_type = pullup.Pullup()
elif self.type == ['Squat']:
self.exercise_type = squat.Squat()
def run(self):
count = -1
status = True
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
cap = cv2.VideoCapture(0)
count_list = [0] * (int(self.goal_time) // 10)
time_intervals_labels = ["{}~{}".format(i*10+1, i*10+10) for i in range(int(self.goal_time) // 10) ]
with mp_pose.Pose(min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
start_time = time.time()
while cap.isOpened():
success, image = cap.read()
if not success:
continue
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
result = pose.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
pose_landmarks = result.pose_landmarks
if pose_landmarks:
count, status = self.exercise_type.countUp(count, status, pose_landmarks)
mp_drawing.draw_landmarks(
image,
result.pose_landmarks,
mp_pose.POSE_CONNECTIONS)
elapsed_time = time.time() - start_time
remaining_time = max(0, int(int(self.goal_time) - elapsed_time))
cv2.putText(image, text='count : {}/{} Remaining time : {}'
.format(count, self.goal_number, int(remaining_time))
, org=(10, 30), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1, color=(0, 0, 255), thickness=2)
cv2.imshow('image', image)
if cv2.waitKey(1) == ord('q'):
break
if count >= int(self.goal_number) and remaining_time <= 0:
write_csv.write_csv(self.exercise_type, count)
method.speak('test complete!')
break
elif count >= int(self.goal_number) and not remaining_time <= 0:
write_csv.write_csv(self.exercise_type, count)
method.speak('test complete!')
break
elif count < int(self.goal_number) and remaining_time <= 0:
write_csv.write_csv(self.exercise_type, count)
method.speak('test failed!')
break
idx = int(elapsed_time)//10
ten_second_count = count
for i in range(idx):
ten_second_count-=count_list[i]
count_list[idx] = ten_second_count
cap.release()
cv2.destroyAllWindows()
test_graph.test_graph(time_intervals_labels, count_list,self.type[0], count, self.goal_number)