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

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

前提条件

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

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

cocoapods を更新する

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

APIGMV 連続配信広告ML 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

初期化を次の例のように変更し直します。

総取引額

NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];

ML Kit

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:));

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

総取引額 ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
顔検出オプションの NSDictionary MLKFaceDetectorOptions
GMVDetectorFaceFastMode Set MLKFaceDetectorOptions.performanceMode to MLKFaceDetectorPerformanceModeFast
GMVDetectorFaceAccurateMode Set MLKFaceDetectorOptions.performanceMode to MLKFaceDetectorPerformanceModeAccurate
GMVDetectorFaceSelfieMode Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceLandmarkType MLKFaceDetectorOptions.landmarkMode
GMVDetectorFaceLandmarkNone Set MLKFaceDetectorOptions.landmarkMode to MLKFaceDetectorLandmarkModeNone
GMVDetectorFaceLandmarkAll Set MLKFaceDetectorOptions.landmarkMode to MLKFaceDetectorLandmarkModeAll
GMVDetectorFaceLandmarkContour Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceClassificationType MLKFaceDetectorOptions.classificationMode
GMVDetectorFaceClassificationNone Set MLKFaceDetectorOptions.classificationMode to MLKFaceDetectorClassificationModeNone
GMVDetectorFaceClassificationAll Set MLKFaceDetectorOptions.classificationMode to MLKFaceDetectorClassificationModeAll
GMVDetectorFaceTrackingEnabled MLKFaceDetectorOptions.trackingEnabled
GMVDetectorProminentFaceOnly Set MLKFaceDetectorOptions.contourMode to MLKFaceDetectorContourModeAll
GMVDetectorFaceMinSize MLKFaceDetectorOptions.minFaceSize

BarcodeDetector

初期化を次の例のように変更し直します。

総取引額

NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];

ML Kit

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:));

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

総取引額 ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
バーコード検出器オプションの NSDictionary MLKBarcodeScannerOptions
GMVDetectorBarcodeFormats MLKBarcodeScannerOptions.formats
GMVBarcodeFeature MLKBarcode
GMVBarcodeFeatureAddress MLKBarcodeAddress
GMVBarcodeFeatureCalendarEvent MLKBarcodeCalendarEvent
GMVBarcodeFeatureContactInfo MLKBarcodeContactInfo
GMVBarcodeFeatureDriverLicense MLKBarcodeDriverLicense
GMVBarcodeFeatureEmail MLKBarcodeEmail
GMVBarcodeFeatureGeoPoint MLKBarcodeGeoPoint
GMVBarcodeFeaturePersonName MLKBarcodePersonName
GMVBarcodeFeaturePhone MLKBarcodePhone
GMVBarcodeFeatureSMS MLKBarcodeSMS
GMVBarcodeFeatureURLBookmark MLKBarcodeURLBookmark
GMVBarcodeFeatureWiFi MLKBarcodeWiFi

TextRecognition

初期化を次の例のように変更し直します。

総取引額

GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];

ML Kit

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:));

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

総取引額 ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
GMVTextBlockFeature MLKTextBlock
GMVTextElementFeature MLKTextElement
GMVTextLineFeature MLKTextLine

参考情報

問題が発生した場合は、コミュニティ ページをご確認ください。このページには、お問い合わせに使用できるチャンネルの概要が記載されています。