python如何实时识别摄像头中手势?
例如ok,剪刀、锤子、布、数字
网友回复
以下是使用Python、OpenCV和MediaPipe实现实时手势识别的完整代码。这个示例可以识别"OK"、"剪刀"、"握拳"、"一"、"二"、"三"等手势:
import cv2 import mediapipe as mp import math # 初始化MediaPipe mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5 ) mp_draw = mp.solutions.drawing_utils # 打开摄像头 cap = cv2.VideoCapture(0) def calculate_distance(p1, p2): """计算两点之间的距离""" return math.sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2) def recognize_gesture(hand_landmarks): """识别手势""" # 获取所有手指的关键点 thumb_tip = hand_landmarks.landmark[4] index_tip = hand_landmarks.landmark[8] middle_tip = hand_landmarks.landmark[12] ring_tip = hand_landmarks.landmark[16] pinky_tip = hand_landmarks.landmark[20] # 获取手掌基准点 wrist = hand_landmarks.landmark[0] thumb_base = hand_landmarks.landmark[2] index_base = hand_landmarks.landmark[5] middle_base = hand_landmarks.landmark[9] ring_base = hand_landmarks.landmark[13] pinky_base = hand_landmarks.landmark[17] # 计算每个手指尖到手掌的距离 thumb_dist = calculate_distance(thumb_tip, wrist) index_dist = calculate_distance(i...
点击查看剩余70%