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

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

GMV

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

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

GMV 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

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

GMV

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

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

GMV 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

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

GMV

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

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

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
GMVTextBlockFeature MLKTextBlock
GMVTextElementFeature MLKTextElement
GMVTextLineFeature MLKTextLine

困ったときは

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