├── AI Pose estimation using OpenCV and Mediapipe.mp4
├── LICENSE
├── README.md
├── Squat pose estimation.ipynb
├── output
├── dig.png
├── info
├── output1.gif
├── output2.gif
├── points.PNG
├── pose_tracking_detector_vitruvian_man.png
├── squat.jpg
└── types.png
└── youTube_video.mp4
/AI Pose estimation using OpenCV and Mediapipe.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/AI Pose estimation using OpenCV and Mediapipe.mp4
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Pradnya Rajendra Patil
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | [1]: https://github.com/Pradnya1208
5 | [2]: https://www.linkedin.com/in/pradnya-patil-b049161ba/
6 | [3]: https://public.tableau.com/app/profile/pradnya.patil3254#!/
7 | [4]: https://twitter.com/Pradnya1208
8 |
9 |
10 | [][1]
11 | [][2]
12 | [.svg)][3]
13 | [][4]
14 |
15 |
16 |
17 | # Squat angle detection using OpenCv and Mediapipe
18 |
19 |

20 |
21 |
22 | ## Overview:
23 | Human pose estimation from video plays a critical role in various applications such as quantifying physical exercises, sign language recognition, and full-body gesture control. For example, it can form the basis for yoga, dance, and fitness applications. It can also enable the overlay of digital content and information on top of the physical world in augmented reality.
24 |
25 | ## ML Pipeline:
26 |
27 | The solution utilizes a two-step detector-tracker ML pipeline. Using a detector, the pipeline first locates the person/pose region-of-interest (ROI) within the frame. The tracker subsequently predicts the pose landmarks and segmentation mask within the ROI using the ROI-cropped frame as input. Note that for video use cases the detector is invoked only as needed, i.e., for the very first frame and when the tracker could no longer identify body pose presence in the previous frame. For other frames the pipeline simply derives the ROI from the previous frame’s pose landmarks.
28 |
29 |

30 |
31 |
32 | ## Objectives:
33 |
34 | - In this project we are going to detect the crucial angles in Squat position.
35 | - The squat and deadlift are exercises prescribed by strength and conditioning professionals for the purpose of strengthening the legs, hips, back and torso musculature
36 | - They are considered closed chain, compound lifts involving the integration of multiple joint systems and muscle groups.
37 | - It has also been shown that long-term lifting with squats and deadlifts not only promotes an increase in bone mineral density in young populations, but it may also help maintain this adaptation well into the later stages of life
38 | - When performed correctly, injuries related to these exercises are uncommon, however poor technique or inappropriate prescription can lead to wide range of issues, especially in combination with heavy weights. Considering the complexity of these exercises and the variables related to performance, understanding the biomechanics is of great importance for achieving optimal muscular development as well as reducing training related injury.
39 | - Therefore, the purpose of this project is to detect the squat angles which will be helpful for the fitness instructors to provide corrective advice where appropriate.
40 |
41 | ## Implementation:
42 |
43 | **Libraries:** `NumPy` `cv2` `Mediapipe`
44 |
45 | ## License
46 |
47 | [MIT](https://github.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/blob/main/LICENSE/)
48 |
49 |
50 |
51 |
52 | ## The Squat:
53 | The squat starts with the descent phase as the hips, knees and ankles all flex. A common cue is to descend until the thighs are parallel with the floor, and the hip joint is either parallel or below the knee joint. Ascent is performed primarily through triple extension of the hips knees and ankles, until the subject returns to the starting position.
54 |
55 |

56 |
57 |
58 | ## Mechanics of the squat:
59 | Analysis of the squat can be achieved by sub categorising the movement into three comprehensive domains; upper body, lower body and movement mechanics. The upper body emphasises the stability and posture of the head, neck and torso, the lower body asses the joint positions of hips, knees and ankles, finally the movement mechanics assess the timing and co-ordination of the exercise.
60 | When we look at the squat, there are typically two main areas we look at:
61 | 1. The knee joint
62 | 2. The hip joint
63 |
64 | When the squat is paused in a certain position, we can then measure the angle of the joints.
65 | **The hip angle** is formed by the position of the back and the thigh. **The knee angle** is formed by the thigh and the position of the lower leg.
66 |
67 | ### Angle Calculation:
68 |
69 |

70 |
71 |
72 | ```
73 | def calculate_angle(a,b,c):
74 | a = np.array(a) # First
75 | b = np.array(b) # Mid
76 | c = np.array(c) # End
77 |
78 | radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
79 | angle = np.abs(radians*180.0/np.pi)
80 |
81 | if angle >180.0:
82 | angle = 360-angle
83 |
84 | return angle
85 | ```
86 | For Calculating angles we need Co-ordinates of **shoulder**, **hip**, **knee**, and **ankle**, which can be obtained from landmark points.
87 | - The landmark model in MediaPipe Pose predicts the location of 33 pose landmarks (see figure below).
88 | 
89 |
90 | #### Get coordinates:
91 | ```
92 | landmarks = results.pose_landmarks.landmark
93 | shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
94 | hip = [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y]
95 | knee = [landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y]
96 | ankle = [landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y]
97 | ```
98 | **Knee-joint angle**:
99 | ```
100 | angle_knee = calculate_angle(hip, knee, ankle)
101 | knee_angle = 180-angle_knee
102 | ```
103 | **Hip-joint angle**:
104 | ```
105 | angle_hip = calculate_angle(shoulder, hip, knee)
106 | hip_angle = 180-angle_hip
107 | ```
108 |
109 | Checkout the [Notebook](https://github.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/blob/main/Squat%20pose%20estimation.ipynb) for complete implementation.
110 |
111 |
112 |

113 |
114 |
115 |
116 |
117 |
118 |
119 | ## Lessons Learned
120 | `Computer Vision`
121 | `Pose Detection`
122 | `Pose Estimation`
123 | `Landmark angle calculation`
124 | `Mediapipe`
125 |
126 | ## References:
127 | - [Mediapipe](https://google.github.io/mediapipe/solutions/pose)
128 | - [BioChemical Analysis of Squat](https://stuartbauld.wixsite.com/performance/single-post/2018/02/27/biomechanical-analysis-of-squat-and-deadlift-1)
129 | - [The real science of the squat](https://squatuniversity.com/2016/04/20/the-real-science-of-the-squat/)
130 |
131 | ### Feedback
132 |
133 | If you have any feedback, please reach out at pradnyapatil671@gmail.com
134 |
135 |
136 |
137 | [1]: https://github.com/Pradnya1208
138 | [2]: https://www.linkedin.com/in/pradnya-patil-b049161ba/
139 | [3]: https://public.tableau.com/app/profile/pradnya.patil3254#!/
140 | [4]: https://twitter.com/Pradnya1208
141 |
142 |
143 | [][1]
144 | [][2]
145 | [.svg)][3]
146 | [][4]
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
--------------------------------------------------------------------------------
/output/dig.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/dig.png
--------------------------------------------------------------------------------
/output/info:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/output/output1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/output1.gif
--------------------------------------------------------------------------------
/output/output2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/output2.gif
--------------------------------------------------------------------------------
/output/points.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/points.PNG
--------------------------------------------------------------------------------
/output/pose_tracking_detector_vitruvian_man.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/pose_tracking_detector_vitruvian_man.png
--------------------------------------------------------------------------------
/output/squat.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/squat.jpg
--------------------------------------------------------------------------------
/output/types.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/output/types.png
--------------------------------------------------------------------------------
/youTube_video.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pradnya1208/Squats-angle-detection-using-OpenCV-and-mediapipe_v1/e4d33bdcaf8a06a6916195ebf7a9ab458ff200d4/youTube_video.mp4
--------------------------------------------------------------------------------