Ghu*_*lam 3 python machine-learning computer-vision mediapipe
我已经在 Windows 11 上使用 Python (3.7.0) 安装了 Mediapipe (0.9.0.1)。我已经能够成功让 Mediapipe 生成地标(用于面部和身体);用于图像、视频和网络摄像头流。
我现在想让 Mediapipe仅绘制身体特定的标志(即排除面部标志)。
我知道我可以使用 OpenCV (或 Czone)来实现这个目标,但是,我希望使用 Mediapipe (即使用draw_landmarksMediaPipe 库中的函数)来实现我的目标。
我正在尝试的具体代码(但有错误)如下:
#Initialize a list to store the detected landmarks.
landmarks = []
# Iterate over the Mediapipe detected landmarks.
for landmark in results.pose_landmarks.landmark:
# Append the Mediapipe landmark into the list.
landmarks.append((int(landmark.x * width), int(landmark.y * height),
(landmark.z * width)))
#create index list for specific landmarks
body_landmark_indices = [11,12,13,14,15,16,23,24,25,26,27,28,29,30,31,32]
landmark_list_body = []
#Create a list which only has the required landmarks
for index in body_landmark_indices:
landmark_list_body.append(landmarks[index - 1])
mp_drawing.draw_landmarks(
image=output_image,
landmark_list=landmark_list_body.pose_landmarks,
connections=mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=landmark_drawing_spec,
connection_drawing_spec=connection_drawing_spec)`
Executing the above I get the error `'list' object has no attribute 'pose_landmarks'
Run Code Online (Sandbox Code Playgroud)
我已替换landmark_list=landmark_list_body.pose_landmarks,为landmark_list=landmark_list_body但有错误。
我现在已经非常分层并且没有想法了。世界上有没有披风的英雄吗?
谢谢。
您可以尝试以下方法:
import cv2
import mediapipe as mp
import numpy as np
from mediapipe.python.solutions.pose import PoseLandmark
from mediapipe.python.solutions.drawing_utils import DrawingSpec
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
custom_style = mp_drawing_styles.get_default_pose_landmarks_style()
custom_connections = list(mp_pose.POSE_CONNECTIONS)
# list of landmarks to exclude from the drawing
excluded_landmarks = [
PoseLandmark.LEFT_EYE,
PoseLandmark.RIGHT_EYE,
PoseLandmark.LEFT_EYE_INNER,
PoseLandmark.RIGHT_EYE_INNER,
PoseLandmark.LEFT_EAR,
PoseLandmark.RIGHT_EAR,
PoseLandmark.LEFT_EYE_OUTER,
PoseLandmark.RIGHT_EYE_OUTER,
PoseLandmark.NOSE,
PoseLandmark.MOUTH_LEFT,
PoseLandmark.MOUTH_RIGHT ]
for landmark in excluded_landmarks:
# we change the way the excluded landmarks are drawn
custom_style[landmark] = DrawingSpec(color=(255,255,0), thickness=None)
# we remove all connections which contain these landmarks
custom_connections = [connection_tuple for connection_tuple in custom_connections
if landmark.value not in connection_tuple]
IMAGE_FILES = ["test.jpg"]
BG_COLOR = (192, 192, 192)
with mp_pose.Pose(
static_image_mode=True,
model_complexity=2,
enable_segmentation=True,
min_detection_confidence=0.5) as pose:
for idx, file in enumerate(IMAGE_FILES):
image = cv2.imread(file)
image_height, image_width, _ = image.shape
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
annotated_image = image.copy()
mp_drawing.draw_landmarks(
annotated_image,
results.pose_landmarks,
connections = custom_connections, # passing the modified connections list
landmark_drawing_spec=custom_style) # and drawing style
cv2.imshow('landmarks', annotated_image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
它修改DrawingSpec和POSE_CONNECTIONS以“隐藏”地标的子集。但是,由于该draw_landmarks()功能在 Mediapipe 中的实现方式,还需要在drawing_utils.py(位于site-packages/mediapipe/python/solutions)中添加一个条件:
if drawing_spec.thickness == None: continue
Run Code Online (Sandbox Code Playgroud)
将其添加到第 188 行之前(请参阅MediaPipe GitHub 存储库, # White circle border)。结果应该是这样的:
...
drawing_spec = landmark_drawing_spec[idx] if isinstance(
landmark_drawing_spec, Mapping) else landmark_drawing_spec
if drawing_spec.thickness == None: continue
# White circle border
circle_border_radius = max(drawing_spec.circle_radius + 1,
int(drawing_spec.circle_radius * 1.2))
...
Run Code Online (Sandbox Code Playgroud)
需要进行此更改,以便完全消除地标周围绘制的白色边框,无论其绘图规范如何。
希望能帮助到你。