前言
Mediapipe是google的一个开源项目,可以提供开源的、跨平台的常用机器学习方案。
mediapipe开发包集成了人脸、眼睛、虹膜、手势、姿态等各种landmark检测与跟踪算法,开源地址:https://google.github.io/mediapipe/
本项目中使用python版本的mediapipe来实现手势识别。
1 环境搭建
参考官方文档安装基于树莓派的mediapipe:https://github.com/googlesamples/mediapipe/tree/main/examples/gesture_recognizer/raspberry_pi
下面对安装方法作简单总结(可以不去看官方推荐流程)。
1.1 使用前提
- 必须为64位的系统(最好更新到Buster),但是我没有使用Buster,而是用了Debain11的树莓派(bullseye)
- 能监控到树莓派上摄像头采集的画面(桌面,虚拟桌面或者相机数据流)
1.2 安装
- 更新pip
python3 -m pip install pip
pip可以使用清华源,速度更快,配置方法参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
- 安装opencv,mediapipe依赖opencv,所以需要先安装opencv
sudo apt install python3-opencv
sudo pip install opencv-contrib-python
sudo apt-get install -y \
libopencv-core-dev \
libopencv-highgui-dev \
libopencv-calib3d-dev \
libopencv-features2d-dev \
libopencv-imgproc-dev \
libopencv-video-dev
- 安装mediapipe
sudo python3 -m pip install mediapipe
2 介绍
手势识别之后会检测出来21个点,每个点分别表示了手中的关键部位,可以通过对关键部位的分析出具体的手势,如下:
3 功能验证
新建一个py文件,输入如下内容,能够运行成功,则表示mediapipe安装成功并且可以运行:
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():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
image.flags.writeable = True
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)
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(1) == 27:
break
cap.release()
树莓派运行mediapipe还是有点勉强,有些卡顿
4 运行结果
运行结果如下,表示成功,如果运行不成功,则根据提示安装必要工具或者库文件: