使用机器学习套件检测姿势 (iOS)

机器学习套件提供了两个经过优化的 SDK,用于姿势检测。

SDK 名称PoseDetectionPoseDetectionAccurate
实现基准检测器的资源会在构建时静态链接到您的应用。准确检测器的资源会在构建时静态链接到您的应用。
应用大小不超过 29.6MB不超过 33.2MB
性能iPhone X:约 45 FPSiPhone X:约 29 FPS

试试看

准备工作

  1. 在 Podfile 中添加以下机器学习套件 pod:

    # If you want to use the base implementation:
    pod 'GoogleMLKit/PoseDetection', '7.0.0'
    
    # If you want to use the accurate implementation:
    pod 'GoogleMLKit/PoseDetectionAccurate', '7.0.0'
    
  2. 安装或更新项目的 pod 后,请使用 Xcode 项目的 xcworkspace 来打开该项目。Xcode 13.2.1 版或更高版本支持机器学习套件。

1. 创建 PoseDetector 实例

如需检测图片中的姿势,请先创建一个 PoseDetector 实例,并视需要指定检测器设置。

PoseDetector 个选项

检测模式

PoseDetector 可在两种检测模式下运行。请务必选择与您的用例相符的模型。

stream(默认)
姿势检测器会先检测图片中最突出的人,然后运行姿势检测。在后续帧中,除非人体被遮挡或不再以较高的置信度检测到,否则系统不会执行人体检测步骤。姿势检测器会尝试跟踪最显眼的人物,并在每次推理中返回其姿势。这有助于缩短延迟时间并顺畅检测。如果您想在视频流中检测姿势,请使用此模式。
singleImage
姿势检测器会检测人体,然后运行姿势检测。系统会针对每张图片运行人脸检测步骤,因此延迟时间会更长,并且不会进行人脸跟踪。在静态图片上使用姿势检测或不需要跟踪时,请使用此模式。

指定姿势检测器选项:

// 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
// 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 的实例。传递您指定的选项:

let poseDetector = PoseDetector.poseDetector(options: options)
MLKPoseDetector *poseDetector =
    [MLKPoseDetector poseDetectorWithOptions:options];

2. 准备输入图片

如需检测姿势,请对每个图片或视频帧执行以下操作。 如果您启用了流模式,则必须基于 CMSampleBuffer 创建 VisionImage 对象。

使用 UIImageCMSampleBuffer 创建一个 VisionImage 对象。

如果您使用的是 UIImage,请按以下步骤操作:

  • 使用 UIImage 创建一个 VisionImage 对象。请务必指定正确的 .orientation
    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

如果您使用的是 CMSampleBuffer,请按以下步骤操作:

  • 指定 CMSampleBuffer 中所含图片数据的方向。

    如需获取图片方向,请运行以下命令:

    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
      }
    }
          
    - (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 对象:
    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: cameraPosition)
     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image.orientation =
       [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition:cameraPosition];

3. 处理图片

VisionImage 传递给姿势检测器的图片处理方法之一。您可以使用异步的 process(image:) 方法或同步的 results() 方法。

如需同步检测对象,请运行以下代码:

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.
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.

如需异步检测对象,请运行以下代码:

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.
}
[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 值。

如果未检测到任何人,则数组为空。

for pose in detectedPoses {
  let leftAnkleLandmark = pose.landmark(ofType: .leftAnkle)
  if leftAnkleLandmark.inFrameLikelihood > 0.5 {
    let position = leftAnkleLandmark.position
  }
}
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。从 AVCaptureVideoDataOutputSampleBufferDelegatecaptureOutput(_, didOutput:from:) 函数调用此方法,以同步获取给定视频帧的结果。将 AVCaptureVideoDataOutputalwaysDiscardsLateVideoFrames 设为 true,以限制对检测器的调用次数。如果在检测器运行时有新的视频帧可用,则会被丢弃。
  • 如果要将检测器的输出作为图形叠加在输入图片上,请先从机器学习套件获取结果,然后在一个步骤中完成图片的呈现和叠加。采用这一方法,每个处理过的输入帧只需在显示表面呈现一次。如需查看示例,请参阅展示示例应用中的 previewOverlayViewMLKDetectionOverlayView 类。

后续步骤