当前位置: 首页 > news >正文

Mediapipe绘制实时3d铰接骨架图——Mediapipe实时姿态估计

一、前言

大约两年前,基于自己的理解我曾写了几篇关于Mediapipe的文章,似乎帮助到了一些人。这两年,忙于比赛、实习、毕业、工作和考研。上篇文章已经是一年多前发的了。这段时间收到很多私信和评论,请原谅无法一一回复了。我将尝试在这篇文章里回答一些大家经常问到的问题。

二、绘制3d铰接骨架

我曾在之前的文章里讲过,可以使用Mediapipe推理得到的3d坐标绘制到3d画布上,使用的函数就是:mp.solutions.drawing_utils.plot_landmarks(),不过只能导出2d图,没法拖动交互,实现效果如下:
在这里插入图片描述
这个函数是官方自己封装的,我们可以利用matplotlib自行实现实时绘制3d铰接骨架图的需求,效果如下:

实时姿态估计


由于画在了3d画布上,这时候就能拖动画布,以不同角度查看实时的人体姿态。大家可以自行尝试。

三、关于Mediapipe的3d坐标

  1. mediapipe可以推理得到3d坐标,但这个3d坐标并不是真实的3d坐标。这些坐标描述了一个以人体臀部为中心的人体外接圆,是虚拟的坐标。这一点可以从其官方描述得知。
    在这里插入图片描述

  2. 在对每一帧图像做处理时,如果要获取某个keypoint(人体某个关节)在图像上的坐标时,可以这样转换:

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

四、关于姿态估计的进一步学习

  1. 如果想获取实际的3d坐标,可以用相机标定,这里涉及的知识更多。Google搜索‘camera calibration’可以学习到更多。
  2. 其他好用的人体姿态估计模型,有mmpose、alphapose、openpose等。个人比较喜欢mmpose,从数据标注到模型训练都比较成熟。
  3. 曾经有人问过,如果要做动物姿态估计,那么毫不犹豫请用DeepLabCut,同样在数据标注和模型训练及导出上,非常成熟易用。

五、所有代码

要结束程序,请按ESC,或者ctrl+c

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,除非你还有其他摄像头
#使用本地视频推理,复制其文件路径代替端口号即可
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
    #实时显示帧数
    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()

六、写在最后

如果有任何问题,欢迎在评论区讨论、赐教。

相关文章:

  • 【解决复杂链式任务打造全能助手】大模型思维链 CoT 应用:LangChain 大模型 结合 做 AutoGPT
  • Gin 集成 prometheus 客户端实现注册和暴露指标
  • Mac下ThingsBoard本地源代码安装和运行
  • C/C++面向对象(OOP)编程-回调函数详解(回调函数、C/C++异步回调、函数指针)
  • ChatGPT的基本原理?
  • 免费分享一套PyQt6学生信息管理系统 Python管理系统 Python源码,挺漂亮的
  • Python实现张万森下雪了的效果
  • 【c语言】飞机大战(1)
  • 数据库系统课程设计(高校成绩管理数据库系统的设计与实现)
  • [笔记] GICv3/v4 ITS 与 LPI
  • springboot学习(八十五) 解决springboot3.2找不到资源无法抛出404错误的问题
  • 使用python的pika链接rabbitMq断裂
  • 【网络面试(5)】收发数据及断开服务器(四次挥手)
  • 自动化网络故障修复管理
  • 常用设计模式全面总结版(JavaKotlin)
  • 垃圾收集器与内存分配策略
  • 记录:开始学习网络安全
  • 牛客网SQL训练5—SQL大厂面试真题
  • 信号量机制(重要)-第二十八天
  • 系统学习Python——装饰器:函数装饰器-[对方法进行装饰:基础知识]
  • 解读|战国子弹库帛书漂泊海外79年今归国,追索仍将继续
  • 盐城经济技术开发区党工委书记王旭东接受纪律审查和监察调查
  • 新任重庆市垫江县委副书记刘振已任县政府党组书记
  • 舞者王佳俊谈“与AI共舞”:像多了一个舞伴,要考虑它的“感受”
  • 网易有道一季度净利润同比增长247%:有能力在今年实现更强劲的利润增长
  • 贵州省委军民融合发展委员会办公室副主任李刚接受审查调查