Hi everyone, I am Obata, an engineer from Next System.
We have developed a posture estimation AI engine called “VisionPose,” capable of detecting human skeletal information from camera footage, videos, and images.
Today, we would like to focus on the “VisionPose Nano,” an affordable research and development starter kit equipped with hardware, eliminating the need for complex setup and installation. We aim to address queries from those who have already acquired or are contemplating purchasing this kit and also share an incredibly convenient Excel sheet that allows you to calculate angles simply by copying and pasting the inference results obtained with Nano!
I understand the skeletal positions with Nano, but how do I calculate joint angles?
By the way, the conceptual aspects of the formulas described below are general, so it can be applied not only to Nano but also to other models within the VisionPose series.
Download the Excel sheet to calculate angles from three joint coordinates
Please download the Excel sheet that you can immediately use by simply copying and pasting the inference results obtained with Nano.
https://www.next-system.com/download/NanoSkeleton_Sample_with_degree.xls
If you copy and paste the inference results from Nano into columns A to AG, the right elbow joint angle will be automatically calculated in column AW!
If you want additional angles, simply copy and paste the formulas in a similar way.
When you copy and paste the inference results from Nano into columns A to AG…..
Yes, it’s simple! The angles are automatically calculated!
Yes, that’s it! Well done!
However, to make it more informative, I would like to explain what each AI to AW column in the Excel processing is doing regarding the angle calculations. Let’s go through the details.
For those wondering how angles are calculated in the first place, do not worry! I have provided a more detailed explanation in the latter part.
Extract only numerical data from the inference result string
First, let`s prepare the skeleton position information (\(x\) ,\(y\)), for the calculation.
AI column: Process to remove the brackets from the inference result string of the right shoulder
=MID(M3,2,LEN(M3)-2)
The square brackets at both ends are removed from the inference result string “[x y]” in column M on the right side (extracting from the second character to the second-to-last character).
Reference : MID function, LEN function
AJ Column : Process to extract only the \(x\) values of the right shoulder
=MID(AI3,1,FIND(” “,AI3,1)-1)
Extract only the \(x\) values from the right shoulder from column AI (values from the left of the string up to the space).
Reference : FIND function
AK Column : Process to extract only the \(y\) values of the right shoulder
=RIGHT(AI3,LEN(AI3)-FIND(” “,AI3,1))
Extract only the \(y\) values of the right shoulder from column AI (values from the right of the string up to the space).
Reference : RIGHT function
With this, we can extract the skeletal position information (\(x\) ,\(y\)) of the right shoulder!
Similarly, extract the (\(x\) ,\(y\)) coordinates for the right elbow (columns AL to AN) and right wrist (columns AO to AQ).
Calculate the skeleton vectors
AR Column : Process to calculate the X-component of the vector for the right upper arm (from right elbow to right shoulder).
=AJ3-AM3
Since the vector is from the right elbow to the right shoulder, the \(x\) value of the right elbow is subtracted from the \(x\) value of the right shoulder.
AS Column : Process to calculate the Y-component of the vector for the right upper arm (from right elbow to right shoulder)
=AK3-AN3
Similar to column AR, the \(y\) value of the right elbow is subtracted from the \(y\) value of the right shoulder.
Additionally, calculate the vector for the right forearm (from right elbow to right wrist) in columns AT to AU.
Calculate the angles
Finally, calculate the angle of the right elbow!
AV Column : Process to calculate the \(cos\)θ of the right elbow angle
=(AR3*AT3+AS3*AU3)/(SQRT(AR3*AR3+AS3*AS3)*SQRT(AT3*AT3+AU3*AU3))
Calculate the cosine from two vectors to determine the angle.
$$\cos\theta = \frac{ \vec{a}\cdot\vec{b} }{ |\vec{a}||\vec{b}| } = \frac{ a_{x}b_{x} + a_{y}b_{y} }{ \sqrt{a_{x}^2+a_{y}^2}\sqrt{b_{x}^2+b_{y}^2} }$$
(The details of the calculation formula will be explained later)
AW Column : Process to convert \(cos\)θ to degrees.
=DEGREES(ACOS(AV3))
Determine the angle (inverse cosine) from the cosine value and then convert it from radians to degrees
Reference : ACOS function, DEGREES function
By performing the above steps in Excel, calculate the joint angle of the right elbow.
Please note that the angle represents the angle formed by the two vectors, so it is expressed as the acute angle (0 to 180 degrees).
Additionally, there are cases where joints might not be visible to the camera, leading to certain inference results being ‘0.’ In such situations, it’s important to note that the joint angles cannot be determined.
Feel free to calculate the desired joint angles according to your preferences!
Explanation on how to calculate angles
Why can we calculate angles with the above formula? For those of you who are eagerly curious, here’s a bit more detailed explanation!
Angle between vectors
If you are familiar with vectors, you can calculate \(cos\)θ from the definition of the dot product of vectors.
Definition of the dot products of vectors
The dot product of vectors is defined as follows:
$$\vec{a}\cdot\vec{b} = |\vec{a}||\vec{b}|\cos\theta$$
Since \(cos\)θ is included here, you can calculate \(cos\)θ if you know the value of the dot product.
That means you just need to solve the following mathematical equation.
$$\cos\theta = \frac{ \vec{a}\cdot\vec{b} }{ |\vec{a}||\vec{b}| } \ldots①$$
Calculate \(cos\)θ from vector components
So, how can you find the value of the dot product? It can be calculated from the components of the vectors.
If the vectors are denoted as \(\vec{a} = (a_{x},a_{y}) , \vec{b} = (b_{x},b_{y})\) then,
The inner product of vectors will be
$$\vec{a}\cdot\vec{b} = a_{x}b_{x} + a_{y}b_{y}\ldots②$$
Also, \(|\vec{a}|\) and \(|\vec{b}|\) represent the lengths of their respective vectors, so it will be
$$|\vec{a}| = \sqrt{a_{x}^2+a_{y}^2}\ldots③$$
$$|\vec{b}| = \sqrt{b_{x}^2+b_{y}^2}\ldots④$$
By combining 1 to 4, it is possible to derive an equation to calculate \(cos\)θ from the vector components. (This formula was used in column AV of the Excel file.)
$$\cos\theta = \frac{ \vec{a}\cdot\vec{b} }{ |\vec{a}||\vec{b}| } = \frac{ a_{x}b_{x} + a_{y}b_{y} }{ \sqrt{a_{x}^2+a_{y}^2}\sqrt{b_{x}^2+b_{y}^2} }$$
Convert \(cos\)θ to an angle
From the above formula, it becomes clear that knowing the coordinates of three points allows us to calculate the angle \(cos\)θ.
However, some of you might be thinking “What I really want is the angle in degrees, knowing \(cos\)θ doesn`t really help, does it?”
Next, I would like to explain the method of converting \(cos\)θ into degrees.
Calculate θ
\(cos\)θ consists of an angle part, represented by θ, so we will calculate θ.
As shown in the diagram, consider the angle of a right angled triangle where the base length is cosθ, and the length of the hypotenuse is 1…
Since it’s not practical to calculate it manually every time, let’s use the inverse function of cosine (arccos) and allow the computer to handle the computation!
If the cosine function is represented as \(y = \cos\theta\), then the inverse function of cosine is related to \(\theta = \arccos y\).
With the help of ACOS function in Excel, we can calculate θ!
Convert θ to degrees
We are able to determine θ, but there is one important point to note.
Generally, θ is expressed in radians (rad) as a unit! (Wait, what?!)
“Therefore, Excel’s ACOS function also outputs results in radians, just like other like functions.
However, please do not worry.
The radian notation simply represents 360° as \(2\pi\) (where \(\pi\) is the mathematical constant pi), making it easy to convert to degrees!
In other words, we can create a conversion formula like this!
$$\theta° = \theta\times\frac{360}{2\pi}[rad] = \theta\times\frac{180}{\pi}[rad]$$
By using this method, θ can now be converted into degrees!
However, for those using Excel, there`s a convenient function known as DEGREES function, so you don`t have to go through all these steps.
Conclusion
Have you successfully determined the joint angles?
I hope this article helps you in achieving what you want.
At our company, we engage in development utilizing various cutting-edge technologies.
We offer tailored solutions based on your specific needs. If you are unsure whether posture estimation technology can meet your needs, please do not hesitate to contact us!
https://www.next-system.com/en/contact
Bonus Tip : Method to calculate \(cos\)θ without using Vectors
For those of you wondering, ‘What’s a vector!? Is it something delicious?!?!’ Do not worry, I’ll also include a method to calculate angles from three points in a 2D plane (XY plane). (The underlying concept is almost the same.)
Using the Law of Cosines to find \(cos\)θ
There is a theorem called the Law of Cosines.
$$a^2 = b^2 + c^2 – 2bc\cos\theta$$
There are three points A, B, and C as shown in the diagram, and the sides opposite to these points (not adjacent to the points) are denoted as a, b, and c respectively.
When rearranging the Law of Cosines to solve for \(cos\)θ, it becomes as follows;
$$\cos\theta = \frac{ b^2 + c^2 – a^2 }{ 2bc }$$
The lengths of segments a, b, and c can be expressed as follows using the x and y coordinates of each point (by the Pythagorean theorem).
$$a = \sqrt{(C_{x} – B_{x})^2 + (C_{y} -B_{y})^2}$$
$$b = \sqrt{(C_{x} – A_{x})^2 + (C_{y} -A_{y})^2}$$
$$c = \sqrt{(B_{x} – A_{x})^2 + (B_{y} -A_{y})^2}$$
When incorporating this into the previous equation, you can calculate \(cos\)θ from the coordinates of the three points.
$$\cos\theta = \frac{ b^2 + c^2 – a^2 }{ 2bc }$$
$$\cos\theta = \frac{ (C_{x} – A_{x})^2 + (C_{y} -A_{y})^2 + (B_{x} – A_{x})^2 + (B_{y} -A_{y})^2 – (C_{x} – B_{x})^2 – (C_{y} -B_{y})^2 }{ 2\sqrt{(C_{x} – A_{x})^2 + (C_{y} -A_{y})^2}\sqrt{(B_{x} – A_{x})^2 + (B_{y} -A_{y})^2} }$$
It might be a bit challenging, but if you incorporate this into Excel as mentioned earlier, you can calculate \(cos\)θ !
=((AP3-AM3)^2+(AQ3-AN3)^2+(AJ3-AM3)^2+(AK3-AN3)^2-(AP3-AJ3)^2-(AQ3-AK3)^2)/(2*SQRT((AP3-AM3)^2+(AQ3-AN3)^2)*SQRT((AJ3-AM3)^2+(AK3-AN3)^2))
Next, follow the same process as before to convert \(cos\)θ into an angle!
For more information, please visit us at Twitter, Facebook, Website & Linkedin.
Table of Contents