randomshit11 commited on
Commit
bfba840
·
verified ·
1 Parent(s): afc29d2

Upload 2 files

Browse files
Files changed (2) hide show
  1. ai_gym.py +150 -0
  2. test.py +71 -0
ai_gym.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultralytics YOLO 🚀, AGPL-3.0 license
2
+
3
+ import cv2
4
+
5
+ from ultralytics.utils.checks import check_imshow
6
+ from ultralytics.utils.plotting import Annotator
7
+
8
+
9
+ class AIGym:
10
+ """A class to manage the gym steps of people in a real-time video stream based on their poses."""
11
+
12
+ def __init__(self):
13
+ """Initializes the AIGym with default values for Visual and Image parameters."""
14
+
15
+ # Image and line thickness
16
+ self.im0 = None
17
+ self.tf = None
18
+
19
+ # Keypoints and count information
20
+ self.keypoints = None
21
+ self.poseup_angle = None
22
+ self.posedown_angle = None
23
+ self.threshold = 0.001
24
+
25
+ # Store stage, count and angle information
26
+ self.angle = None
27
+ self.count = None
28
+ self.stage = None
29
+ self.pose_type = "pushup"
30
+ self.kpts_to_check = None
31
+
32
+ # Visual Information
33
+ self.view_img = False
34
+ self.annotator = None
35
+
36
+ # Check if environment support imshow
37
+ self.env_check = check_imshow(warn=True)
38
+
39
+ def set_args(
40
+ self,
41
+ kpts_to_check,
42
+ line_thickness=2,
43
+ view_img=False,
44
+ pose_up_angle=145.0,
45
+ pose_down_angle=90.0,
46
+ pose_type="pullup",
47
+ ):
48
+ """
49
+ Configures the AIGym line_thickness, save image and view image parameters.
50
+
51
+ Args:
52
+ kpts_to_check (list): 3 keypoints for counting
53
+ line_thickness (int): Line thickness for bounding boxes.
54
+ view_img (bool): display the im0
55
+ pose_up_angle (float): Angle to set pose position up
56
+ pose_down_angle (float): Angle to set pose position down
57
+ pose_type (str): "pushup", "pullup" or "abworkout"
58
+ """
59
+ self.kpts_to_check = kpts_to_check
60
+ self.tf = line_thickness
61
+ self.view_img = view_img
62
+ self.poseup_angle = pose_up_angle
63
+ self.posedown_angle = pose_down_angle
64
+ self.pose_type = pose_type
65
+
66
+ def start_counting(self, im0, results, frame_count):
67
+ """
68
+ Function used to count the gym steps.
69
+
70
+ Args:
71
+ im0 (ndarray): Current frame from the video stream.
72
+ results (list): Pose estimation data
73
+ frame_count (int): store current frame count
74
+ """
75
+ self.im0 = im0
76
+ if frame_count == 1:
77
+ self.count = [0] * len(results[0])
78
+ self.angle = [0] * len(results[0])
79
+ self.stage = ["-" for _ in results[0]]
80
+ self.keypoints = results[0].keypoints.data
81
+ self.annotator = Annotator(im0, line_width=2)
82
+
83
+ for ind, k in enumerate(reversed(self.keypoints)):
84
+ if self.pose_type in {"pushup", "pullup"}:
85
+ self.angle[ind] = self.annotator.estimate_pose_angle(
86
+ k[int(self.kpts_to_check[0])].cpu(),
87
+ k[int(self.kpts_to_check[1])].cpu(),
88
+ k[int(self.kpts_to_check[2])].cpu(),
89
+ )
90
+ self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)
91
+
92
+ if self.pose_type == "abworkout":
93
+ self.angle[ind] = self.annotator.estimate_pose_angle(
94
+ k[int(self.kpts_to_check[0])].cpu(),
95
+ k[int(self.kpts_to_check[1])].cpu(),
96
+ k[int(self.kpts_to_check[2])].cpu(),
97
+ )
98
+ self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)
99
+ if self.angle[ind] > self.poseup_angle:
100
+ self.stage[ind] = "down"
101
+ if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
102
+ self.stage[ind] = "up"
103
+ self.count[ind] += 1
104
+ self.annotator.plot_angle_and_count_and_stage(
105
+ angle_text=self.angle[ind],
106
+ count_text=self.count[ind],
107
+ stage_text=self.stage[ind],
108
+ center_kpt=k[int(self.kpts_to_check[1])],
109
+ line_thickness=self.tf,
110
+ )
111
+
112
+ if self.pose_type == "pushup":
113
+ if self.angle[ind] > self.poseup_angle:
114
+ self.stage[ind] = "up"
115
+ if self.angle[ind] < self.posedown_angle and self.stage[ind] == "up":
116
+ self.stage[ind] = "down"
117
+ self.count[ind] += 1
118
+ self.annotator.plot_angle_and_count_and_stage(
119
+ angle_text=self.angle[ind],
120
+ count_text=self.count[ind],
121
+ stage_text=self.stage[ind],
122
+ center_kpt=k[int(self.kpts_to_check[1])],
123
+ line_thickness=self.tf,
124
+ )
125
+ if self.pose_type == "pullup":
126
+ if self.angle[ind] > self.poseup_angle:
127
+ self.stage[ind] = "down"
128
+ if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
129
+ self.stage[ind] = "up"
130
+ self.count[ind] += 1
131
+ self.annotator.plot_angle_and_count_and_stage(
132
+ angle_text=self.angle[ind],
133
+ count_text=self.count[ind],
134
+ stage_text=self.stage[ind],
135
+ center_kpt=k[int(self.kpts_to_check[1])],
136
+ line_thickness=self.tf,
137
+ )
138
+
139
+ self.annotator.kpts(k, shape=(640, 640), radius=1, kpt_line=True)
140
+
141
+ if self.env_check and self.view_img:
142
+ cv2.imshow("Ultralytics YOLOv8 AI GYM", self.im0)
143
+ if cv2.waitKey(1) & 0xFF == ord("q"):
144
+ return
145
+
146
+ return self.im0
147
+
148
+
149
+ if __name__ == "__main__":
150
+ AIGym()
test.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from ultralytics import YOLO
2
+ # import ai_gym
3
+ # import cv2
4
+
5
+ # model = YOLO("yolov8n-pose.pt")
6
+ # cap = cv2.VideoCapture("pullups.mp4")
7
+ # assert cap.isOpened(), "Error reading video file"
8
+ # w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
9
+
10
+ # video_writer = cv2.VideoWriter("workouts.avi",
11
+ # cv2.VideoWriter_fourcc(*'mp4v'),
12
+ # fps,
13
+ # (w, h))
14
+
15
+ # gym_object = ai_gym.AIGym() # init AI GYM module
16
+ # gym_object.set_args(line_thickness=2,
17
+ # view_img=True,
18
+ # pose_type="pullup",
19
+ # kpts_to_check=[6, 8, 10])
20
+
21
+ # frame_count = 0
22
+ # while cap.isOpened():
23
+ # success, im0 = cap.read()
24
+ # if not success:
25
+ # print("Video frame is empty or video processing has been successfully completed.")
26
+ # break
27
+ # frame_count += 1
28
+ # results = model.track(im0, verbose=False) # Tracking recommended
29
+ # #results = model.predict(im0) # Prediction also supported
30
+ # im0 = gym_object.start_counting(im0, results, frame_count)
31
+ # video_writer.write(im0)
32
+
33
+ # cv2.destroyAllWindows()
34
+ # video_writer.release()
35
+
36
+
37
+ from ultralytics import YOLO
38
+ from ultralytics.solutions import ai_gym
39
+ import cv2
40
+
41
+ model = YOLO("yolov8n-pose.pt")
42
+ cap = cv2.VideoCapture("pullups.mp4")
43
+ assert cap.isOpened(), "Error reading video file"
44
+ w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
45
+
46
+ video_writer = cv2.VideoWriter("output_video.avi",
47
+ cv2.VideoWriter_fourcc(*'mp4v'),
48
+ fps,
49
+ (w, h))
50
+
51
+ gym_object = ai_gym.AIGym() # init AI GYM module
52
+ gym_object.set_args(line_thickness=2,
53
+ view_img=False, # Set view_img to False to prevent displaying the video in real-time
54
+ pose_type="pushup",
55
+ kpts_to_check=[6, 8, 10])
56
+
57
+ frame_count = 0
58
+ while cap.isOpened():
59
+ success, im0 = cap.read()
60
+ if not success:
61
+ print("Video frame is empty or video processing has been successfully completed.")
62
+ break
63
+ frame_count += 1
64
+ results = model.track(im0, verbose=False) # Tracking recommended
65
+ #results = model.predict(im0) # Prediction also supported
66
+ im0 = gym_object.start_counting(im0, results, frame_count)
67
+ video_writer.write(im0)
68
+
69
+ cap.release()
70
+ video_writer.release()
71
+ cv2.destroyAllWindows()