-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquat_test.py
More file actions
77 lines (58 loc) · 2.64 KB
/
Copy pathsquat_test.py
File metadata and controls
77 lines (58 loc) · 2.64 KB
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
import cv2
import mediapipe as mp
import time
# Initialize MediaPipe Pose
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
# Initialize MediaPipe Drawing
mp_drawing = mp.solutions.drawing_utils
# Capture video from the standard (default) camera
cap = cv2.VideoCapture(0) # Use 0 for the default webcam
# Adjusted thresholds for detecting squats
squat_threshold_down = 0.05 # Adjusted threshold for detecting the downward movement
squat_threshold_up = 0.02 # Adjusted threshold for detecting the upward movement
# Flags and counters
squat_down = False
squat_count = 0
last_squat_time = 0
message_duration = 2 # Duration to prevent multiple detections in seconds
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Convert the frame to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame with MediaPipe Pose
results = pose.process(rgb_frame)
if results.pose_landmarks:
# Get the landmarks
landmarks = results.pose_landmarks.landmark
# Get the y-coordinate of the hips and knees
left_hip_y = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y
right_hip_y = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y
left_knee_y = landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y
right_knee_y = landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y
# Compute the average y-coordinate of hips and knees
avg_hip_y = (left_hip_y + right_hip_y) / 2
avg_knee_y = (left_knee_y + right_knee_y) / 2
current_time = time.time()
# Check if the user is squatting down
if avg_hip_y >= ((80 * (avg_knee_y + squat_threshold_down)) / 100):
if not squat_down and (current_time - last_squat_time) > message_duration:
squat_down = True
squat_count += 1
last_squat_time = current_time
# Check if the user is standing up from the squat
if avg_hip_y <= ((80 * (avg_knee_y + squat_threshold_down)) / 100):
if squat_down:
squat_down = False
# Draw the pose landmarks on the frame
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# Display the squat count on the frame
cv2.putText(frame, f'Squat Count: {squat_count}', (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# Display the frame
cv2.imshow('Squat Detection', frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()