I'm using Mediapipe to obtain the coordinates of points on a person's face via webcam. While recording these coordinates, I consider point 8 (the midpoint between the eyebrows) as the origin. However, when the person's face turns, the coordinate plane also rotates, causing the slope between two points to change. My code is as follows:
def calculate_2D_slope(self, point1, point2):
if point1 and point2:
# DESCRIPTION: Get data for points at indexes 8, 9.
origin_point_no_8 = next((point for point in self.origin_points_coordinates if point["index"] == 8), None)
origin_point_no_9 = next((point for point in self.origin_points_coordinates if point["index"] == 9), None)
# DESCRIPTION: Calculate the angle difference between two slopes.
if origin_point_no_8["x"] == 0 and origin_point_no_8["y"] == 0 and origin_point_no_8["z"] == 0:
origin_slope = (origin_point_no_9["y"] - origin_point_no_8["y"]) / (origin_point_no_9["x"] - origin_point_no_8["x"])
point12_slope = (point2["y"] - point1["y"]) / (point2["x"] - point1["x"])
return degrees(atan((point12_slope - origin_slope) / (1 + point12_slope * origin_slope)))
else:
print("Origin point 8 is not zero.")
return None
In this code, point 8 (the midpoint between the eyebrows) is considered as the origin. Point 9 creates a reference slope from this origin. Essentially, I use point 8 as the origin and the slope formed with point 9 to understand how the coordinate system rotates when the face turns.
Using this reference slope, I am trying to calculate the slope between the other two points according to the rotated coordinate system. During this calculation, I am using trigonometric addition and subtraction formulas to find the correct angle difference.
Question:
- Am I using the trigonometric addition and subtraction formulas correctly?
- Do you have any suggestions for other approaches or corrections?