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

このドキュメントでは、iOS でプロジェクトを Google Mobile Vision(GMV)から 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

次の例に示すように、初期化を再コーディングします。

総取引額

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;

GMVDetector を MLKFaceDetector に置き換えます。推論 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;

GMVDetector を MLKBarcodeScanner に置き換えます。推論 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;

GMVDetector を MLKTextRecognizer に置き換えます。推論 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

困ったときは

問題が発生した場合は、コミュニティ ページをご覧ください。このページでは、Google に連絡するためのチャネルについて説明しています。