在使用mediapipe的过程中,发现
mp.solutions.drawing_utils中的plot_landmarks函数不能实时显示骨骼点映射到3d坐标系的连续视频。在github中找到很好的解决办法。stebusse/mediapipe-plot-pose-live:这个小脚本可让您在整洁的3D图表中绘制来自MediaPipe的姿势世界地标。 (github.com)新增plot_pose_live.py文件代码如下:
import matplotlib.pyplot as plt
# connections for the MediaPipe topology
LANDMARK_GROUPS = [
[8, 6, 5, 4, 0, 1, 2, 3, 7], # eyes
[10, 9], # mouth
[11, 13, 15, 17, 19, 15, 21], # right arm
[11, 23, 25, 27, 29, 31, 27], # right body side
[12, 14, 16, 18, 20, 16, 22], # left arm
[12, 24, 26, 28, 30, 32, 28], # left body side
[11, 12], # shoulder
[23, 24], # waist
]
def plot_world_landmarks(ax, landmarks, landmark_groups=LANDMARK_GROUPS):
"""_summary_
Args:
ax: plot axes
landmarks mediapipe
"""
# skip when no landmarks are detected
if landmarks is None:
return
ax.cla()
# had to flip the z axis
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(1, -1)
# get coordinates for each group and plot
for group in landmark_groups:
plotX, plotY, plotZ = [], [], []
plotX = [landmarks.landmark[i].x for i in group]
plotY = [landmarks.landmark[i].y for i in group]
plotZ = [landmarks.landmark[i].z for i in group]
# this can be changed according to your camera
ax.plot(plotX, plotZ, plotY)
plt.pause(.001)
return
实时显示的example示例,可使用摄像头实时显示,也可以选择视频:
import mediapipe as mp
import cv2
import plot_pose_live
import matplotlib.pyplot as plt
# setup plot
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
# setup mediapipe
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
# open webcam
video_path ='your_video.mp4'
# cap = cv2.VideoCapture(1) # change index
cap = cv2.VideoCapture(video_path)
with mp_pose.Pose(
min_tracking_confidence=0.5,
min_detection_confidence=0.5,
model_complexity=1,
smooth_landmarks=True,
) as pose:
while cap.isOpened():
# read webcam image
success, image = cap.read()
# skip empty frames
if not success:
continue
# calculate pose
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# draw 3D pose landmarks live
plot_pose_live.plot_world_landmarks(ax, results.pose_world_landmarks)
# draw image
cv2.imshow("MediaPipePose", cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
输入视频结果显示: