從 Mobile Vision 遷移至 iOS 上的 ML Kit

本文將說明如何將專案從 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 CocoaPod 的依附元件:

APIGMV PodML Kit Pod
條碼掃描 GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
臉部偵測 GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
文字辨識 GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

整體 API 異動

這些變更適用於所有 API:

  • GMV 的推論 API 會使用 UIImageCMSampleBufferRef 做為輸入內容。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 有兩種不同的偵測 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:));

變更下列類別、方法和名稱:

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 有兩種不同的偵測 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:));

變更下列類別、方法和名稱:

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 有兩種不同的偵測 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:));

變更下列類別、方法和名稱:

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

取得說明

如有任何問題,請參閱社群頁面,瞭解可用來與我們聯絡的管道。