Mobile Vision から ML Kit(iOS)への移行

このドキュメントでは、Google Mobile Vision(GMV)から iOS の ML Kit にプロジェクトを移行するために必要な手順について説明します。

前提条件

コードの移行を開始する前に、次の要件を満たしていることを確認してください。

  • ML Kit は Xcode 13.2.1 以降をサポートしています。
  • ML Kit は iOS バージョン 15.5 以降をサポートしています。
  • ML Kit は 32 ビット アーキテクチャ(i386 と armv7)をサポートしていません。ML Kit は 64 ビット アーキテクチャ(x86_64 と arm64)をサポートしています。

cocoapods を更新する

アプリの Podfile で、ML Kit iOS CocoaPods の依存関係を更新します。

APIGMV PodML Kit Pod
バーコード スキャン GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
顔検出 GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
テキスト認識 GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

API の全体的な変更

この変更は、すべての API に適用されます。

  • GMV の推論 API は、UIImage または CMSampleBufferRef を入力として受け取ります。ML Kit は、それらを MLKVisionImage でラップし、それを入力として受け取ります。
  • GMV は NSDictionary を使用して、さまざまな検出オプションを渡します。ML Kit では、この目的に専用のオプション クラスを使用します。
  • GMV は、検出機能を作成するときに、検出機能のタイプを単一の GMVDetector クラスに渡します。ML Kit では、専用のクラスを使用して、検出機能、スキャナ、認識ツールの個別のインスタンスを作成します。
  • GMV の API は同期検出のみをサポートしています。ML Kit の推論 API は、同期と非同期の両方で呼び出すことができます。
  • GMV は AVCaptureVideoDataOutput を拡張し、複数の検出を同時に実行するためのマルチ検出フレームワークを提供します。ML Kit にはこのようなメカニズムはありません。ただし、必要に応じてデベロッパーが同じ機能を実装できます。

API 固有の変更

このセクションでは、各 Vision API に対応する GMV クラスと ML Kit クラスとメソッドについて説明します。また、API を初期化する方法についても説明します。

FaceDetector

次の例に示すように、初期化を再コードします。

総取引額ML Kit
NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];
MLKFaceDetectorOptions *options = [[MLKFaceDetectorOptions alloc] init];
options.performanceMode = MLKFaceDetectorPerformanceModeAccurate;
options.classificationMode = MLKFaceDetectorClassificationModeAll;
options.landmarkMode = MLKFaceDetectorLandmarkModeAll;
MLKFaceDetector *faceDetector = [MLKFaceDetector faceDetectorWithOptions:options];

GMVDetector には 2 つの異なる検出 API があります。どちらも同期オペレーションです。

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

GMVDetectorMLKFaceDetector に置き換えます。推論 API は同期または非同期で呼び出すことができます。

同期非同期
- (nullable NSArray<MLKFace *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKFaceDetectionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

次のクラス、メソッド、名前を変更します。

BarcodeDetector

次の例に示すように、初期化を再コードします。

総取引額ML Kit
NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];
MLKBarcodeScannerOptions *options = [[MLKBarcodeScannerOptions alloc] init];
options.formats = MLKBarcodeFormatCode128 | MLKBarcodeFormatQRCode;
MLKBarcodeScanner *barcodeScanner =
    [MLKBarcodeScanner barcodeScannerWithOptions:options];

GMVDetector には 2 つの異なる検出 API があります。どちらも同期オペレーションです。

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

GMVDetectorMLKBarcodeScanner に置き換えます。推論 API は同期または非同期で呼び出すことができます。

同期非同期
- (nullable NSArray<MLKBarcode *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKBarcodeScanningCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

次のクラス、メソッド、名前を変更します。

TextRecognition

次の例に示すように、初期化を再コードします。

総取引額ML Kit
GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];
MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector には 2 つの異なる検出 API があります。どちらも同期オペレーションです。

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInImage:(UIImage *)image
            options:(nullable NSDictionary *)options;

- (nullable NSArray<__kindof GMVFeature *> *)
    featuresInBuffer:(CMSampleBufferRef)sampleBuffer
             options:(nullable NSDictionary *)options;

GMVDetectorMLKTextRecognizer に置き換えます。推論 API は同期または非同期で呼び出すことができます。

同期非同期
- (nullable MLKText *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKTextRecognitionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

次のクラス、メソッド、名前を変更します。

困ったときは

問題が発生した場合は、コミュニティ ページで、YouTube にお問い合わせいただけるチャネルをご確認ください。