-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.py
88 lines (76 loc) · 2.73 KB
/
main.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
## import packages
import cv2
import argparse
from utils import *
import mediapipe as mp
from body_part_angle import BodyPartAngle
from types_of_exercise import TypeOfExercise
## setup agrparse
ap = argparse.ArgumentParser()
ap.add_argument("-t",
"--exercise_type",
type=str,
help='Type of activity to do',
required=True)
ap.add_argument("-vs",
"--video_source",
type=str,
help='Type of activity to do',
required=False)
args = vars(ap.parse_args())
## drawing body
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
## setting the video source
if args["video_source"] is not None:
cap = cv2.VideoCapture(args["video_source"])
else:
cap = cv2.VideoCapture(0) # webcam
cap.set(3, 800) # width
cap.set(4, 480) # height
## setup mediapipe
with mp_pose.Pose(min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
counter = 0 # movement of exercise
status = True # state of move
while cap.isOpened():
try:
ret, frame = cap.read()
# result_screen = np.zeros((250, 400, 3), np.uint8)
frame = cv2.resize(frame, (800, 480), interpolation=cv2.INTER_AREA)
## recolor frame to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame.flags.writeable = False
## make detection
results = pose.process(frame)
## recolor back to BGR
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
try:
landmarks = results.pose_landmarks.landmark
counter, status = TypeOfExercise(landmarks).calculate_exercise(
args["exercise_type"], counter, status)
except:
pass
score_table(args["exercise_type"], counter, status)
## render detections (for landmarks)
mp_drawing.draw_landmarks(
frame,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(255, 255, 255),
thickness=2,
circle_radius=2),
mp_drawing.DrawingSpec(color=(174, 139, 45),
thickness=2,
circle_radius=2),
)
cv2.imshow('Video', frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
print("counter: " + str(counter))
break
except:
print("err counter: " + str(counter))
break
cap.release()
cv2.destroyAllWindows()