Skip to content

Commit a94c9a3

Browse files
Initial commit
0 parents  commit a94c9a3

33 files changed

+1243
-0
lines changed
5.6 MB
Binary file not shown.
2.29 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.

1. Steps Tracker/steps_tracker.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import cv2 as cv
2+
import numpy as np
3+
from utils import draw_text_with_bg
4+
from Pose_estimationModule import PoseDetector
5+
6+
# Initializing pose detector and video capture
7+
detector = PoseDetector()
8+
cap = cv.VideoCapture("VIDEOS/INPUTS/running.mp4")
9+
10+
# Getting video properties (width, height, FPS) and setting up the output file for saving
11+
w, h, fps = (int(cap.get(x)) for x in (cv.CAP_PROP_FRAME_WIDTH, cv.CAP_PROP_FRAME_HEIGHT, cv.CAP_PROP_FPS))
12+
filename = "VIDEOS/OUTPUTS/tracking_steps.mp4"
13+
out = cv.VideoWriter(filename, cv.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
14+
15+
if not cap.isOpened():
16+
print("Error: couldn't open the video!")
17+
18+
# Initializing state variables for counting steps and detecting stage (up/down)
19+
count = 0
20+
stage = None # Can be 'right_up' or 'left_up'
21+
color = (0, 0, 255) # Defaulting to red color for visualization
22+
23+
def detect_and_count_steps(right_wrist, left_wrist, stage, count):
24+
"""
25+
Detecting and counting steps based on wrist movements.
26+
Args:
27+
right_wrist: Coordinates of the right wrist (x, y).
28+
left_wrist: Coordinates of the left wrist (x, y).
29+
stage: Current movement stage ('right_up' or 'left_up').
30+
count: Current step count.
31+
Returns:
32+
Updated stage and count.
33+
"""
34+
if right_wrist[1] < left_wrist[1] and stage != 'right_up': # Right wrist goes above left
35+
stage = 'right_up'
36+
count += 1 # Counting the step
37+
elif left_wrist[1] < right_wrist[1] and stage != 'left_up': # Left wrist goes above right
38+
stage = 'left_up'
39+
count += 1 # Counting the step
40+
41+
return stage, count
42+
43+
# Main loop for processing the video frames
44+
while cap.isOpened():
45+
ret, frame = cap.read()
46+
if not ret:
47+
break
48+
49+
# Detecting pose and extracting landmarks
50+
frame = detector.find_pose(frame, draw=False)
51+
landmarks = detector.get_positions(frame)
52+
if 15 in landmarks and 16 in landmarks:
53+
right_wrist = landmarks[16]
54+
left_wrist = landmarks[15]
55+
56+
# Detecting and counting steps based on wrist movements
57+
stage, count = detect_and_count_steps(right_wrist, left_wrist, stage, count)
58+
59+
# Displaying the step count on the frame
60+
draw_text_with_bg(frame, f"Steps: {count}", (0, 53), font_scale=2, thickness=4)
61+
62+
# Saving the processed frame to the output video
63+
out.write(frame)
64+
65+
# Resizing frame for displaying
66+
resizing_factor = 0.35
67+
resized_shape = (int(resizing_factor * frame.shape[1]), int(resizing_factor * frame.shape[0]))
68+
resized_frame = cv.resize(frame, resized_shape)
69+
70+
# Displaying the current frame
71+
cv.imshow("Video", resized_frame)
72+
73+
# Exiting if the user presses 'p'
74+
if cv.waitKey(1) & 0xff == ord('p'):
75+
break
76+
77+
# Releasing video capture and writer, closing display windows
78+
cap.release()
79+
out.release()
80+
cv.destroyAllWindows()

1. Steps Tracker/steps_tracker_2.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import cv2 as cv
2+
import numpy as np
3+
from utils import draw_text_with_bg
4+
from Pose_estimationModule import PoseDetector
5+
6+
# Initializing pose detector and video capture
7+
detector = PoseDetector()
8+
cap = cv.VideoCapture("VIDEOS/INPUTS/running_2.mp4")
9+
10+
# Getting video properties (width, height, FPS) and setting up the output file for saving
11+
w, h, fps = (int(cap.get(x)) for x in (cv.CAP_PROP_FRAME_WIDTH, cv.CAP_PROP_FRAME_HEIGHT, cv.CAP_PROP_FPS))
12+
filename = "VIDEOS/OUTPUTS/steps_tracker.mp4"
13+
out = cv.VideoWriter(filename, cv.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
14+
15+
if not cap.isOpened():
16+
print("Error: couldn't open the video!")
17+
18+
# Initializing state variables for counting steps and detecting stage (up/down)
19+
count = 0
20+
stage = None # Can be 'right_up' or 'left_up'
21+
color = (0, 0, 255) # Defaulting to red color for visualization
22+
23+
def detect_and_count_steps(right_ankle, left_ankle, stage, count):
24+
"""
25+
Detecting and counting steps based on ankle movements.
26+
Args:
27+
right_ankle: Coordinates of the right ankle (x, y).
28+
left_ankle: Coordinates of the left ankle (x, y).
29+
stage: Current movement stage ('right_up' or 'left_up').
30+
count: Current step count.
31+
Returns:
32+
Updated stage and count.
33+
"""
34+
if right_ankle[1] < left_ankle[1] and stage != 'right_up': # Right ankle goes above left
35+
stage = 'right_up'
36+
count += 1 # Counting the step
37+
elif left_ankle[1] < right_ankle[1] and stage != 'left_up': # Left ankle goes above right
38+
stage = 'left_up'
39+
count += 1 # Counting the step
40+
41+
return stage, count
42+
43+
# Main loop for processing the video frames
44+
while cap.isOpened():
45+
ret, frame = cap.read()
46+
if not ret:
47+
break
48+
49+
# Detecting pose and extracting landmarks
50+
frame = detector.find_pose(frame, draw = False)
51+
landmarks = detector.get_positions(frame)
52+
if 27 in landmarks and 28 in landmarks:
53+
right_ankle = landmarks[28] # Assuming landmark 28 is the right ankle
54+
left_ankle = landmarks[27] # Assuming landmark 27 is the left ankle
55+
56+
# Detecting and counting steps based on ankle movements
57+
stage, count = detect_and_count_steps(right_ankle, left_ankle, stage, count)
58+
59+
# Displaying the step count on the frame
60+
draw_text_with_bg(frame, f"Steps: {count}", (0, 40), font_scale=1, thickness=2)
61+
62+
# Saving the processed frame to the output video
63+
out.write(frame)
64+
65+
# Resizing frame for displaying
66+
resizing_factor = 1
67+
resized_shape = (int(resizing_factor * frame.shape[1]), int(resizing_factor * frame.shape[0]))
68+
resized_frame = cv.resize(frame, resized_shape)
69+
70+
# Displaying the current frame
71+
cv.imshow("Video", resized_frame)
72+
73+
# Exiting if the user presses 'p'
74+
if cv.waitKey(1) & 0xff == ord('p'):
75+
break
76+
77+
# Releasing video capture and writer, closing display windows
78+
cap.release()
79+
out.release()
80+
cv.destroyAllWindows()
3.06 MB
Binary file not shown.
Binary file not shown.

2. Jumping Jacks/jumping_jacks.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import cv2 as cv
2+
import numpy as np
3+
from utils import draw_text_with_bg
4+
from Pose_estimationModule import PoseDetector
5+
6+
# Initializing the pose detector
7+
detector = PoseDetector()
8+
cap = cv.VideoCapture("VIDEOS/INPUTS/jumping_jacks.mp4")
9+
w, h, fps = (int(cap.get(x)) for x in (cv.CAP_PROP_FRAME_WIDTH, cv.CAP_PROP_FRAME_HEIGHT, cv.CAP_PROP_FPS))
10+
filename = "VIDEOS/OUTPUTS/jumping_jacks_count.mp4"
11+
out = cv.VideoWriter(filename, cv.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
12+
13+
if not cap.isOpened():
14+
print("Error: couldn't open the video!")
15+
16+
# Initializing state variables for both hands
17+
count = 0
18+
stage = None # 'up' or 'down'
19+
color = (0, 0, 255) # Red for down, green for up
20+
21+
# Function to calculate the angle formed by three points
22+
def calculate_angle(a, b, c):
23+
"""
24+
Calculating the angle formed by three points.
25+
26+
Args:
27+
a: First point (e.g., shoulder) [x, y].
28+
b: Middle point (e.g., elbow) [x, y].
29+
c: Third point (e.g., wrist) [x, y].
30+
31+
Returns:
32+
angle: Angle in degrees between the vectors ab and bc.
33+
"""
34+
a = np.array(a)
35+
b = np.array(b)
36+
c = np.array(c)
37+
38+
ab = a - b # Vector from shoulder to elbow
39+
bc = c - b # Vector from elbow to wrist
40+
41+
cosine_angle = np.dot(ab, bc) / (np.linalg.norm(ab) * np.linalg.norm(bc))
42+
cosine_angle = np.clip(cosine_angle, -1.0, 1.0) # Clipping to avoid domain error in arccos
43+
angle = np.degrees(np.arccos(cosine_angle))
44+
45+
return angle
46+
47+
# Function for counting and setting color based on angle
48+
def update_count_and_color_by_angle(angle, stage, count):
49+
"""
50+
Updating the count and color based on the elbow angle.
51+
52+
Args:
53+
angle: The current elbow angle in degrees.
54+
stage: The current stage of the arm ('up' or 'down').
55+
count: The current count of repetitions.
56+
57+
Returns:
58+
stage: Updated stage ('up' or 'down').
59+
count: Updated repetition count.
60+
color: Updated color (green for 'up', red for 'down').
61+
"""
62+
if angle < 50: # "down" position
63+
stage = "down"
64+
color = (0, 0, 255) # Setting red color for down position
65+
elif angle > 100 and stage == "down": # "up" position and counting repetition
66+
count += 1
67+
stage = "up"
68+
color = (0, 255, 0) # Setting green color for up position
69+
else:
70+
# Keeping the current color if no state change
71+
color = (0, 255, 0) if stage == "up" else (0, 0, 255)
72+
73+
return stage, count, color
74+
75+
# Main loop for processing video frames
76+
while cap.isOpened():
77+
ret, frame = cap.read()
78+
if not ret:
79+
break
80+
81+
# Detecting pose and getting landmark positions
82+
frame = detector.find_pose(frame, draw=False)
83+
landmarks = detector.get_positions(frame)
84+
85+
# Getting left and right elbow/wrist/shoulder landmarks
86+
left_shoulder = landmarks[11]
87+
left_hip = landmarks[23]
88+
left_elbow = landmarks[13]
89+
90+
right_shoulder = landmarks[12]
91+
right_hip = landmarks[24]
92+
right_elbow = landmarks[14]
93+
94+
# Calculating the angle for both arms
95+
angle_left = calculate_angle(left_hip, left_shoulder, left_elbow)
96+
angle_right = calculate_angle(right_hip, right_shoulder, right_elbow)
97+
98+
# Updating right hand count and color based on angle
99+
stage, count, color = update_count_and_color_by_angle(angle_left, stage, count)
100+
101+
# Displaying the count and angle for both hands on the frame
102+
draw_text_with_bg(frame, f"Count: {count}", (0, 50), font_scale=1.5, thickness=2, bg_color=color)
103+
104+
# Drawing lines between shoulder, hip, and elbow
105+
cv.line(frame, left_shoulder, left_hip, (255, 255, 255), 2)
106+
cv.line(frame, left_shoulder, left_elbow, (255, 255, 255), 2)
107+
cv.line(frame, right_shoulder, right_hip, (255, 255, 255), 2)
108+
cv.line(frame, right_shoulder, right_elbow, (255, 255, 255), 2)
109+
110+
# Displaying the angles on the frame
111+
draw_text_with_bg(frame, f"{int(angle_left)} degrees", left_shoulder, font_scale=0.8, thickness=2)
112+
draw_text_with_bg(frame, f"{int(angle_left)} degrees", (right_shoulder[0] - 100, right_shoulder[1]), font_scale=0.8, thickness=2)
113+
114+
# Writing the frame to the output video
115+
out.write(frame)
116+
117+
# Resizing the frame for display
118+
resizing_factor = 0.45
119+
resized_shape = (int(resizing_factor * frame.shape[1]), int(resizing_factor * frame.shape[0]))
120+
resized_frame = cv.resize(frame, resized_shape)
121+
122+
# Displaying the video
123+
cv.imshow("Video", resized_frame)
124+
125+
# Breaking the loop on 'p' key press
126+
if cv.waitKey(1) & 0xff == ord('p'):
127+
break
128+
129+
# Releasing video resources
130+
cap.release()
131+
out.release()
132+
cv.destroyAllWindows()
Binary file not shown.
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cv2 as cv
2+
from utils import draw_text_with_bg
3+
from Pose_estimationModule import PoseDetector
4+
5+
# Initializing the pose detector
6+
detector = PoseDetector()
7+
cap = cv.VideoCapture("VIDEOS/INPUTS/bench_press.mp4")
8+
w, h, fps = (int(cap.get(x)) for x in (cv.CAP_PROP_FRAME_WIDTH, cv.CAP_PROP_FRAME_HEIGHT, cv.CAP_PROP_FPS))
9+
filename = "VIDEOS/OUTPUTS/bench_press_counter.mp4"
10+
out = cv.VideoWriter(filename, cv.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
11+
12+
if not cap.isOpened():
13+
print("Error: couldn't open the video!")
14+
15+
# Initializing count and state variables
16+
count = 0
17+
stage = None
18+
color = (0, 0, 255) # Red for 'down' state
19+
20+
# Function to check and update the state based on the elbow position
21+
def update_count_and_color(elbow, shoulder, stage, count):
22+
"""
23+
Updating the count and color based on the elbow and shoulder positions.
24+
25+
Args:
26+
elbow: Coordinates of the elbow [x, y].
27+
shoulder: Coordinates of the shoulder [x, y].
28+
stage: Current stage ('up' or 'down').
29+
count: Current count of repetitions.
30+
31+
Returns:
32+
stage: Updated stage.
33+
count: Updated count.
34+
color: Updated color (red for 'down', green for 'up').
35+
"""
36+
if elbow[1] > shoulder[1]: # Elbow below shoulder (down position)
37+
stage = 'down'
38+
color = (0, 0, 255) # Red color
39+
elif elbow[1] < shoulder[1] and stage == 'down': # Elbow above shoulder (up position)
40+
count += 1
41+
stage = 'up'
42+
color = (0, 255, 0) # Green color
43+
44+
return stage, count, color
45+
46+
# Processing the video frame by frame
47+
while cap.isOpened():
48+
ret, frame = cap.read()
49+
if not ret:
50+
break
51+
52+
# Detecting pose and getting landmark positions
53+
frame = detector.find_pose(frame, draw=False)
54+
landmarks = detector.get_positions(frame)
55+
56+
left_shoulder, right_shoulder = landmarks[11], landmarks[12]
57+
left_elbow, right_elbow = landmarks[13], landmarks[14]
58+
59+
# Updating count and color based on right arm position
60+
stage, count, color = update_count_and_color(right_elbow, right_shoulder, stage, count)
61+
62+
# Displaying the count on the frame
63+
draw_text_with_bg(frame, f"Count: {count}", (0, 60), font_scale=2, thickness=4, bg_color=color,
64+
text_color=(0, 0, 0))
65+
66+
# Resizing the frame for display
67+
resizing_factor = 0.45
68+
resized_shape = (int(resizing_factor * frame.shape[1]), int(resizing_factor * frame.shape[0]))
69+
resized_frame = cv.resize(frame, resized_shape)
70+
71+
# Writing the frame to the output video
72+
out.write(frame)
73+
74+
# Displaying the resized video
75+
cv.imshow("Video", resized_frame)
76+
77+
# Breaking on 'p' key press
78+
if cv.waitKey(1) & 0xff == ord('p'):
79+
break
80+
81+
# Releasing video resources
82+
cap.release()
83+
out.release()
84+
cv.destroyAllWindows()
5.04 MB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)