运行时依赖
安装命令
点击复制技能文档
Google MediaPipe 概述 MediaPipe 是 Google 的开源框架,用于构建设备上的机器学习管道。它提供了跨平台的 API,用于视觉、文本、音频和 LLM 推理任务,以及一个低级别的基于图的管道框架,用于自定义 ML 工作负载。涵盖了计算机视觉任务(人脸检测、人脸网格、手势跟踪、姿势估计、整体标志、物体检测、图像分类/分割、手势识别)、文本任务(文本分类、文本嵌入、语言检测)、音频分类、设备上的 LLM 推理,使用 MediaPipe GenAI/任务,模型自定义,使用 MediaPipe Model Maker,使用 MediaPipe 框架(图、计算器、数据包)可视化/构建 ML 管道,在图像/视频上绘制/渲染标志和检测结果,了解 MediaPipe 的架构和组件生态系统(解决方案、任务、Model Maker、Studio、框架)。
架构 MediaPipe 有两层: MediaPipe 解决方案(高级别)- 预建、可直接使用的 ML 任务,通过跨平台 API。对于大多数应用程序使用此层。 MediaPipe 框架(低级别)- 基于图的管道构建器(数据包、图、计算器),用于自定义设备上的 ML 管道。C++ 核心,具有 Android/iOS 绑定。
解决方案层由以下组成: MediaPipe 任务 - 跨平台库(Python、Android、iOS、Web/JS、C++),包装预训练模型 MediaPipe 模型 - 每个任务可下载的预训练 TFLite 模型包 MediaPipe Model Maker - 使用自己的数据微调/自定义模型 MediaPipe Studio - 基于浏览器的无代码基准测试和原型设计工具
安装 Python pip install mediapipe 最新版本(截至 2026-05):0.10.35。Python 包包含所有任务。模型将在运行时或预下载时单独下载。 Android 在 build.gradle 中添加:implementation 'com.google.mediapipe:tasks-vision:0.10.35' 根据需要将 vision 替换为 text、audio 或 genai。 Web/JavaScript npm install @mediapipe/tasks-vision 可用包:@mediapipe/tasks-vision、@mediapipe/tasks-text、@mediapipe/tasks-audio、@mediapipe/tasks-genai。 iOS(CocoaPods)pod 'MediaPipeTasksVision'
快速入门 - Python 示例 人脸检测 导入 mediapipe as mp 从 mediapipe.tasks 导入 python 从 mediapipe.tasks.python 导入 vision model_path = '/absolute/path/to/blaze_face_short_range.tflite' base_options = python.BaseOptions(model_asset_path=model_path) options = vision.FaceDetectorOptions(base_options=base_options) detector = vision.FaceDetector.create_from_options(options) image = mp.Image.create_from_file('photo.jpg') result = detector.detect(image) 对于 result 中的每个检测: bbox = detection.bounding_box print(f"人脸位于 x={bbox.origin_x}, y={bbox.origin_y}, " f"w={bbox.width}, h={bbox.height}, " f"score={detection.categories[0].score}")
手部标志检测 model_path = '/path/to/hand_landmarker.task' options = vision.HandLandmarkerOptions( base_options=python.BaseOptions(model_asset_path=model_path), num_hands=2) detector = vision.HandLandmarker.create_from_options(options) image = mp.Image.create_from_file('hands.jpg') result = detector.detect(image) 对于 result 中的每个手部标志: 对于 hand_landmarks 中的每个标志: print(f"标志:x={lm.x}, y={lm.y}, z={lm.z}")
姿势标志检测 model_path = '/path/to/pose_landmarker_lite.task' options = vision.PoseLandmarkerOptions( base_options=python.BaseOptions(model_asset_path=model_path)) detector = vision.PoseLandmarker.create_from_options(options) image = mp.Image.create_from_file('person.jpg') result = detector.detect(image) # result.pose_landmarks 是一个 NormalizedLandmark 列表(每个 33 个标志) # result.pose_world_landmarks 提供 3D 世界坐标
物体检测 model_path = '/path/to/efficientdet_lite0.tflite' options = vision.ObjectDetectorOptions( base_options=python.BaseOptions(model_asset_path=model_path), max_results=5) detector = vision.ObjectDetector.create_from_options(options) image = mp.Image.create_from_file('scene.jpg') result = detector.detect(image) 对于 result 中的每个检测: print(f"类:{detection.categories[0].category_name}, " f"边界框:{detection.bounding_box}")
文本分类 从 mediapipe.tasks.python 导入 text model_path = '/path/to/text_classifier.tflite' options = text.TextClassifierOptions( base_options=python.BaseOptions(model_asset_path=model_path)) classifier = text.TextClassifier.create_from_options(options) result = classifier.classify("我绝对喜欢这部电影!") 对于 result 中的每个分类: print(f"{category.category_name}:{category.score:.4f}")
在图像上绘制标志 导入 cv2 导入 mediapipe as mp 从 mediapipe.tasks.python 导入 vision 从 mediapipe.framework.formats 导入 landmark_pb2 # ... 检测标志 ... # 将结果标志转换为 NormalizedLandmarkList hand_landmarks_proto