Example - Squats Analysis
A sample Guru Schema that counts squats in a user video
Get Started
Let’s extend our default Guru Schema to counts reps and gives feedback on squats during a workout. You can clone this template here and run this app in your console.
We’ll walk through the code below:
Imports
First, let’s import some common utilities and data types from the standard Guru library guru/stdlib
. These will help us with our video analysis.
Constructor
Let’s initalize the state we want to track throughout the video, such as the frames that contain a person (personFrames
), the reps data for when squats begin and end (reps
), and the angles between the hip and knee joints so we can analyze the quality of the movement (hipKneeAngles
).
We can do this with the constructor
field in GuruSchema
.
Process Video
Now we can start writing the code that applies our AI models and processes each frame. This logic goes into processFrame
. The code below:
- Identifies objects labeled as “person” within the frame.
- Stores the first identified person in the personFrames array.
- Uses the MovementAnalyzer to determine repetitions based on keypoint distances.
- Calculates the angle between the right knee and right hip keypoints for each repetition.
- Returns the calculated outputs.
Render
Now that we’ve extracted information from the frames using AI, we’ll want to analyze that information and render it onto the video. To do this, we’ll use renderFrame. We’ll transform the state we’ve collected in processFrame
with standard Javascript functions and use guru/stdlib
functions to draw on the frame. The code below:
- Checks if there are any frames with a person identified.
- Draws bounding boxes and skeletons for the identified person.
- Displays a triangle between the right knee and right hip keypoints.
- Identifies and displays the current rep.
- Displays the hip-knee angle value. If the angle indicates bad form (less than -2°), a feedback message is shown to get the hips lower.
Outputs
Finally, we can use the outputs
method to specify what JSON output we want from our schema once the video is finished processing. In this case, we care about the number of reps and the hip-knee joint angle throughout the video.
Full Code
Putting it altogether, our GuruSchema
looks like this.