Ultralytics YOLO26을 사용한 운동 모니터링
자세 추정과 Ultralytics YOLO26을 통해 운동을 모니터링하면 주요 신체 랜드마크와 관절을 실시간으로 정확하게 추적하여 운동 평가를 개선할 수 있습니다. 이 기술은 운동 자세에 대한 즉각적인 피드백을 제공하고, 운동 루틴을 추적하며, 성과 지표를 측정하여 사용자와 트레이너 모두의 훈련 세션을 최적화합니다.
시청: Ultralytics YOLO로 운동 동작을 모니터링하는 방법 | 스쿼트, 레그 익스텐션, 팔굽혀펴기 등
운동 모니터링의 장점#
- 성능 최적화: 모니터링 데이터를 바탕으로 운동을 맞춤화하여 더 나은 결과를 얻습니다.
- 목표 달성: 측정 가능한 진척을 위해 피트니스 목표를 추적하고 조정합니다.
- 개인화: 효과를 높이기 위해 개인 데이터를 바탕으로 맞춤 운동 계획을 제공합니다.
- 건강 인식 향상: 건강 문제나 과도한 트레이닝을 나타내는 패턴을 조기에 감지합니다.
- 정보에 기반한 의사결정: 데이터를 바탕으로 루틴을 조정하고 현실적인 목표를 설정합니다.
실제 애플리케이션#
| 운동 모니터링 | 운동 모니터링 |
|---|---|
![]() | ![]() |
| 푸시업 횟수 계산 | 풀업 횟수 계산 |
YOLO26으로 운동 모니터링하기#
AIGym 솔루션은 자세 추정에 YOLO26n-pose와 같은 모델을 사용하고, 팔다리가 up_angle와 down_angle 임계값 사이를 움직일 때 세 키포인트가 이루는 각도를 측정해 반복 횟수를 계산합니다. kpts을 사용해 추적하려는 운동의 키포인트를 선택한 다음, Python API 또는 CLI로 비디오에서 솔루션을 실행합니다.
# Run a workout example
yolo solutions workout show=True
# Pass a source video
yolo solutions workout source="path/to/video.mp4"
# Use keypoints for pushups
yolo solutions workout kpts="[6, 8, 10]"키포인트 맵#

AIGym 인수#
다음은 AIGym 인수 표입니다:
| 인수 | 유형 | 기본값 | 설명 |
|---|---|---|---|
model | str | None | Ultralytics YOLO 모델 파일의 경로입니다. |
up_angle | float | 145.0 | '위' 자세의 각도 임계값입니다. |
down_angle | float | 90.0 | '아래' 자세의 각도 임계값입니다. |
kpts | list[int] | [6, 8, 10] | 운동 모니터링에 사용할 키포인트 인덱스 3개의 목록입니다. 이 키포인트는 팔굽혀펴기, 턱걸이, 스쿼트 및 복근 운동과 같은 운동에 필요한 어깨, 팔꿈치, 손목 등의 신체 관절이나 부위에 해당합니다. |
line_width | int | 2 | Solution에서 그리는 상자, 키포인트 및 개수 표시의 선 두께입니다. |
verbose | bool | True | 프레임별 입력 형태, 클래스 개수 및 처리 속도 로그를 활성화합니다. 추적 호출 자체는 항상 로그를 출력하지 않습니다. |
AIGym 솔루션은 다양한 객체 추적 매개변수도 지원합니다.
| 인수 | 유형 | 기본값 | 설명 |
|---|---|---|---|
tracker | str | 'tracktrack.yaml' | 사용할 추적 알고리즘을 지정합니다. 기본 제공 옵션: botsort.yaml, bytetrack.yaml, ocsort.yaml, deepocsort.yaml, fasttrack.yaml, tracktrack.yaml. |
conf | float | 0.25 | 탐지에 대한 신뢰도 임계값을 설정합니다. 값을 낮추면 추적할 수 있는 객체가 늘어나지만 오탐이 포함될 수 있습니다. |
iou | float | 0.5 | 중복 탐지를 필터링하기 위한 교집합 대비 합집합 (IoU) 임계값을 설정합니다. |
classes | list | None | 클래스 인덱스로 결과를 필터링합니다. 예를 들어 classes=[0, 2, 3]은 지정된 클래스만 추적합니다. |
device | str | None | 추론에 사용할 장치를 지정합니다(예: cpu, cuda:0 또는 0). 사용자는 모델 실행에 사용할 CPU, 특정 GPU 또는 기타 연산 장치를 선택할 수 있습니다. |
imgsz | int or tuple | 640 | 추론 대상 이미지 크기입니다. 정사각형을 나타내는 정수 또는 (height, width) 쌍을 지정합니다. 모델 stride에 맞춰 형태가 조정됩니다. |
max_det | int | 300 | 추적 전에 프레임별로 유지할 최대 탐지 수를 지정하여 밀집된 장면에서 솔루션이 처리하는 객체 수를 제한합니다. |
quantize | int or str | None | 추론 정밀도: CPU 이외의 환경에서 PyTorch 및 TorchScript 모델은 16(FP16) 또는 32/unset(FP32)을 사용하며, 다른 형식은 아티팩트와 런타임이 선택하는 정밀도를 사용합니다. 더 이상 사용되지 않는 half 인수를 대체합니다. |
또한 다음 시각화 설정을 적용할 수 있습니다.
| 인수 | 유형 | 기본값 | 설명 |
|---|---|---|---|
show | bool | False | True이면 주석이 추가된 이미지나 비디오를 창에 표시합니다. 개발 또는 테스트 중 즉각적인 시각적 피드백을 얻는 데 유용합니다. |
show_labels | bool | True | 시각화 출력에 각 감지 결과의 레이블을 표시합니다. 감지된 객체를 즉시 파악할 수 있습니다. |
자주 묻는 질문#
Ultralytics YOLO26으로 운동을 모니터링하려면 자세 추정 기능을 활용해 주요 신체 랜드마크와 관절을 실시간으로 추적하고 분석할 수 있습니다. 이를 통해 운동 자세에 대한 즉각적인 피드백을 받고, 반복 횟수를 계산하며, 성과 지표를 측정할 수 있습니다. 다음과 같이 제공된 푸시업, 풀업 또는 복근 운동 예제 코드로 시작할 수 있습니다.
import cv2 from ultralytics import solutions cap = cv2.VideoCapture("path/to/video.mp4") assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) gym = solutions.AIGym( line_width=2, show=True, kpts=[6, 8, 10], ) while cap.isOpened(): success, im0 = cap.read() if not success: print("Video frame is empty or processing is complete.") break results = gym(im0) cap.release() cv2.destroyAllWindows()운동 모니터링에 Ultralytics YOLO26을 사용하면 다음과 같은 주요 이점을 얻을 수 있습니다.
- 성능 최적화: 모니터링 데이터를 바탕으로 운동을 맞춤화하면 더 나은 결과를 얻을 수 있습니다.
- 목표 달성: 측정 가능한 진척을 위해 피트니스 목표를 쉽게 추적하고 조정할 수 있습니다.
- 개인화: 최적의 효과를 위해 개인 데이터를 바탕으로 맞춤 운동 계획을 받을 수 있습니다.
- 건강 인식 향상: 잠재적인 건강 문제나 과도한 트레이닝을 나타내는 패턴을 조기에 감지합니다.
- 정보에 기반한 의사결정: 데이터를 바탕으로 루틴을 조정하고 현실적인 목표를 설정할 수 있습니다.
이러한 이점이 실제로 어떻게 구현되는지 YouTube 동영상 데모를 시청할 수 있습니다.
Ultralytics YOLO26 자세 모델은 주요 신체 랜드마크와 관절을 실시간으로 정확하게 추적하여 운동 자세와 반복 횟수에 대한 신뢰할 수 있는 피드백을 제공합니다. 실제 정확도는 카메라 각도, 조명, 가림 현상, 그리고 각 운동에 적합한
kpts,up_angle,down_angle값을 선택하는 것에도 영향을 받습니다.예, Ultralytics YOLO26은 사용자 지정 운동 루틴에 맞게 조정할 수 있습니다.
AIGym클래스는up_angle,down_angle,kpts인수를 사용해 운동 반복 횟수를 감지합니다. 특정 운동을 감지하도록 키포인트와 각도를 지정할 수 있습니다. 설정 예제는 다음과 같습니다.from ultralytics import solutions gym = solutions.AIGym( line_width=2, show=True, kpts=[6, 8, 10], # 푸시업용입니다. 다른 운동에 맞게 사용자 지정할 수 있습니다. )이러한 유연성 덕분에 다양한 운동을 모니터링하고 피트니스 목표에 따라 루틴을 사용자 지정할 수 있습니다.
운동 모니터링 출력을 저장하려면 처리된 프레임을 저장하는 비디오 라이터를 코드에 추가하면 됩니다. 다음은 예제입니다.
import cv2 from ultralytics import solutions cap = cv2.VideoCapture("path/to/video.mp4") assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) video_writer = cv2.VideoWriter("workouts.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) gym = solutions.AIGym( line_width=2, show=True, kpts=[6, 8, 10], ) while cap.isOpened(): success, im0 = cap.read() if not success: print("Video frame is empty or processing is complete.") break results = gym(im0) video_writer.write(results.plot_im) cap.release() video_writer.release() cv2.destroyAllWindows()이 설정은 모니터링한 동영상을 출력 파일에 기록하므로 나중에 운동 성과를 검토하거나 트레이너와 공유해 추가 피드백을 받을 수 있습니다.
댓글41
질문을 하거나 결과를 공유하거나 다른 사람을 도와주세요.
流川疯May I ask how to obtain the value of step and perform some logical operations, such as counting to 10 times for pop-up reminders,many thanks
1
Glenn JocherUltralytics(수정됨)@wynshiter the
AIGymsolution counts repetitions for you: callresults = gym_object(frame)and readresults.workout_count(one entry per tracked person). Add your own logic on top, for exampleif any(c >= 10 for c in results.workout_count):to trigger a pop-up reminder. For further details, please refer to the documentation on workouts monitoring.nel-mv
Thanks for your answer, Glenn. And the documentation on workouts monitoring is just this same page (https://docs.ultralytics.com/guides/workouts-monitoring) we are commenting on?
And from the comments you gave above, we know that we can get the angles from the gym object: im0, angle_data = gym_object.start_counting(im0, results)
I was expecting we could get the 'step' value similarly via a return variable.
Do you know where we could see a list of return variables that the gym object gives?
Glenn JocherUltralytics(수정됨)The
AIGymresults provideworkout_count,workout_stageandworkout_anglefor each tracked person (results = gym_object(frame)), so repetitions are counted for you. To act on them, add your own logic in your processing loop, for example whenworkout_countreaches a target. For further details, please refer to the Workouts Monitoring documentation.DanishkhanOp
Where to get the model
Glenn JocherUltralytics(수정됨)@DanishkhanOp the pose model (
yolo26n-pose.ptby default) is downloaded automatically the first timeAIGymis initialized, or you can download it manually from the Ultralytics YOLO26 Model Documentation. You can also specifymodel="yolo26n-pose.pt"in yourAIGym()constructor as shown in the workout monitoring examples.
AhmedMostafa3m
hello, i guess i came a little late , it ssems that start_counting method is not available any more i want to know how can i use the code above after updating. results = model.predict(frame, verbose=False) frame = gym_object.start_counting(frame, results) thanks alot
Glenn JocherUltralytics(수정됨)@AhmedMostafa3m
start_countinghas been replaced: the solution is now called directly on the frame and runs pose tracking itself, so no separatemodel.trackcall is needed:from ultralytics import solutions gym = solutions.AIGym(model="yolo26n-pose.pt") # Initialize once results = gym(frame) frame = results.plot_imFor full implementation details, see the AIGym documentation.
DanishkhanOp
Where i can get the model
Glenn JocherUltralytics(수정됨)@DanishkhanOp the pose estimation model is used automatically when you initialize the
AIGymclass as shown in the code examples. By default it isyolo26n-pose.pt, which you can also set withmodel="yolo26n-pose.pt"in yoursolutions.AIGym()call; it will be downloaded from our servers if not already cached. For more details on pose models, visit our Keypoint Estimation Docs.
AdadAlShabab
Hi. Suppose I'm detecting the angle between [8, 6, 12] and I wanna show text something like: "The angle between Shoulder and Wrist : {detected angle}" using cv2.putText. How3 can I do that?
Glenn JocherUltralytics@AdadAlShabab to display the angle using
cv2.putText, you can calculate the angle between the keypoints and then overlay the text on the image. Here's a minimal example:import cv2 import numpy as np # Assuming you have the coordinates of the keypoints shoulder = np.array([x1, y1]) elbow = np.array([x2, y2]) wrist = np.array([x3, y3]) # Calculate the angle angle = np.degrees(np.arctan2(wrist[1] - elbow[1], wrist[0] - elbow[0]) - np.arctan2(shoulder[1] - elbow[1], shoulder[0] - elbow[0])) # Display the angle cv2.putText(im0, f"Angle: {angle:.2f}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)This will show the angle between the shoulder, elbow, and wrist on your video frame. Adjust the coordinates and text position as needed.
1AdadAlShabab
Hi Glenn. Thanks for your wonderful reply. Can you please tell the about the keypoints [x1, y1]? Like : "shoulder = np.array([x1, y1]) elbow = np.array([x2, y2]) wrist = np.array([x3, y3]" What values should I put here?
Glenn JocherUltralytics@AdadAlShabab to determine the values for keypoints like [x1, y1], you need to extract them from the model's output. When you run the model, it provides coordinates for each keypoint. For example,
results[0].keypointswill give you the keypoints, and you can access specific ones using their indices. Adjust these based on your specific use case.AdadAlShabab
This code: " if result[0].keypoints is not None: keypoints = result[0].keypoints.data.cpu().numpy() # Access the actual keypoints data
# Debugging: Print keypoints shape to understand the issue print(f"Keypoints shape: {keypoints.shape}")"Providing me results for each frame: 0: 384x640 1 person, 45.3ms Speed: 2.0ms preprocess, 45.3ms inference, 0.0ms postprocess per image at shape (1, 3, 384, 640) Keypoints shape: (1, 17, 3) Person keypoints shape: (17, 3) Keypoint 0: (x=1086.50, y=456.84), Confidence=0.97 Keypoint 1: (x=1098.32, y=446.95), Confidence=0.95 Keypoint 2: (x=1077.20, y=450.47), Confidence=0.90 Keypoint 3: (x=1121.03, y=453.73), Confidence=0.87 Keypoint 4: (x=1068.29, y=461.00), Confidence=0.64 Keypoint 5: (x=1147.66, y=499.11), Confidence=0.99 Keypoint 6: (x=1054.84, y=529.72), Confidence=0.98 Keypoint 7: (x=1192.78, y=521.58), Confidence=0.98 Keypoint 8: (x=1061.91, y=577.40), Confidence=0.90 Keypoint 9: (x=1161.27, y=528.25), Confidence=0.95 Keypoint 10: (x=1100.56, y=558.10), Confidence=0.86 Keypoint 11: (x=1189.49, y=611.80), Confidence=0.99 Keypoint 12: (x=1134.91, y=632.54), Confidence=0.98 Keypoint 13: (x=1234.99, y=644.81), Confidence=0.97 Keypoint 14: (x=1194.94, y=665.07), Confidence=0.94 Keypoint 15: (x=1295.68, y=737.43), Confidence=0.88 Keypoint 16: (x=1300.56, y=750.85), Confidence=0.85
Are these the values you were talking about?
Glenn JocherUltralytics(수정됨)Yes, those are the keypoint values. You can use them to calculate angles between specific joints. For example, use the coordinates of keypoints 8 (elbow), 6 (shoulder), and 12 (hip) to compute the angle and display it with
cv2.putText. If you need further assistance, feel free to ask!
actionTurtle
If I wanted to count how many steps you took running. Would you just set the pose type to "running"? Or do I need to make my own model for running? Thank you
Glenn JocherUltralytics@actionTurtle to count steps for running, you'll need to customize the model since "running" isn't a predefined pose type like "pushup" or "pullup." You can adapt the existing model by training it with running-specific keypoints. For more details, please refer to the workouts monitoring guide.
actionTurtle
"You can adapt the existing model by training it with running-specific keypoints."
Sorry, I still don't understand. Is there a walk though of how I would do that?
Glenn JocherUltralytics@actionTurtle to create a custom model for running, you'll need to train it with running-specific keypoints. Unfortunately, there's no built-in "running" pose type, so you'll need to gather a dataset of running poses and train the model accordingly. For a detailed guide on training custom models, please refer to the Ultralytics documentation.
AdadAlShabab
Hi. The model is working pretty fine. But when I'm running the python file the detected angle data are shown in box. But I want to have the data stored in variable or maybe a list so that I can use them use outside or print in console. Please help.
Glenn JocherUltralytics(수정됨)@AdadAlShabab to store the detected angle data in a list, read
workout_anglefrom the results the solution returns for each frame. Here's a basic example:import cv2 from ultralytics import solutions cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" gym_object = solutions.AIGym(model="yolo26n-pose.pt", kpts=[6, 8, 10], show=True) angles = [] while cap.isOpened(): success, im0 = cap.read() if not success: break results = gym_object(im0) angles.append(results.workout_angle) # one angle per tracked person cap.release() cv2.destroyAllWindows() print(angles)This will store the angle data in the
angleslist for further use.2AdadAlShabab
Hi. I want detect angle of two hands simultaneously. So I wrote the code : gym_object = solutions.AIGym( line_thickness=2, view_img=True, pose_type="pushup", kpts_to_check={[8, 6, 12,],[5, 7, 9]}, )
But it giving me error: TypeError: unhashable type: 'list'
How to solve? Please let me know. TIA
Glenn JocherUltralytics(수정됨)kptsmust be a flat list of three keypoint indices, written with square brackets, for examplekpts=[6, 8, 10]. Curly braces raise the "unhashable type" error, and nested lists such as[[8, 6, 12], [5, 7, 9]]are not supported. To track two sets of keypoints, create oneAIGyminstance for each:gym_left = solutions.AIGym(kpts=[5, 7, 9]) gym_right = solutions.AIGym(kpts=[6, 8, 10])AdadAlShabab
Hi Glenn! Still it throwing error: im0 = gym_object.start_counting(im0, results) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Bass fish streamlit.venv\Lib\site-packages\ultralytics\solutions\ai_gym.py", line 85, in start_counting k[int(self.kpts_to_check[0])].cpu(), ^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Glenn JocherUltralytics(수정됨)The error occurs because
kptsshould be a flat list of three integers, not nested lists. To track both hands, create a separateAIGyminstance for each set of keypoints and run both on every frame. If you need further assistance, please check the latest documentation or update your package to the latest version.
sg zhouCan we train extra classes for pose prediction?
Glenn JocherUltralytics@Shengguang-Zhou yes, you can train extra classes for pose prediction by customizing the
AIGymclass and specifying the keypoints and angles for the exercises you want to monitor. For more details, please refer to the Workouts Monitoring Guide.
MarserHK
Hello, I would like to ask if there are any examples of using YOLOv8 for behavior recognition in electrical power field scenarios?
Glenn JocherUltralytics@MarserHK currently, we don't have specific examples for behavior recognition in electrical power field scenarios. However, you can adapt the pose estimation capabilities of YOLOv8 for such tasks by training on relevant datasets. For more details on pose estimation, you can refer to the workouts monitoring guide here.
duncanwaugh
Hello, it seems like it has to be setup for a specific type of workout eg. pushups and then it would track pushups, but if you started squatting it wouldn't track your squats. I'm curious if there is a way where U can have a live video feed where lets say you do 10 pushups, then 10 pullups, it will identify how its a different exercise and count it. Thanks.
Glenn JocherUltralytics(수정됨)Hello,
Thank you for your insightful question! You're correct that the current setup of the
AIGymsolution in Ultralytics YOLOv8 is designed to track specific types of workouts like pushups, pullups, and ab workouts individually. However, your idea of dynamically identifying and counting different exercises in a single session is quite intriguing and can be approached with some modifications.Dynamic Workout Detection and Counting
To achieve dynamic detection and counting of different exercises, you would need to implement a mechanism that can:
- Identify the type of exercise being performed: This could be done by analyzing the keypoint movements and angles to classify the exercise type.
- Switch the counting logic based on the identified exercise: Once the exercise type is identified, the corresponding counting logic can be applied.
Here's a conceptual approach to get you started:
- Exercise Classification: Use a simple heuristic or a machine learning model to classify the exercise based on keypoint movements.
- Dynamic Counting: Switch between different
AIGymconfigurations based on the classified exercise.
Example Code
Below is a basic example to illustrate this concept. Note that this is a starting point and may require further refinement based on your specific requirements.
import cv2 from ultralytics import YOLO, solutions # Load the pose estimation model model = YOLO("yolo26n-pose.pt") cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) # Initialize video writer if needed video_writer = cv2.VideoWriter("workouts.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) # Define a function to classify the exercise type def classify_exercise(keypoints): # Placeholder logic for exercise classification # Replace with actual logic based on keypoint analysis if some_condition_based_on_keypoints: return "pushup" elif some_other_condition_based_on_keypoints: return "pullup" else: return "unknown" # Initialize the AIGym object gym_objects = { "pushup": solutions.AIGym(show=True, kpts=[6, 8, 10]), "pullup": solutions.AIGym(show=True, kpts=[6, 8, 10]), # Add other exercises as needed } frame_count = 0 current_exercise = "unknown" while cap.isOpened(): success, im0 = cap.read() if not success: print("Video frame is empty or video processing has been successfully completed.") break frame_count += 1 results = model.track(im0, verbose=False) # Tracking recommended # Classify the exercise based on keypoints keypoints = results[0].keypoints exercise_type = classify_exercise(keypoints) if exercise_type in gym_objects: gym_object = gym_objects[exercise_type] im0 = gym_object(im0).plot_im # AIGym runs its own pose tracking video_writer.write(im0) cv2.destroyAllWindows() video_writer.release()Next Steps
- Refine Exercise Classification: Develop a robust method to classify exercises based on keypoint data. This could involve machine learning models trained on labeled exercise data.
- Expand Exercise Types: Add more exercises to the
gym_objectsdictionary and their corresponding keypoint checks. - Optimize Performance: Ensure the solution runs efficiently in real-time, especially if processing live video feeds.
For more detailed guidance and advanced configurations, you can refer to our Workouts Monitoring Guide.
Feel free to experiment with this approach and let us know how it works for you! If you encounter any issues or have further questions, don't hesitate to reach out.
Glenn JocherUltralytics(수정됨)@duncanwaugh hello,
Thank you for your insightful question! You're correct that the current setup of Ultralytics YOLOv8 for workout monitoring is designed to track specific exercises like pushups, pullups, and ab workouts individually. However, your idea of having a live video feed that can dynamically identify and count different exercises in sequence is fascinating and certainly feasible with some modifications.
Dynamic Exercise Recognition
To achieve dynamic exercise recognition, you would need to implement a mechanism that can classify the type of exercise being performed in real-time and then switch to the matching
AIGymcounter. Here's a high-level approach to how you might achieve this:-
Exercise Classification: Use a classification model to identify the type of exercise being performed. This model can be trained to recognize different exercises based on the input video frames.
-
Counter Switching: Once the exercise type is identified, switch to the
AIGyminstance (keypointskpts) for that exercise. This can be done dynamically within the video processing loop.
Example Implementation
Below is a simplified example to illustrate this concept. This example assumes you have a model for exercise classification;
AIGymruns its own pose model.import cv2 from ultralytics import YOLO, solutions # Load models classification_model = YOLO("exercise_classification_model.pt") cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) gym_objects = { "pushup": solutions.AIGym(show=True, kpts=[6, 8, 10]), "pullup": solutions.AIGym(show=True, kpts=[6, 8, 10]), "squat": solutions.AIGym(show=True, kpts=[12, 14, 16]) } current_exercise = None frame_count = 0 while cap.isOpened(): success, im0 = cap.read() if not success: print("Video frame is empty or video processing has been successfully completed.") break frame_count += 1 # Classify exercise type every few frames if frame_count % 30 == 0: # Adjust frequency as needed classification_results = classification_model.predict(im0) current_exercise = classification_model.names[classification_results[0].probs.top1] # class name of the top-1 prediction if current_exercise: gym_object = gym_objects[current_exercise] im0 = gym_object(im0).plot_im # AIGym runs its own pose tracking cv2.imshow("Workout Monitoring", im0) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()Key Points:
- Exercise Classification: The
classification_modelpredicts the type of exercise. - Counter Switching: Based on the predicted exercise, the corresponding
AIGymobject is used. - Dynamic Processing: This setup allows for dynamic switching between different exercises during a workout session.
This approach provides a flexible and scalable solution for monitoring multiple types of exercises in a single session. For more detailed guidance and advanced configurations, you can refer to our Workouts Monitoring Guide.
Feel free to experiment with this setup and adjust it to better fit your specific use case. If you encounter any issues or have further questions, don't hesitate to reach out!
-
duncanwaugh
awesome thanks
Glenn JocherUltralytics(수정됨)@duncanwaugh hello,
Thank you for your enthusiasm! 😊
To address your query about dynamically identifying and counting different exercises within a single workout session, such as transitioning from pushups to pullups, you would need to implement a mechanism that can classify the type of exercise being performed in real-time and then switch to the matching
AIGymcounter.Dynamic Exercise Recognition
Here's a high-level approach to achieve this:
- Exercise Classification: Use a classification model to identify the type of exercise being performed. This model can be trained to recognize different exercises based on the input video frames.
- Counter Switching: Once the exercise type is identified, switch to the
AIGyminstance (keypointskpts) for that exercise. This can be done dynamically within the video processing loop.
Example Implementation
Below is a simplified example to illustrate this concept. This example assumes you have a model for exercise classification;
AIGymruns its own pose model.import cv2 from ultralytics import YOLO, solutions # Load models classification_model = YOLO("exercise_classification_model.pt") cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) gym_objects = { "pushup": solutions.AIGym(show=True, kpts=[6, 8, 10]), "pullup": solutions.AIGym(show=True, kpts=[6, 8, 10]), "squat": solutions.AIGym(show=True, kpts=[12, 14, 16]) } current_exercise = None frame_count = 0 while cap.isOpened(): success, im0 = cap.read() if not success: print("Video frame is empty or video processing has been successfully completed.") break frame_count += 1 # Classify exercise type every few frames if frame_count % 30 == 0: # Adjust frequency as needed classification_results = classification_model.predict(im0) current_exercise = classification_model.names[classification_results[0].probs.top1] # class name of the top-1 prediction if current_exercise: gym_object = gym_objects[current_exercise] im0 = gym_object(im0).plot_im # AIGym runs its own pose tracking cv2.imshow("Workout Monitoring", im0) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()Key Points:
- Exercise Classification: The
classification_modelpredicts the type of exercise. - Counter Switching: Based on the predicted exercise, the corresponding
AIGymobject is used. - Dynamic Processing: This setup allows for dynamic switching between different exercises during a workout session.
This approach provides a flexible and scalable solution for monitoring multiple types of exercises in a single session. For more detailed guidance and advanced configurations, you can refer to our Workouts Monitoring Guide.
Feel free to experiment with this setup and adjust it to better fit your specific use case. If you encounter any issues or have further questions, don't hesitate to reach out!
1
felipeca2
hello, this is possible whit CLI? thanks
Paula DerrengerUltralytics(수정됨)Hello,
Yes, you can monitor workouts using the CLI with the
workoutsolution:yolo solutions workout source=path/to/video/file.mp4 kpts="[6, 8, 10]"The
kptsargument selects the three keypoints whose angle is counted ([6, 8, 10]is for pushups). For more detailed information, please refer to the documentation.Happy training! 💪

