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.

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 Version 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 iOS-Cocoapods in der Podfile-Datei Ihrer App:

APIGMV-PodML Kit-Pod
Scannen von Barcodes 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 Inferenz-APIs von GMV verwenden UIImage oder CMSampleBufferRef als Eingabe. ML Kit umschließt sie in ein MLKVisionImage und verwendet dieses als Eingabe.
  • GMV verwendet NSDictionary, um verschiedene Detektoroptionen zu übergeben. ML Kit verwendet dafür spezielle Optionsklassen.
  • GMV übergibt den Detektortyp an die einzelne GMVDetector-Klasse, wenn ein Detektor erstellt wird. 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 Framework mit mehreren Detektoren, um mehrere Erkennungen gleichzeitig durchzuführen. 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 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:

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

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

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

Hilfe erhalten

Bei Problemen finden Sie auf unserer Community-Seite weitere Informationen dazu, wie Sie uns kontaktieren können.