Body estimation 学习之:如何使用 mediapipe 迅速实现一个姿态检测相关的应用

mediapipe 简介

Mediapipe是google的一个开源项目,支持跨平台的常用ML方案。项目主页在这里,可以看到很多常用的AI功能它都支持,举几个常用的例子:

人脸检测
FaceMesh: 从图像/视频中重建出人脸的3D Mesh,可以用于AR渲染
人像分割: 从图像/视频中把人分割出来,可用于视频会议,像Zoom/钉钉都有这样的功能
手势跟踪:可以标出21个关键点的3D坐标
人体姿态估计: 可以给出33个关键点的3D坐标
头发上色:可以把头发检测出来,并图上颜色
这个项目支持很多个平台,例如常见的Android, IOS, Web以及C++,可以方便部署。

mediapipe 快速安装

  • anaconda 没有对应的资源,用 pip 安装就行,可以换源下载更方便哦
pip install mediapipe -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 建议重新使用一个 anaconda 的虚拟环境进行配置,因为 mediapipe 可能和 tensorflow 等包的版本有冲突,所以建议新建一个环境,反正也不麻烦

代码 demo

# @Time : 2022/1/6 17:40 
# @Author : PeinuanQin
# @File : demo.py
import mediapipe as mp
import cv2
import time


class PoseEstimator:
    def __init__(self
                 , static_model=True
                 , upper_body_only=False
                 , smooth_landmarks=True
                 , min_detection_conf=0.5
                 , min_tracking_conf=0.5):
        self.mp_pose = mp.solutions.pose
        self.mp_drawing = mp.solutions.drawing_utils
        self.pose = self.mp_pose.Pose(static_image_mode=static_model
                                      , upper_body_only=upper_body_only
                                      , smooth_landmarks=smooth_landmarks
                                      , min_detection_confidence=min_detection_conf
                                      , min_tracking_confidence=min_tracking_conf)
        self.result = None
        self.pre_time = 0


    def pose_esitmation_for_videos(self,video_path):
        cap = cv2.VideoCapture(video_path)
        while True:
            success, img = cap.read()
            height, weight, _ = img.shape
            '''通过内置函数得到所有的 landmarks 信息'''
            self.result = self.pose.process(img)
            # self.result = self.pose.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

            current_time = time.time()
            '''求出视频的 fps 写在视频左上角'''
            fps = 1 / (current_time - self.pre_time)
            cv2.putText(img,str(int(fps)),(70,50),cv2.FONT_HERSHEY_PLAIN,3,(255,0,0),3)
            self.pre_time = current_time
            '''对视频中检测出的 keypoints 进行标定'''
            self.mp_drawing.draw_landmarks(img,
                                      self.result.pose_landmarks,
                                      self.mp_pose.POSE_CONNECTIONS) # 使用这个参数就会将图中的 keypoint 进行划线连接
            cv2.imshow("Img",img)
            cv2.waitKey(1)

    def pose_estimation_for_img(self,img_path):
    
        '''注意 cv2 读文件的时候不能有中文路径,否则读不出来,或者你自己可以
        采用其他的方式读入图片'''
        img = cv2.imread(img_path)
        self.result = self.pose.process(img)
        self.mp_drawing.draw_landmarks(img,
                                      self.result.pose_landmarks,
                                      self.mp_pose.POSE_CONNECTIONS)

        cv2.imshow("Img", img)
        cv2.waitKey(0)

    def show_landmarks(self):
        assert_words = "you should first call the 'pose_estimation_for_img' or 'pose_estimation_for_videos' for a result "
        assert self.result is not None, assert_words


if __name__ == '__main__':
    estimator = PoseEstimator()
    estimator.pose_estimation_for_img("./sample.jpg")
    # estimator.pose_esitmation_for_videos("./walking.mp4")

效果展示

在这里插入图片描述

扩展尝试

  • 你可以自己探索这个 mediapipe 进行一些扩展的应用,比如做个俯卧撑计数装置或者仰卧起坐计数装置等,比较方便简单,抽空我会做一些小的 demo 放上来供大家参考~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暖仔会飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值