预备知识
勾股定理
跟随移动算法
手势识别图解
项目源代码
"""
演示一个简单的虚拟拖拽
步骤:
1、opencv 读取视频流
2、在视频图像上画一个方块
3、通过mediapipe库获取手指关节坐标
4、判断手指是否在方块上
5、是,方块跟着移动
6、完善:通过食指和中指指尖距离确定是否激活移动
7、完善:画面显示FPS等信息
"""
import cv2
import math#导入mediapipe的相关模块
# 导入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.handshands = mp_hands.Hands(model_complexity=0,min_detection_confidence=0.5,min_tracking_confidence=0.5)#读取视频流
cap = cv2.VideoCapture(0) #调用摄像头#获取画面的长度和宽度
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"width = {width}\n")
print(f"height = {height}")
#方块初始化数组
x = 100
y = 100
w = 200
h = 200on_square = False
square_color = (0,255,0)while True:ret,frame = cap.read()#读取视频帧if ret:#镜像frame = cv2.flip(frame,1)frame.flags.writeable = Falseframe = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)#识别results = hands.process(frame)frame.flags.writeable = Trueframe = 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())# 使用这两句看一下里面到底是什么?# print(type(hand_landmarks))# print(hand_landmarks)# exit()#21个关键点的x、y坐标列表x_list = []y_list = []for landmarks in hand_landmarks.landmark:x_list.append(landmarks.x)y_list.append(landmarks.y)#输出一下长度# print(len(x_list))# 获取食指指尖坐标,坐标位置查看:https://google.github.io/mediapipe/solutions/handsindex_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_distance =math.sqrt( (middle_finger_x - index_finger_x)**2 + (middle_finger_y-index_finger_y)**2)finger_distance = math.hypot((middle_finger_x - index_finger_x),(middle_finger_y - index_finger_y))# 看一下距离# print(finger_distance)#把食指画出来cv2.circle(frame,(index_finger_x,index_finger_y),20,(0,0,255),-1)#判断食指指尖在不在方块上if finger_distance < 60:# X坐标范围 Y坐标范围if (index_finger_x > x and index_finger_x < (x+w)) and (index_finger_y > y and index_finger_y < (y+h)):if on_square == False:print('在')L1 = index_finger_x - xL2 = index_finger_y - ysquare_color = (255,0,255)on_square = Trueelse:print("不在")else:#解除on_square = Falsesquare_color = (0,255,0)#更新坐标if on_square:x = index_finger_x - L1y = index_finger_y - L2#在视频中画一个方块# cv2.rectangle(frame,(x,y),(x+w,y+h),square_color,-1)#半透明处理overlay = frame.copy()cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0)#展示画面cv2.imshow("demo",frame)#退出条件if cv2.waitKey(10) & 0xFF == ord('q'):break