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 für iOS zu migrieren.

Vorbereitung

Bevor Sie mit der Migration Ihres Codes beginnen, müssen Sie die folgenden Anforderungen erfüllen:

  • ML Kit unterstützt Xcode 13.2.1 oder höher.
  • ML Kit unterstützt iOS 15.5 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-CocoaPods für iOS in der Podfile-Datei Ihrer App:

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

Allgemeine API-Änderungen

Diese Änderungen gelten für alle APIs:

  • Die GMV-Inferenz-APIs verwenden UIImage oder CMSampleBufferRef als Eingabe. ML Kit umschließt sie mit einem MLKVisionImage und verwendet diesen als Eingabe.
  • Für den GMV werden verschiedene Optionen für den Detektor über NSDictionary übergeben. ML Kit verwendet zu diesem Zweck spezielle Optionsklassen.
  • Der GMV übergibt den Detektortyp an die einzelne GMVDetector-Klasse, wenn ein Detektor erstellt wird. In ML Kit werden spezielle Klassen verwendet, um separate Instanzen für Detektoren, Scanner und Erkennungsfunktionen 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 Framework mit mehreren Detektoren, mit dem mehrere Erkennungen gleichzeitig durchgeführt werden können. ML Kit bietet keine solchen Mechanismen, aber dieselbe Funktionalität kann bei Bedarf vom Entwickler implementiert werden.

API-spezifische Änderungen

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

FaceDetector

Codieren Sie die Initialisierung wie in diesem Beispiel neu:

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 bietet zwei verschiedene APIs zur Erkennung. 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 Inference 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:

GMV 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:

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 bietet zwei verschiedene APIs zur Erkennung. 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 Inference 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:

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary der Optionen für die Barcode-Erkennung 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:

GMV

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector bietet zwei verschiedene APIs zur Erkennung. 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 Inference 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:

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

Hilfe erhalten

Sollten Probleme auftreten, findest du auf unserer Community-Seite Informationen dazu, wie du uns kontaktieren kannst.