from math import sqrt def calculate_distance(ax, ay, bx, by): distance = sqrt(((bx - ax) ** 2 + (by - ay) ** 2)) return distance def check_index_finger(hand_landmarks): return check_finger(hand_landmarks, 8, 5, 'wskazujacy') def check_middle_finger(hand_landmarks): return check_finger(hand_landmarks, 12, 9, 'srodkowy') def check_ring_finger(hand_landmarks): return check_finger(hand_landmarks, 16, 13, 'serdeczny') def check_pinky_finger(hand_landmarks): return check_finger(hand_landmarks, 20, 17, 'maly') def check_finger(hand_landmarks, top_idx, bottom_idx, name): ax = hand_landmarks.landmark[top_idx].x ay = hand_landmarks.landmark[top_idx].y bx = hand_landmarks.landmark[bottom_idx].x by = hand_landmarks.landmark[bottom_idx].y distance_top_bottom = calculate_distance(ax, ay, bx, by) ax = hand_landmarks.landmark[bottom_idx].x ay = hand_landmarks.landmark[bottom_idx].y bx = hand_landmarks.landmark[0].x by = hand_landmarks.landmark[0].y distance_bottom_start = calculate_distance(ax, ay, bx, by) if distance_bottom_start < distance_top_bottom + 0.1: result = name + '_wyprostowany' else: result = name + '_niewyprostowany' return result, hand_landmarks.landmark[top_idx].x, hand_landmarks.landmark[top_idx].y