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 放上来供大家参考~