import cv2 import mediapipe as mp import mathdef vector_2d_angle(v1,v2):&#39;&#39;&#39;求解二维向量的角度&#39;&#39;&#39;v1_x&#61;v1[0]v1_y&#61;v1[1]v2_x&#61;v2[0]v2_y&#61;v2[1]try:angle_&#61; math.degrees(math.acos((v1_x*v2_x&#43;v1_y*v2_y)/(((v1_x**2&#43;v1_y**2)**0.5)*((v2_x**2&#43;v2_y**2)**0.5))))except:angle_ &#61;65535.if angle_ >180.:angle_ &#61;65535.return angle_ def hand_angle(hand_):&#39;&#39;&#39;获取对应手相关向量的二维角度,根据角度确定手势&#39;&#39;&#39;angle_list &#61;[]#---------------------------- thumb 大拇指角度angle_ &#61; vector_2d_angle(((int(hand_[0][0])- int(hand_[2][0])),(int(hand_[0][1])-int(hand_[2][1]))),((int(hand_[3][0])- int(hand_[4][0])),(int(hand_[3][1])- int(hand_[4][1]))))angle_list.append(angle_)#---------------------------- index 食指角度angle_ &#61; vector_2d_angle(((int(hand_[0][0])-int(hand_[6][0])),(int(hand_[0][1])- int(hand_[6][1]))),((int(hand_[7][0])- int(hand_[8][0])),(int(hand_[7][1])- int(hand_[8][1]))))angle_list.append(angle_)#---------------------------- middle 中指角度angle_ &#61; vector_2d_angle(((int(hand_[0][0])- int(hand_[10][0])),(int(hand_[0][1])- int(hand_[10][1]))),((int(hand_[11][0])- int(hand_[12][0])),(int(hand_[11][1])- int(hand_[12][1]))))angle_list.append(angle_)#---------------------------- ring 无名指角度angle_ &#61; vector_2d_angle(((int(hand_[0][0])- int(hand_[14][0])),(int(hand_[0][1])- int(hand_[14][1]))),((int(hand_[15][0])- int(hand_[16][0])),(int(hand_[15][1])- int(hand_[16][1]))))angle_list.append(angle_)#---------------------------- pink 小拇指角度angle_ &#61; vector_2d_angle(((int(hand_[0][0])- int(hand_[18][0])),(int(hand_[0][1])- int(hand_[18][1]))),((int(hand_[19][0])- int(hand_[20][0])),(int(hand_[19][1])- int(hand_[20][1]))))angle_list.append(angle_)return angle_listdef h_gesture(angle_list):&#39;&#39;&#39;# 二维约束的方法定义手势# fist five gun love one six three thumbup yeah&#39;&#39;&#39;thr_angle &#61;65.thr_angle_thumb &#61;53.thr_angle_s &#61;49.gesture_str &#61; Noneif65535. not in angle_list:if(angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):gesture_str &#61;"fist"elif(angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):gesture_str &#61;"five"elif(angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):gesture_str &#61;"gun"elif(angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):gesture_str &#61;"love"elif(angle_list[0]>5) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):gesture_str &#61;"one"elif(angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):gesture_str &#61;"six"elif(angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]>thr_angle):gesture_str &#61;"three"elif(angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):gesture_str &#61;"thumbUp"elif(angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):gesture_str &#61;"two"return gesture_strdef detect():mp_drawing &#61; mp.solutions.drawing_utilsmp_hands &#61; mp.solutions.handshands &#61; mp_hands.Hands(static_image_mode&#61;False,max_num_hands&#61;1,min_detection_confidence&#61;0.75,min_tracking_confidence&#61;0.75)cap &#61; cv2.VideoCapture(0)while True:ret,frame &#61; cap.read()frame &#61; cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)frame&#61; cv2.flip(frame,1)results &#61; hands.process(frame)frame &#61; cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)if results.multi_hand_landmarks:forhand_landmarksin results.multi_hand_landmarks:mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)hand_local &#61;[]foriin range(21):x &#61; hand_landmarks.landmark[i].x*frame.shape[1]y &#61; hand_landmarks.landmark[i].y*frame.shape[0]hand_local.append((x,y))if hand_local:angle_list &#61; hand_angle(hand_local)gesture_str &#61; h_gesture(angle_list)cv2.putText(frame,gesture_str,(0,100),0,1.3,(0,0,255),3)cv2.imshow(&#39;MediaPipe Hands&#39;, frame)if cv2.waitKey(1)& 0xFF &#61;&#61;27:breakcap.release()if __name__ &#61;&#61;&#39;__main__&#39;:detect()