-
Notifications
You must be signed in to change notification settings - Fork 57
/
step_counting.py
82 lines (66 loc) · 2.42 KB
/
step_counting.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
import cv2
from ultralytics import YOLO
import numpy as np
from gtts import gTTS
from playsound import playsound
import tempfile
# Load the YOLO model
model = YOLO("yolov8s-pose.pt")
# Open the webcam
cap = cv2.VideoCapture(0)
# Define the body part indices
body_index = {"left_knee": 13, "right_knee": 14, "left_ankle": 15, "right_ankle": 16}
# Initialize step count, previouqs positions, and thresholds
step_count = 0
prev_left_ankle_y = None
prev_right_ankle_y = None
step_threshold = 12
min_wait_frames = 8
wait_frames = 0
# Generate the 'Step' audio file
tts = gTTS(text="Step", lang="en")
temp_file = tempfile.NamedTemporaryFile(delete=False)
tts.save(temp_file.name)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model(frame, verbose=False, conf=0.5)
annotated_frame = results[0].plot()
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Round the results to the nearest decimal
rounded_results = np.round(results[0].keypoints.numpy(), 1)
# Get the keypoints for the body parts
try:
left_knee = rounded_results[0][body_index["left_knee"]]
right_knee = rounded_results[0][body_index["right_knee"]]
left_ankle = rounded_results[0][body_index["left_ankle"]]
right_ankle = rounded_results[0][body_index["right_ankle"]]
if (
(left_knee[2] > 0.5)
and (right_knee[2] > 0.5)
and (left_ankle[2] > 0.5)
and (right_ankle[2] > 0.5)
):
if (
prev_left_ankle_y is not None
and prev_right_ankle_y is not None
and wait_frames == 0
):
left_diff = abs(left_ankle[1] - prev_left_ankle_y)
right_diff = abs(right_ankle[1] - prev_right_ankle_y)
if max(left_diff, right_diff) > step_threshold:
step_count += 1
print(f"Step taken: {step_count}")
wait_frames = min_wait_frames
prev_left_ankle_y = left_ankle[1]
prev_right_ankle_y = right_ankle[1]
if wait_frames > 0:
wait_frames -= 1
except:
print("No human detected.")
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows