mediapipe 手部识别

官方文档:https://google.github.io/mediapipe/solutions/hands
安装:pip install mediapipe

# pip install mediapipe
import cv2
# 导入mediapipe:https://google.github.io/mediapipe/solutions/hands
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

hands = mp_hands.Hands(
    model_complexity=0,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5)

# 读取视频流
cap = cv2.VideoCapture(0)
# 获取画面宽度、高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

while True:
    ret, frame = cap.read()

    # 镜像
    frame = cv2.flip(frame, 1)

    # 手势识别
    frame.flags.writeable = False
    # 颜色转换
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # 识别
    results = hands.process(frame)

    # 如果有结果
    if results.multi_hand_landmarks:

        # 遍历双手
        for hand_landmarks in results.multi_hand_landmarks:
            mp_drawing.draw_landmarks(
                frame,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())

            # 获取 需要的 关键点
            # 21 个关键点的x,y坐标列表
            x_list = []
            y_list = []
            for landmark in hand_landmarks.landmark:
                x_list.append(landmark.x)
                y_list.append(landmark.y)

            # 获取食指指尖坐标
            index_finger_x = int(x_list[8] * width)
            index_finger_y = int(y_list[8] * height)
            print(index_finger_x, index_finger_y)
            # 绘制圆
            cv2.circle(frame, (index_finger_x, index_finger_y), 20, (0, 0, 255), -1)

    # 显示 需改回来
    frame.flags.writeable = True
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    # 显示画面
    cv2.imshow('demo', frame)

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

mediapipe 手部识别_第1张图片

你可能感兴趣的:(趣味小项目,python,opencv,计算机视觉)