机器学习套件提供了两种优化的姿势检测 SDK。
SDK 名称 | 姿势检测 | 姿势检测准确 |
---|---|---|
实现 | 基本检测器的素材资源会在构建时静态关联到您的应用。 | 准确的检测器的资源会在构建时静态关联到您的应用。 |
应用大小 | 29.6MB | 33.2 MB |
性能 | iPhone X:约 45 FPS | iPhone X:约 29 FPS |
试试看
- 试用示例应用,查看此 API 的示例用法。
准备工作
在 Podfile 中添加以下机器学习套件 Pod:
# If you want to use the base implementation: pod 'GoogleMLKit/PoseDetection', '3.2.0' # If you want to use the accurate implementation: pod 'GoogleMLKit/PoseDetectionAccurate', '3.2.0'
安装或更新项目的 Pod 之后,请使用 Xcode 项目的
xcworkspace
打开该项目。Xcode 13.2.1 版或更高版本支持机器学习套件。
1. 创建 PoseDetector
实例
如需检测图片中的姿势,请先创建一个 PoseDetector
实例,并视需要指定检测器设置。
PoseDetector
个选项
检测模式
PoseDetector
在两种检测模式下运行。请务必选择与您的用例匹配的版本。
stream
(默认)- 姿势检测器将首先检测图片中最突出的人,然后运行姿势检测。在后续帧中,除非人物被遮挡或不再高度检测,否则不会执行人物检测步骤。姿势检测器将尝试跟踪最突出的人物,并在每次推断中返回其姿势。这样可以缩短延迟时间并使检测更顺畅。当您想要检测视频流中的姿态时,请使用此模式。
singleImage
- 姿势检测器将检测一个人,然后运行姿势检测。系统会针对每张图片运行人物检测步骤,因此延迟时间会更长,并且没有人跟踪。在对静态图片使用姿势检测或不需要跟踪时采用此模式。
指定姿势检测器选项:
Swift
// Base pose detector with streaming, when depending on the PoseDetection SDK let options = PoseDetectorOptions() options.detectorMode = .stream // Accurate pose detector on static images, when depending on the // PoseDetectionAccurate SDK let options = AccuratePoseDetectorOptions() options.detectorMode = .singleImage
Objective-C
// Base pose detector with streaming, when depending on the PoseDetection SDK MLKPoseDetectorOptions *options = [[MLKPoseDetectorOptions alloc] init]; options.detectorMode = MLKPoseDetectorModeStream; // Accurate pose detector on static images, when depending on the // PoseDetectionAccurate SDK MLKAccuratePoseDetectorOptions *options = [[MLKAccuratePoseDetectorOptions alloc] init]; options.detectorMode = MLKPoseDetectorModeSingleImage;
最后,获取 PoseDetector
的一个实例。传递您指定的选项:
Swift
let poseDetector = PoseDetector.poseDetector(options: options)
Objective-C
MLKPoseDetector *poseDetector = [MLKPoseDetector poseDetectorWithOptions:options];
2. 准备输入图片
如需检测姿势,请对每个图片或视频帧执行以下操作。
如果您启用了流模式,则必须从 CMSampleBuffer
创建 VisionImage
对象。
使用 UIImage
或 CMSampleBuffer
创建 VisionImage
对象。
如果您使用的是 UIImage
,请按以下步骤操作:
- 使用
UIImage
创建一个VisionImage
对象。请务必指定正确的.orientation
。Swift
let image = VisionImage(image: UIImage) visionImage.orientation = image.imageOrientation
Objective-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
如果您使用的是 CMSampleBuffer
,请按以下步骤操作:
-
指定
CMSampleBuffer
中包含的图片数据的方向。如需获取图片方向,请按以下步骤操作:
Swift
func imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> UIImage.Orientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftMirrored : .right case .landscapeLeft: return cameraPosition == .front ? .downMirrored : .up case .portraitUpsideDown: return cameraPosition == .front ? .rightMirrored : .left case .landscapeRight: return cameraPosition == .front ? .upMirrored : .down case .faceDown, .faceUp, .unknown: return .up } }
Objective-C
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
- 使用
CMSampleBuffer
对象和屏幕方向创建一个VisionImage
对象:Swift
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
Objective-C
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. 处理图片
将 VisionImage
传递给姿势检测器的图片处理方法之一。您可以使用异步 process(image:)
方法或同步 results()
方法。
要同步检测对象,请执行以下操作:
Swift
var results: [Pose] do { results = try poseDetector.results(in: image) } catch let error { print("Failed to detect pose with error: \(error.localizedDescription).") return } guard let detectedPoses = results, !detectedPoses.isEmpty else { print("Pose detector returned no results.") return } // Success. Get pose landmarks here.
Objective-C
NSError *error; NSArray*poses = [poseDetector resultsInImage:image error:&error]; if (error != nil) { // Error. return; } if (poses.count == 0) { // No pose detected. return; } // Success. Get pose landmarks here.
如需异步检测对象,请执行以下操作:
Swift
poseDetector.process(image) { detectedPoses, error in guard error == nil else { // Error. return } guard !detectedPoses.isEmpty else { // No pose detected. return } // Success. Get pose landmarks here. }
Objective-C
[poseDetector processImage:image completion:^(NSArray* _Nullable poses, NSError * _Nullable error) { if (error != nil) { // Error. return; } if (poses.count == 0) { // No pose detected. return; } // Success. Get pose landmarks here. }];
4. 获取有关检测到的姿势的信息
如果在图片中检测到人,姿势检测 API 会将 Pose
对象数组传递给完成处理程序或返回数组,具体取决于您调用的是异步方法还是同步方法。
如果这个人不完全在图片内,模型会将缺失的地标坐标分配给帧外,并为其指定较低的 InFrameConfidence 值。
如果未检测到人员,则数组为空。
Swift
for pose in detectedPoses { let leftAnkleLandmark = pose.landmark(ofType: .leftAnkle) if leftAnkleLandmark.inFrameLikelihood > 0.5 { let position = leftAnkleLandmark.position } }
Objective-C
for (MLKPose *pose in detectedPoses) { MLKPoseLandmark *leftAnkleLandmark = [pose landmarkOfType:MLKPoseLandmarkTypeLeftAnkle]; if (leftAnkleLandmark.inFrameLikelihood > 0.5) { MLKVision3DPoint *position = leftAnkleLandmark.position; } }
效果提升技巧
结果的质量取决于输入图片的质量:
- 为了使机器学习套件准确检测姿势,图片中的人物应该由足够的像素数据表示;为了获得最佳性能,拍摄对象应至少为 256x256 像素。
- 如果您在实时应用中检测到姿势,则还应考虑输入图片的整体尺寸。较小图片的处理速度相对较快,因此,为了缩短延迟时间,请以较低的分辨率捕获图片,但请牢记上述分辨率要求,并确保拍摄对象占据尽可能大的图片。
- 图片聚焦不良也会影响准确性。如果您没有获得可接受的结果,请让用户重新捕获图片。
如果要在实时应用中使用姿势检测,请遵循以下准则以实现最佳帧速率:
- 使用基本 PoseDetection SDK 和
stream
检测模式。 - 建议以较低分辨率捕获图片,不过,您也要牢记此 API 的图片尺寸要求。
- 如需处理视频帧,请使用检测器的
results(in:)
同步 API。从 AVCaptureVideoDataOutputSampleBufferDelegate 的 captureOutput(_, DidOutput:from:) 函数调用此方法,以从指定视频帧同步获取结果。将 AVCaptureVideoDataOutput 的 alwaysIgnoresLateVideoFrames 设置为 true,即可限制对检测器的调用。如果在检测器运行时有新视频帧可用,该帧将被丢弃。 - 如果要将检测器的输出作为图形叠加在输入图片上,请先从机器学习套件获取结果,然后在一个步骤中完成图片的呈现和叠加。采用这一方法,每个已处理的输入帧只需在显示表面呈现一次。如需查看示例,请参阅展示示例应用中的 previewOverlayView 和 MLKDetectionOverlayView 类。
后续步骤
- 如需了解如何使用姿势特征对姿势进行分类,请参阅姿势分类提示。
- 请查看 GitHub 上的机器学习套件快速入门示例,获取使用此 API 的相关示例。