File size: 1,413 Bytes
d523ed2
 
 
 
19f60ef
d523ed2
 
 
19f60ef
d523ed2
 
 
 
 
 
 
 
 
 
 
19f60ef
d523ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# utils.py
import math

def distance_between_points(p1, p2):
    # euclidean distance between two 3d points
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2)

def angle_between_points(a, b, c):
    # returns the angle ABC (in degrees) where B is the vertex
    ab = (a[0]-b[0], a[1]-b[1], a[2]-b[2])
    cb = (c[0]-b[0], c[1]-b[1], c[2]-b[2])
    dot_product = ab[0]*cb[0] + ab[1]*cb[1] + ab[2]*cb[2]
    mag_ab = math.sqrt(ab[0]**2 + ab[1]**2 + ab[2]**2)
    mag_cb = math.sqrt(cb[0]**2 + cb[1]**2 + cb[2]**2)
    if mag_ab * mag_cb == 0:
        return 0
    angle_rad = math.acos(dot_product / (mag_ab * mag_cb))
    return math.degrees(angle_rad)

def is_finger_extended(landmarks, finger, direction=None):
    # checks if a specific finger is extended based on joint angles
    finger_joints = {
        "thumb": [1, 2, 3, 4],
        "index": [5, 6, 7, 8],
        "middle": [9, 10, 11, 12],
        "ring": [13, 14, 15, 16],
        "pinky": [17, 18, 19, 20]
    }
    joints = finger_joints[finger]
    angle = angle_between_points(landmarks[joints[0]], landmarks[joints[1]], landmarks[joints[2]])
    extended = angle > 160  # straight finger
    if direction == "up":
        return extended and landmarks[joints[-1]][1] < landmarks[joints[0]][1]
    elif direction == "down":
        return extended and landmarks[joints[-1]][1] > landmarks[joints[0]][1]
    return extended