forked from thillai-c/AI-Fitness-trainer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
92 lines (79 loc) · 2.88 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
89
90
91
92
import cv2
import argparse
from utils import *
import mediapipe as mp
from body_part_angle import BodyPartAngle
from types_of_exercise import TypeOfExercise
ap = argparse.ArgumentParser()
# Adds an optional argument
ap.add_argument("-mt",
"--move-type",
type=str,
help="The types of move type",
required=False)
# Adds the argument (-t for exercise type) and adds the argument to the argument parser
ap.add_argument("-t",
"--action-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())
args = vars(ap.parse_args())
args = vars(ap.parse_args())
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
if args["video_source"] is not None:
cap = cv2.VideoCapture(args["video_source"])
else:
if cv2.VideoCapture(1):
cap = cv2.VideoCapture(0) # continuity cam from iphone
else:
cap = cv2.VideoCapture(0) # webcam
# setup mediapipe
with mp_pose.Pose(min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
ctx = {} # movement of exercise
action_type = args["action_type"]
move_type = args["move_type"]
while cap.isOpened():
ret, frame = cap.read()
frame = image_resize(frame)
# result_screen = np.zeros((250, 400, 3), np.uint8)
# 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
move_type_inst = determine_movement_type(move_type, landmarks)
ctx = move_type_inst.calculate_exercise(
action_type, ctx)
except:
pass
frame = score_table(move_type, action_type, frame, ctx)
# 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'):
break
cap.release()
cv2.destroyAllWindows()