MediaPipe是用于构建应用ML管道的多模式(例如,视频,音频,任何时间序列数据)的框架。利用MediaPipe,可以将感知管道构建为模块化组件的图形,包括例如推理模型(例如,TensorFlow,TFLite)和媒体处理功能。
有关手部检测和手部跟踪的整体背景,请阅读此 Google AI博客文章。
Google AI博客文章
MediaPipe说明文档
实现步骤参考链接
为了方便配置各种环境,我安装了anaconda软件,然后使用anaconda安装了python3.8、opencv3.4以上版本和mediapine包等
配置环境参考链接
配置环境需要自行配置,网上有各种流程。
当配置好python、opencv之后
然后pip install mediapipe安装mediapipe包
import cv2 import mediapipe as mp 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.75, min_tracking_confidence=0.75) cap = cv2.VideoCapture(0) while True: ret,frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 因为摄像头是镜像的,所以将摄像头水平翻转 # 不是镜像的可以不翻转 frame= cv2.flip(frame,1) results = hands.process(frame) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) if results.multi_handedness: for hand_label in results.multi_handedness: print(hand_label) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: print('hand_landmarks:' hand_landmarks) # 关键点可视化 mp_drawing.draw_landmarks( frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) cv2.imshow('MediaPipe Hands', frame) if cv2.waitKey(1) & 0xFF == 27: break cap.release()
效果如图所示:
然后通过对检测到的手部关键点之间的角度计算便可得知手指是否弯曲,然后便可以实现简单的手势识别,同时也实现了手势方向(上下左右)判断,代码随后分享,谢谢观看。