#๐Ÿ”’ Am I Using the Right Approach to Calculate Slope Differences in a Rotated Coordinate System?

6 messages ยท Page 1 of 1 (latest)

muted flume
#

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?
undone nimbusBOT
#

@muted flume

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

muted flume
stray moat
#

Your current setup doesn't use the trigonometric addition or subtraction formulas directly. Instead, you're using the arctangent of the difference quotient, which is related but not quite the same. Instead of dealing with slopes, you could use a rotation matrix to change the coordinates. This would be more precise and handle all rotation angles. Another option is to think of the points as vectors and use vector operations to calculate angles and rotations.

undone nimbusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.