OpenCV实战-手势虚拟拖拽

'''
步骤:
1.opencv获取视频流
2.在画面上画一个方块
3.通过mediapipe获取手指关键点坐标
4.判断手指是否在方块上
5.如果在方块上,方块跟着手指移动
'''
#导入opencv
import cv2
import numpy as np
import time
import math

#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(0)
start = time.time()
  
#获取画面宽度和高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

square_x = 100
square_y = 100
square_width = 100
square_height = 100
on_square = False

while True:
    #获取图像
    ret, frame = cap.read()
    #显示图像
    if ret:
        frame = cv2.flip(frame, 1)
        frame.flags.writeable=False
        img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        result = hands.process(img)
        frame.flags.writeable=True
        fame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        #判断是否出现双手
        if result.multi_hand_landmarks:
            #解析遍历双手
            for hand_landmarks in result.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个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(width * x_list[8])
            index_finger_y = int(height * y_list[8])

            #获取中指指尖
            index_zhongzhi_x = int(width * x_list[12])
            index_zhongzhi_y = int(height * y_list[12])

            #判断食指指尖在不在方块上
            distance = math.hypot((index_finger_x-index_zhongzhi_x),(index_finger_y-index_zhongzhi_y))
            print(distance)

            if distance < 60:
                if (index_finger_x > square_x) and (index_finger_x < (square_x+square_width)) and (index_finger_y >square_y) and(index_finger_y < (square_y+square_height)):
                    if on_square == False:
                        L1 = abs(index_finger_x - square_x)
                        L2 = abs(index_finger_y - square_y)
                        on_square = True

                        #print('在方块上')
                        #on_square = True
                        #on_square = False  
                    # new_square_x = index_finger_x - L1
                    # new_square_y = index_finger_y - L2
                    # cv2.rectangle(frame, (new_square_x, new_square_y), (new_square_x+square_width, new_square_y+square_height), (255, 0, 255), -1)

                else:
                    #on_square = False
                    pass
            else:
                on_square = False
            if on_square:
                square_x = index_finger_x - L1
                square_y = index_finger_y - L2
                #cv2.rectangle(frame, (new_square_x, new_square_y), (new_square_x+square_width, new_square_y+square_height), (255, 0, 255), -1)
        frame = cv2.rectangle(frame, (square_x, square_y), (square_x+square_width, square_y+square_height), (255, 0, 255), -1)
        #frame = cv2.circle(frame, (400, 400), 20, (255, 0, 0), -1)
        now = time.time()
        fps = int(1/(now - start))
        start = now
        
        cv2.putText(frame, 'fps:'+str(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.imshow('Demo', frame)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    else:
        break

cap.release()
cv2.destroyAllWindows()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值