一、前言
大約兩年前,基于自己的理解我曾寫了幾篇關(guān)于Mediapipe的文章,似乎幫助到了一些人。這兩年,忙于比賽、實習(xí)、畢業(yè)、工作和考研。上篇文章已經(jīng)是一年多前發(fā)的了。這段時間收到很多私信和評論,請原諒無法一一回復(fù)了。我將嘗試在這篇文章里回答一些大家經(jīng)常問到的問題。
二、繪制3d鉸接骨架
我曾在之前的文章里講過,可以使用Mediapipe推理得到的3d坐標(biāo)繪制到3d畫布上,使用的函數(shù)就是:mp.solutions.drawing_utils.plot_landmarks(),不過只能導(dǎo)出2d圖,沒法拖動交互,實現(xiàn)效果如下:
這個函數(shù)是官方自己封裝的,我們可以利用matplotlib自行實現(xiàn)實時繪制3d鉸接骨架圖的需求,效果如下:
實時姿態(tài)估計
由于畫在了3d畫布上,這時候就能拖動畫布,以不同角度查看實時的人體姿態(tài)。大家可以自行嘗試。
三、關(guān)于Mediapipe的3d坐標(biāo)
-
mediapipe可以推理得到3d坐標(biāo),但這個3d坐標(biāo)并不是真實的3d坐標(biāo)。這些坐標(biāo)描述了一個以人體臀部為中心的人體外接圓,是虛擬的坐標(biāo)。這一點可以從其官方描述得知。
-
在對每一幀圖像做處理時,如果要獲取某個keypoint(人體某個關(guān)節(jié))在圖像上的坐標(biāo)時,可以這樣轉(zhuǎn)換:
results = pose.process(img)
X_ = results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].x * img_width
Y_ = results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].y * img_height
四、關(guān)于姿態(tài)估計的進(jìn)一步學(xué)習(xí)
- 如果想獲取實際的3d坐標(biāo),可以用相機(jī)標(biāo)定,這里涉及的知識更多。Google搜索‘camera calibration’可以學(xué)習(xí)到更多。
- 其他好用的人體姿態(tài)估計模型,有mmpose、alphapose、openpose等。個人比較喜歡mmpose,從數(shù)據(jù)標(biāo)注到模型訓(xùn)練都比較成熟。
- 曾經(jīng)有人問過,如果要做動物姿態(tài)估計,那么毫不猶豫請用DeepLabCut,同樣在數(shù)據(jù)標(biāo)注和模型訓(xùn)練及導(dǎo)出上,非常成熟易用。
五、所有代碼
要結(jié)束程序,請按ESC,或者ctrl+c文章來源:http://www.zghlxwxcb.cn/news/detail-777439.html
import cv2
import matplotlib.pyplot as plt
import mediapipe as mp
import time
import numpy as np
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
colorclass = plt.cm.ScalarMappable(cmap='jet')
colors = colorclass.to_rgba(np.linspace(0, 1, int(33)))
colormap = (colors[:, 0:3])
def draw3d(plt, ax, world_landmarks, connnection=mp_pose.POSE_CONNECTIONS):
ax.clear()
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
landmarks = []
for index, landmark in enumerate(world_landmarks.landmark):
landmarks.append([landmark.x, landmark.z, landmark.y*(-1)])
landmarks = np.array(landmarks)
ax.scatter(landmarks[:, 0], landmarks[:, 1], landmarks[:, 2], c=np.array(colormap), s=50)
for _c in connnection:
ax.plot([landmarks[_c[0], 0], landmarks[_c[1], 0]],
[landmarks[_c[0], 1], landmarks[_c[1], 1]],
[landmarks[_c[0], 2], landmarks[_c[1], 2]], 'k')
plt.pause(0.001)
#端口號一般是0,除非你還有其他攝像頭
#使用本地視頻推理,復(fù)制其文件路徑代替端口號即可
cap = cv2.VideoCapture(0)
with mp_pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
model_complexity = 1) as pose:
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
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
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
start = time.time()
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pose.process(image)
# Draw the pose annotation on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
end = time.time()
fps = 1 / (end - start)
fps = "%.2f fps" % fps
#實時顯示幀數(shù)
image = cv2.flip(image, 1)
cv2.putText(image, "FPS {0}".format(fps), (100, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255),3)
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(5) & 0xFF == 27:
break
if results.pose_world_landmarks:
draw3d(plt, ax, results.pose_world_landmarks)
cap.release()
六、寫在最后
如果有任何問題,歡迎在評論區(qū)討論、賜教。文章來源地址http://www.zghlxwxcb.cn/news/detail-777439.html
到了這里,關(guān)于Mediapipe人體骨架檢測和實時3d繪制——Mediapipe實時姿態(tài)估計的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!