mediapipe手势识别示例代码
时间: 2023-07-26 20:03:26 浏览: 234
易实现的手势识别源代码
以下是一个简单的 Mediapipe 手势识别示例代码,需要安装 Mediapipe 和 OpenCV 库:
```python
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
with mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Ignoring empty camera frame.")
continue
frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
frame.flags.writeable = False
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)
# 获取手指关节点坐标
finger_tips = []
for landmark in hand_landmarks.landmark:
if landmark.visibility < 0 or landmark.presence < 0:
continue
x, y = int(landmark.x * frame.shape[1]), int(landmark.y * frame.shape[0])
if landmark.id in [8, 12, 16, 20]:
finger_tips.append((x, y))
# 检测手势
if len(finger_tips) == 4:
cv2.putText(frame, "OK", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
elif len(finger_tips) == 0:
cv2.putText(frame, "Fist", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(frame, "Unknown", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow('MediaPipe Hands', frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
这个示例代码可以实时从摄像头读取图像,使用 Mediapipe 进行手部关键点检测,并根据手指关节点坐标判断手势类型。在检测到 OK 手势时,在视频中显示“OK”,在检测到拳头时,在视频中显示“Fist”。
阅读全文