Von Mobile Vision zu ML Kit für iOS migrieren

In diesem Dokument werden die Schritte beschrieben, die Sie ausführen müssen, um Ihre Projekte von Google Mobile Vision (GMV) zu ML Kit unter iOS zu migrieren.

Voraussetzungen

Stellen Sie vor der Migration des Codes sicher, dass Sie die folgenden Anforderungen erfüllen:

  • ML Kit unterstützt Xcode 13.2.1 oder höher.
  • ML Kit unterstützt iOS 10 oder höher.
  • ML Kit unterstützt keine 32-Bit-Architekturen (i386 und armv7). ML Kit unterstützt 64-Bit-Architekturen (x86_64 und arm64).

CocoaPods aktualisieren

Aktualisieren Sie die Abhängigkeiten für die ML Kit-iOS-CocoaPods in der Podfile Ihrer App:

APIBWW-PodML Kit-Pod
Barcode-Scan GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
Gesichtserkennung GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
Texterkennung GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

Allgemeine Änderungen an der API

Diese Änderungen gelten für alle APIs:

  • Die Inferenz-APIs von GMV erhalten UIImage oder CMSampleBufferRef als Eingabe. ML Kit schließt sie in eine MLKVisionImage ein und verwendet diese als Eingabe.
  • Der Bruttowarenwert (BWW) verwendet NSDictionary, um verschiedene Detektoroptionen zu übergeben. ML Kit verwendet zu diesem Zweck spezielle Optionsklassen.
  • Die GMV übergibt den Detektortyp beim Erstellen eines Detektors an die einzelne GMVDetector-Klasse. ML Kit verwendet spezielle Klassen, um separate Detektor-, Scanner- und Erkennungsinstanzen zu erstellen.
  • Die APIs von GMV unterstützen nur die synchrone Erkennung. Die Inferenz-APIs von ML Kit können synchron und asynchron aufgerufen werden.
  • GMV erweitert AVCaptureVideoDataOutput und bietet ein Multi-Detektor-Framework für die gleichzeitige Durchführung mehrerer Erkennungen. ML Kit bietet keine solchen Mechanismen, aber bei Bedarf kann der Entwickler dieselben Funktionen implementieren.

API-spezifische Änderungen

In diesem Abschnitt werden die entsprechenden GMV- und ML Kit-Klassen und -Methoden für jede Vision API beschrieben. Außerdem wird gezeigt, wie die API initialisiert wird.

FaceDetector

Codieren Sie die Initialisierung wie in diesem Beispiel neu:

Bruttowarenwert (BWW)

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 hat zwei verschiedene Erkennungs-APIs. Beide sind synchrone Vorgänge:

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

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

Ersetzen Sie GMVDetector durch MLKFaceDetector. Die Inferenz-API kann synchron oder asynchron aufgerufen werden.

Synchron

- (nullable NSArray<MLKFace *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;

Asynchron

- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKFaceDetectionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Ändern Sie die folgenden Klassen, Methoden und Namen:

Bruttowarenwert (BWW) ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary der Optionen für die Gesichtserkennung 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

Codieren Sie die Initialisierung wie in diesem Beispiel neu:

Bruttowarenwert (BWW)

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 hat zwei verschiedene Erkennungs-APIs. Beide sind synchrone Vorgänge:

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

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

Ersetzen Sie GMVDetector durch MLKBarcodeScanner. Die Inferenz-API kann synchron oder asynchron aufgerufen werden.

Synchron

- (nullable NSArray<MLKBarcode *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;

Asynchron

- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKBarcodeScanningCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Ändern Sie die folgenden Klassen, Methoden und Namen:

Bruttowarenwert (BWW) ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary der Optionen für Barcodedetektoren 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

Codieren Sie die Initialisierung wie in diesem Beispiel neu:

Bruttowarenwert (BWW)

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector hat zwei verschiedene Erkennungs-APIs. Beide sind synchrone Vorgänge:

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

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

Ersetzen Sie GMVDetector durch MLKTextRecognizer. Die Inferenz-API kann synchron oder asynchron aufgerufen werden.

Synchron

- (nullable MLKText *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;

Asynchron

- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKTextRecognitionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Ändern Sie die folgenden Klassen, Methoden und Namen:

Bruttowarenwert (BWW) ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
GMVTextBlockFeature MLKTextBlock
GMVTextElementFeature MLKTextElement
GMVTextLineFeature MLKTextLine

Unterstützung erhalten

Falls Probleme auftreten, findest du auf unserer Community-Seite eine Übersicht über die Kanäle, über die du mit uns Kontakt aufnehmen kannst.