diff --git a/gesture_control/special_gestures.py b/gesture_control/special_gestures.py index 847d18a..596e0a1 100644 --- a/gesture_control/special_gestures.py +++ b/gesture_control/special_gestures.py @@ -1,6 +1,5 @@ import numpy as np - class SpecialGestureDetector: def __init__(self, mode='geometric', model_path=None, class_names=None): self.mode = mode @@ -55,7 +54,7 @@ class SpecialGestureDetector: l_hip = landmarks[idx['left_hip']][:2] r_hip = landmarks[idx['right_hip']][:2] nose = np.array(landmarks[idx['nose']][:2]) - + shoulder_center_y = (l_sh[1] + r_sh[1]) / 2 hip_center_y = (l_hip[1] + r_hip[1]) / 2 torso_height = hip_center_y - shoulder_center_y @@ -75,11 +74,30 @@ class SpecialGestureDetector: v2 = wrist - elbow return angle_between_vectors(v1, v2) + def segments_intersect(p1, p2, p3, p4): + def cross(o, a, b): + return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o [1])* (b[0] - o[0]) + d1 = cross(p3, p4, p1) + d2 = cross(p3, p4, p2) + d3 = cross(p1, p2, p3) + d4 = cross(p1, p2, p4) + return (d1 * d2 < 0) and (d3 * d4 < 0) + + def line_intersection(p1, p2, p3, p4): + d1 = p2 - p1 + d2 = p4 -p3 + denom = d1[0] * d2[1] - d1[1] * d2[0] + if abs(denom) < 1e-6: + return None + t = ((p3[0] - p1[0]) * d2[1] - (p3[1] - p1[1]) * d2[0]) / denom + return p1 + t * d1 + # ---- Вычисляем углы ---- l_angle = elbow_angle(l_sh, l_el, l_wr) # угол в левом локте r_angle = elbow_angle(r_sh, r_el, r_wr) # угол в правом локте # ---- КРЕСТ ---- + ''' # 1. Оба локтя сильно согнуты (< 100°) elbows_bent = (l_angle < 100 and r_angle < 100) # 2. Левое запястье правее правого (перекрест) @@ -91,6 +109,15 @@ class SpecialGestureDetector: ) cross = elbows_bent and wrists_crossed and wrists_at_chest + if cross: + return 'cross' + ''' + forearms_cross = segments_intersect(l_el, l_wr, r_el, r_wr) + intersection = line_intersection(l_el, l_wr, r_el, r_wr) + intersection_on_chest = False + if intersection is not None: + intersection_on_chest = (shoulder_center_y - 0.2 * torso_height < intersection[1] < hip_center_y + 0.2 * torso_height) + cross = forearms_cross and intersection_on_chest if cross: return 'cross' diff --git a/install_deps.sh b/install_deps.sh index 56b3bbd..25deb0f 100644 --- a/install_deps.sh +++ b/install_deps.sh @@ -6,6 +6,7 @@ pip install --upgrade pip pip install numpy==1.24.3 pip install pandas==2.0.3 +pip install pyyaml==6.0.3 pip install opencv-python==4.12.0.88 pip install opencv-python-headless==4.12.0.88 pip install matplotlib==3.7.5