应用场景:ai换脸,根据左右手势选择图片,上下则表示选中。
版本号:python3.7(一开始是3.6,但是mediapipe最低就是3.7
因为网上检测的都不太准,所以我在判断的时候加入了如果70次里55次检测的是左才返回左,测试完之后效果还可以,蛮准的。判断方法想的头都要秃了。文章来源:https://www.toymoban.com/news/detail-847290.html
def gesture3(cap, gesture_buffer_size=70, gesture_threshold=55):
direction = None
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# 初始化手部检测模型
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5)
gesture_buffer = deque(maxlen=gesture_buffer_size) # 用于存储最近的几次手势识别结果
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
# 将图像转换为RGB格式
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# 执行手部检测
results = hands.process(image)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# 在图像上绘制手部标记
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2, circle_radius=2)
)
# 获取手部关键点坐标
landmarks = hand_landmarks.landmark
# 获取手掌位置(关键点索引为0)
palm_landmark = hand_landmarks.landmark[0]
palm_x = palm_landmark.x
palm_y = palm_landmark.y
# 获取除大拇指之外的其他手指的指尖位置
index_tip = hand_landmarks.landmark[mp.solutions.hands.HandLandmark.INDEX_FINGER_TIP]
middle_tip = hand_landmarks.landmark[mp.solutions.hands.HandLandmark.MIDDLE_FINGER_TIP]
ring_tip = hand_landmarks.landmark[mp.solutions.hands.HandLandmark.RING_FINGER_TIP]
pinky_tip = hand_landmarks.landmark[mp.solutions.hands.HandLandmark.PINKY_TIP]
# 计算其他手指指尖到手掌位置的距离
index_distance = math.sqrt((index_tip.x - palm_x) ** 2 + (index_tip.y - palm_y) ** 2)
middle_distance = math.sqrt((middle_tip.x - palm_x) ** 2 + (middle_tip.y - palm_y) ** 2)
ring_distance = math.sqrt((ring_tip.x - palm_x) ** 2 + (ring_tip.y - palm_y) ** 2)
pinky_distance = math.sqrt((pinky_tip.x - palm_x) ** 2 + (pinky_tip.y - palm_y) ** 2)
# 假设第二个关键点代表食指指尖,第三个关键点代表拇指指尖
index_finger_tip = landmarks[8]
# 获取拇指 MCP (第一关节)和 TIP(指尖)的坐标
thumb_mcp = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_MCP]
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
# 获取手腕的坐标
wrist = landmarks[0]
# 计算食指和拇指指尖之间的距离
distance = math.sqrt((thumb_tip.x - index_finger_tip.x)**2 + (thumb_tip.y - index_finger_tip.y)**2 + (thumb_tip.z - index_finger_tip.z)**2)
# 计算手指和手掌在X轴和Y轴上的距离
x_distance = index_tip.x - palm_x
y_distance = index_tip.y - palm_y
if abs(x_distance) > abs(y_distance):
# 当手腕在食指的左边,并且食指和拇指指尖之间的距离较大时,判断为向左的手势。 判断手心是否超前,以及食指和拇指指尖的距离来确定手势
if index_tip.x < palm_x and middle_tip.x < palm_x and ring_tip.x < palm_x and pinky_tip.x < palm_x and wrist.x > index_finger_tip.x and distance > 0.2 and index_finger_tip.x < thumb_tip.x - 0.05: # 假设手心超前且食指和拇指指尖的距离大于0.1
print('Left')
gesture_buffer.append("Left")
elif index_tip.x > palm_x and middle_tip.x > palm_x and ring_tip.x > palm_x and pinky_tip.x > palm_x and wrist.x < index_finger_tip.x and distance > 0.1 and index_finger_tip.x > thumb_tip.x + 0.05: # 假设手心超前且食指和拇指指尖的距离大于0.1
print('Right')
gesture_buffer.append("Right")
else:
# 当手腕在食指的上方,并且食指和拇指指尖之间的距离较大时,判断为向上的手势
if index_tip.y < palm_y and middle_tip.y < palm_y and ring_tip.y < palm_y and pinky_tip.y < palm_y and wrist.y > index_finger_tip.y and distance > 0.2: # 假设手心超前且食指和拇指指尖的距离大于0.1
print('Up')
gesture_buffer.append("Up")
# 这里我不用下了,如果你们需要直接把注释去掉就可以。当手腕在食指的下方,并且食指和拇指指尖之间的距离较大时,判断为向下的手势
# elif index_tip.y > palm_y and middle_tip.y > palm_y and ring_tip.y > palm_y and pinky_tip.y > palm_y and wrist.y < index_finger_tip.y and distance > 0.1: # 假设手心超前且食指和拇指指尖的距离大于0.1
# print('Down')
# gesture_buffer.append("Down")
# 统计手势识别结果
gesture_counts = {gesture: gesture_buffer.count(gesture) for gesture in set(gesture_buffer)}
# 判断最终手势
if len(gesture_buffer) == gesture_buffer_size and max(gesture_counts.values()) >= gesture_threshold:
direction = max(gesture_counts, key=gesture_counts.get)
cv2.imshow('Hand Gestures', cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
if direction or cv2.waitKey(5) & 0xFF == 27: # 按ESC键退出
break
return direction
实现效果如下:文章来源地址https://www.toymoban.com/news/detail-847290.html
到了这里,关于python+opencv+mediapipe实现手势检测上下左右(含完整代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!