ML Kit を使用してポーズを検出する(iOS)

ML Kit には、姿勢検出用に最適化された SDK が 2 つ用意されています。

SDK 名PoseDetectionPoseDetectionAccurate
実装ベース検出機能のアセットは、ビルド時にアプリに静的にリンクされます。正確な検出器のアセットは、ビルド時にアプリに静的にリンクされます。
アプリのサイズ最大 29.6 MB最大 33.2 MB
パフォーマンスiPhone X: 約 45 FPSiPhone X: 約 29 FPS

試してみる

始める前に

  1. Podfile に次の ML Kit 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'
    
  2. プロジェクトの Pod をインストールまたは更新したら、xcworkspace を使用して Xcode プロジェクトを開きます。ML Kit は Xcode バージョン 13.2.1 以降でサポートされています。

1. PoseDetector のインスタンスを作成する

画像内のポーズを検出するには、まず PoseDetector のインスタンスを作成し、必要に応じて検出機能の設定を指定します。

PoseDetector つのオプション

検出モード

PoseDetector は 2 つの検出モードで動作します。ユースケースに合ったものを選択してください。

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;
  }
}

パフォーマンス改善のヒント

結果の品質は入力画像の品質によって異なります。

  • ML Kit でポーズを正確に検出するには、画像内の人物を十分なピクセルデータで表す必要があります。最適なパフォーマンスを得るには、被写体が少なくとも 256x256 ピクセルである必要があります。
  • リアルタイム アプリケーションでポーズを検出する場合は、入力画像の全体的なサイズも考慮する必要があります。サイズが小さいほど処理が速くなるため、レイテンシを短縮し、低い解像度で画像をキャプチャします。ただし、上記の解像度要件に留意し、被写体が画像のできるだけ多くを占有するようにしてください。
  • 画像のフォーカスが不適切であることも精度に影響することがあります。満足できる結果が得られない場合は、画像をキャプチャし直すようユーザーに依頼します。

リアルタイムのアプリケーションで姿勢検出を使用する場合は、最適なフレームレートを得るために次のガイドラインに従ってください。

  • 基本の PoseDetection SDK と stream 検出モードを使用します。
  • 低解像度で画像をキャプチャすることを検討してください。ただし、この API の画像サイズの要件にも注意してください。
  • 動画フレームの処理には、検出機能の results(in:) 同期 API を使用します。このメソッドを AVCaptureVideoDataOutputSampleBufferDelegatecaptureOutput(_, didOutput:from:) 関数から呼び出して、指定された動画フレームから結果を同期的に取得します。検出機能の呼び出しをスロットリングするには、AVCaptureVideoDataOutputalwaysDiscardsLateVideoFrames を true のままにします。検出機能の実行中に新しい動画フレームが使用可能になった場合、そのフレームは破棄されます。
  • 検出機能の出力を使用して入力画像にグラフィックをオーバーレイする場合は、まず ML Kit から結果を取得してから、画像とオーバーレイを 1 つのステップでレンダリングします。これにより、処理された入力フレームごとに、ディスプレイ サーフェスに 1 回だけレンダリングされます。例については、ショーケース サンプルアプリの previewOverlayView クラスと MLKDetectionOverlayView クラスをご覧ください。

次のステップ