1 准备工作

1.1 安装Baze

(1)下载Bazelisk

MediaPipe使用bazel进行构建的,安装bazellisk主要是为了更新bazel

进入bazel的项目https://github.com/bazelbuild/bazelisk/releases,下载二进制文件bazelisk-linux-amd64

然后将文件移动到/usr/local/bin/bazel/,并修改其可执行权限

1
2
sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel
sudo chmod u+x /usr/local/bin/bazel

检查bazel是否安装成功

1
bazel version

可以查看到bazel的版本就算成功。

(2)安装Bazel

从 GitHub 上的 Bazel 版本页面下载名为 bazel-version-installer-linux-x86_64.sh的shell脚本文件。

例如我本次安装的就是bazel-6.3.2-installer-linux-x86_64.sh

执行如下命令:

1
2
3
4
# 给bazel-6.3.2-installer-linux-x86_64.sh脚本可执行权限
chmod +x bazel-6.3.2-installer-linux-x86_64.sh
# 下载bazel
./bazel-6.3.2-installer-linux-x86_64.sh --user

根据提示,将 source /home/mahaofei/bin/bazel/bin/bazel-complete.bash 添加到 ~/.bashrc 中:

1
echo 'source /home/mahaofei/.bazel/bin/bazel-complete.bash' >> ~/.bashrc

重启终端,使用bazel --version命令检查是否安装成功。

1.2 下载Mediapipe

(1)克隆项目

1
git clone [email protected]:google/mediapipe.git

(2)安装opencv和ffmpeg

进入克隆的项目中,为setup_opencv.sh添加可执行权限,并运行

1
2
chmod +x setup_opencv.sh
./setup_opencv.sh

2 使用

(1)python环境

创建虚拟环境,并安装必要的包

1
2
3
4
5
conda create -n mediapipe python=3.8
conda activate mediapipe
pip install opencv-python
pip install opencv-contrib-python
pip install mediapipe

(2)使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic

cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue

image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = holistic.process(image)
#画图
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image,
results.face_landmarks,
mp_holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style())
mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
mp_holistic.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles
.get_default_pose_landmarks_style())

mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)

#右手21个节点坐标
if results.right_hand_landmarks:
for index, landmarks in enumerate(results.right_hand_landmarks.landmark):
print(index,landmarks )
#鼻子坐标
#print(results.pose_landmarks.landmark[mp_holistic.PoseLandmark.NOSE])
cv2.imshow('MediaPipe Holistic', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()

image.png