最近在学习计算机视觉相关,打算写一个系列的实战项目,便于自己学习并加深记忆。
废话不多说,直接先上成果图!!

环境配置

pip install opencv-python
pip install mediapipe

利用mediapipe进行手指关节点检测

算法分析

跟随移动算法.png
当手指进入虚拟色块内部时,设坐标为(a,b),再根据色块左上角坐标X、Y即可计算出L1、L2,无论手指如何移动,L1、L2始终保持不变,只需要得到手指的新坐标(A,B),即可计算出X1与Y1与其对角线坐标。
话不多说,直接上代码

medeapipe中获取手指坐标的下表参考下图

hand_landmarks

import cv2
import numpy as np
import mediapipe as mp
import math
cap = cv2.VideoCapture(1)
# 获取画面宽度与高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 固定参数
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
)

# 方块相关参数
square_x = 100
square_y = 100
square_width = 100
square_color = (255,0,0)

L1 = 0
L2 = 0
# 判断中指与食指是否津贴
on_squre = False
while True:
    # 获取帧
    rec,frame = cap.read()
    # 镜像翻转
    frame = cv2.flip(frame,1)
    # mediapipe处理
    frame.flags.writeable = False
    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    results = hands.process(frame)

    frame.flags.writeable = True
    frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR)

    # 是否出现手
    if results.multi_hand_landmarks:
        # 解析手
        for hand_landmarks in results.multi_hand_landmarks :
            # 绘制21个关节点
            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个xy坐标
            x_list = []
            y_list = []

            for hand_landmark in hand_landmarks.landmark:
                # 添加xy坐标
                x_list.append(hand_landmark.x)
                y_list.append(hand_landmark.y)
            
            # 获取食指指尖
            index_finger_x = int(x_list[8] * width)
            index_finger_y = int(y_list[8] * height)

            # 获取中指坐标
            middle_finger_x = int(x_list[12] * width)
            middle_finger_y = int(y_list[12] * height)

            # 计算食指与中指距离
            finger_len = math.hypot((index_finger_x-middle_finger_x),(index_finger_y-middle_finger_y))

            if finger_len < 50:

                # 判断食指指尖是否在方块内
                if index_finger_x > square_x and index_finger_x < square_width  + square_x  and index_finger_y > square_y and index_finger_y < square_width  + square_y:
                    if on_squre == False :
                        print('在方块上')
                        L1 = abs(index_finger_x - square_x)
                        L2 = abs(index_finger_y - square_y)
                        on_squre = True
                        square_color = (255,0,255)
                else:
                    print('不在方块上')

                if on_squre:
                    square_x = index_finger_x - L1
                    square_y = index_finger_y - L2
            else:
                on_squre = False
                square_color = (255,0,0)
    overlay = frame.copy()
    cv2.rectangle(frame,(square_x,square_y),(square_x+square_width,square_y+square_width),
    square_color,-1)
    frame = cv2.addWeighted(overlay,0.5,frame,0.5,0)
    cv2.imshow('Demo',frame)

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
最后修改:2023 年 01 月 26 日
如果觉得我的文章对你有用,请随意赞赏