一. 引言
MediaPipe 是一款由 Google Research 开发并开源的多媒体 机器学习模型 应用框架。在谷歌,一系列重要产品,如 YouTube、Google Lens、ARCore、Google Home 以及 Nest,都已深度整合了 MediaPipe。MediaPipe大有用武之地,可以做物体检测、自拍分割、头发分割、人脸检测、手部检测、运动追踪,等等。基于此可以实现更高级的功能。
二. 怎么做
最近在学校做项目需要用到mediapipe,但网上没有很好的教程,于是根据官方文档自己尝试理解也有一些收获,在这里记录一下。
1.官方文档地址Mediapipe
2.实验环境
I.win10 II. Pycharm2021 III. Python3.8 IV. mediapipe0.89
3.我需要检测人体骨架和手部,那么先构建这样的检测模型。根据官网的例子,mediapipe.solutions下有我们需要的解决方案,来看看。
import mediapipe as mp
mp_holistic = mp.solutions.holistic
help(mp.solutions)
Help on package mediapipe.python.solutions in mediapipe.python:
NAME
mediapipe.python.solutions - MediaPipe Solutions Python API.
PACKAGE CONTENTS
download_utils
drawing_styles
drawing_utils
drawing_utils_test
face_detection
face_detection_test
face_mesh
face_mesh_connections
face_mesh_test
hands
hands_connections
hands_test
holistic
holistic_test
objectron
objectron_test
pose
pose_connections
pose_test
selfie_segmentation
selfie_segmentation_test
以上就是mediapipe提供的解决方案,其中drawing_utils是画图用的,drawing_styles应该是渲染风格,face_detection用于面部检测,face_mesh用于绘人脸面网,hands用于手部检测, holistic 是整体的解决方案(包括人脸、骨架、手),pose是识别姿势,objectron用于目标检测,selfie_segmentation是自拍分割。
4. 有了方法,根据官网的例子,先导入必要的包,然后建立模型。
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils #画图是必要的
mp_drawing_styles = mp.solutions.drawing_styles
#选择需要的解决方案,手部检测就mp_hands=mp.solutions.hands,其他类似
mp_holistic = mp.solutions.holistic
5.接着打开 摄像头 ,并建立我们的类。
cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# 加载一个视频的话,把continue换成break
continue
先看看mp_holistic.Holistic下有什么参数
Methods defined here:
| __init__(self, static_image_mode=False, model_complexity=1,
smooth_landmarks=True, enable_segmentation=False,
smooth_segmentation=True,
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删