# 修改该函数
def plot_landmarks(landmark_list: landmark_pb2.NormalizedLandmarkList,
connections: Optional[List[Tuple[int, int]]] = None,
landmark_drawing_spec: DrawingSpec = DrawingSpec(
color=RED_COLOR, thickness=5),
connection_drawing_spec: DrawingSpec = DrawingSpec(
color=BLACK_COLOR, thickness=5),
elevation: int = 10,
azimuth: int = 10):
"""Plot the landmarks and the connections in matplotlib 3d.
Args:
landmark_list: A normalized landmark list proto message to be plotted.
connections: A list of landmark index tuples that specifies how landmarks to
be connected.
landmark_drawing_spec: A DrawingSpec object that specifies the landmarks'
drawing settings such as color and line thickness.
connection_drawing_spec: A DrawingSpec object that specifies the
connections' drawing settings such as color and line thickness.
elevation: The elevation from which to view the plot.
azimuth: the azimuth angle to rotate the plot.
Raises:
ValueError: If any connetions contain invalid landmark index.
"""
if not landmark_list:
return
plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')
ax.view_init(elev=elevation, azim=azimuth)
plotted_landmarks = {}
for idx, landmark in enumerate(landmark_list.landmark):
if ((landmark.HasField('visibility') and
landmark.visibility < _VISIBILITY_THRESHOLD) or
(landmark.HasField('presence') and
landmark.presence < _PRESENCE_THRESHOLD)):
continue
ax.scatter3D(
xs=[-landmark.z],
ys=[landmark.x],
zs=[-landmark.y],
color=_normalize_color(landmark_drawing_spec.color[::-1]),
linewidth=landmark_drawing_spec.thickness)
plotted_landmarks[idx] = (-landmark.z, landmark.x, -landmark.y)
if connections:
num_landmarks = len(landmark_list.landmark)
# Draws the connections if the start and end landmarks are both visible.
for connection in connections:
start_idx = connection[0]
end_idx = connection[1]
if not (0 <= start_idx < num_landmarks and 0 <= end_idx < num_landmarks):
raise ValueError(f'Landmark index is out of range. Invalid connection '
f'from landmark #{start_idx} to landmark #{end_idx}.')
if start_idx in plotted_landmarks and end_idx in plotted_landmarks:
landmark_pair = [
plotted_landmarks[start_idx], plotted_landmarks[end_idx]
]
ax.plot3D(
xs=[landmark_pair[0][0], landmark_pair[1][0]],
ys=[landmark_pair[0][1], landmark_pair[1][1]],
zs=[landmark_pair[0][2], landmark_pair[1][2]],
color=_normalize_color(
connection_drawing_spec.color[::-1]),
linewidth=connection_drawing_spec.thickness)
##########🚀🚀🚀############
import PIL.Image as Image
# 将plt转化为numpy数据
canvas = FigureCanvasAgg(plt.gcf())
# 绘制图像
canvas.draw()
# 获取图像尺寸
w, h = canvas.get_width_height()
# 解码string 得到argb图像
buf = np.fromstring(canvas.tostring_argb(), dtype=np.uint8)
# 重构成w h 4(argb)图像
buf.shape = (w, h, 4)
# 转换为 RGBA
buf = np.roll(buf, 3, axis=2)
# 得到 Image RGBA图像对象 (需要Image对象的同学到此为止就可以了)
image = Image.frombytes("RGBA", (w, h), buf.tostring())
# 转换为numpy array rgba四通道数组
image = np.asarray(image)
# 转换为rgb图像
rgb_image = image[:, :, :3]
# print(rgb_image)
return cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
# cv2.imshow("res",rgb_image)
##########🚀🚀🚀############
# plt.show()
修改mediapipe中绘制pose的三维坐标函数,将PLT格式转成CV2格式,实时显示
最新推荐文章于 2023-09-05 17:15:37 发布