demo!!成功!

发布时间 2023-06-14 00:37:55作者: 沉梦昂志_doc

最近一直在弄mediapipe的手指关节,之前很多bug,通过不断的修改和完善,慢慢摸索出来了!

终于在2023年6月14日 0:30,完成了第一次demo演示的成功!

 真的超级开心!

贴一下代码(视频流处需要修改路径)(需要安装mediapipe包和opencv包):

"""
author:keith
date: 2023-5-5
步骤:
1、opencv获取视频流
2、在画面上画一个方块
3、通过mediapipe获取手指关键点坐标
4、判断手指是否在方块上
5、如果在方块上,方块跟着手指移动
"""


# 导入opencv
import cv2
import numpy as np
#mediapipe 相关参数
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('D:\masterResearch\demo\qq.mp4')

#方块相关参数
square_x = 100
square_y = 100
square_witdh = 100


while True:
    
    # 读取每一帧
    ret,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:
            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()
            )


    #画一个方块
    cv2.rectangle(frame,(square_x,square_y),(square_x+square_witdh,square_y+square_witdh),(255,0,0),-1)

    # 显示
    cv2.imshow('Virtual drag',frame)

    # 退出条件
    if cv2.waitKey(10) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

再接再厉!