手指数识别Python
时间: 2024-02-16 21:57:24 浏览: 216
以下是基于MediaPipe手部关键点检测实现手指数识别的Python代码:
```python
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# 初始化MediaPipe Hands模型
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
# 定义手指关键点编号
finger_tip_ids = [4, 8, 12, 16, 20]
# 打开摄像头
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
# 将图像从BGR转换为RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理图像并检测手部关键点
results = hands.process(image)
# 将图像从RGB转换为BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 绘制手部关键点和连接线
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 计算五根手指的角度
finger_angles = []
for finger_tip_id in finger_tip_ids:
finger_tip = hand_landmarks.landmark[finger_tip_id]
finger_pip = hand_landmarks.landmark[finger_tip_id - 2]
finger_mcp = hand_landmarks.landmark[finger_tip_id - 3]
finger_angle = mp_hands.angle(
finger_tip, finger_pip, finger_mcp)
finger_angles.append(finger_angle)
# 判断手势
if finger_angles[0] < 30 and all(angle > 160 for angle in finger_angles[1:]):
cv2.putText(image, "Thumb up", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif all(angle < 90 for angle in finger_angles):
cv2.putText(image, "Fist", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif all(angle > 130 for angle in finger_angles):
cv2.putText(image, "Five", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif finger_angles[0] > 160 and all(angle < 100 for angle in finger_angles[1:]):
cv2.putText(image, "Thumbs down", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(image, "Other", (20, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 显示图像
cv2.imshow('Hand Gesture Recognition', image)
# 按下q键退出
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# 释放资源
hands.close()
cap.release()
cv2.destroyAllWindows()
```
该代码使用MediaPipe库实现手部21个关键点实时检测,并根据手部关键点坐标计算五根手指的角度,通过手指角度判断手势,例程中包含数字0~9的手势。
阅读全文