Bermigrasi dari Mobile Vision ke ML Kit di iOS

Dokumen ini membahas langkah-langkah yang perlu Anda lakukan untuk memigrasikan project Anda dari Google Mobile Vision (GMV) ke ML Kit di iOS.

Prasyarat

Sebelum mulai memigrasikan kode, pastikan Anda memenuhi persyaratan berikut:

  • ML Kit mendukung Xcode 13.2.1 atau yang lebih baru.
  • ML Kit mendukung iOS versi 10 atau yang lebih baru.
  • ML Kit tidak mendukung arsitektur 32-bit (i386 dan armv7). ML Kit mendukung arsitektur 64-bit (x86_64 dan arm64).

Mengupdate cocoapods

Perbarui dependensi untuk cocoapod iOS ML Kit di Podfile aplikasi Anda:

APIPod GMVPod ML Kit
Pemindaian kode batang GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
Deteksi wajah GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
Pengenalan teks GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

Keseluruhan perubahan API

Perubahan ini berlaku untuk semua API:

  • API inferensi GMV menggunakan UIImage atau CMSampleBufferRef sebagai input. ML Kit menggabungkannya ke dalam MLKVisionImage dan mengambilnya sebagai input.
  • GMV menggunakan NSDictionary untuk meneruskan berbagai opsi detektor. ML Kit menggunakan class opsi khusus untuk tujuan tersebut.
  • GMV meneruskan jenis detektor ke satu class GMVDetector saat membuat detektor. ML Kit menggunakan class khusus untuk membuat instance detektor, pemindai, dan pengenal yang terpisah.
  • API GMV hanya mendukung deteksi sinkron. API inferensi ML Kit dapat dipanggil secara sinkron dan asinkron.
  • GMV memperluas AVCaptureVideoDataOutput dan menyediakan framework multi-detektor untuk melakukan beberapa deteksi secara bersamaan. ML Kit tidak menyediakan mekanisme tersebut, tetapi fungsi yang sama dapat diterapkan oleh developer jika diinginkan.

Perubahan khusus API

Bagian ini menjelaskan class dan metode GMV dan ML Kit yang sesuai untuk setiap Vision API, dan menunjukkan cara menginisialisasi API tersebut.

FaceDetector

Kode ulang inisialisasi seperti yang ditunjukkan dalam contoh ini:

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 memiliki dua API deteksi yang berbeda. Keduanya adalah operasi sinkron:

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

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

Ganti GMVDetector dengan MLKFaceDetector. Inferensi API dapat dipanggil secara sinkron atau asinkron.

Sinkron

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

Asinkron

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

Ubah class, metode, dan nama berikut:

GMV ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary opsi deteksi wajah 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

Kode ulang inisialisasi seperti yang ditunjukkan dalam contoh ini:

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 memiliki dua API deteksi yang berbeda. Keduanya adalah operasi sinkron:

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

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

Ganti GMVDetector dengan MLKBarcodeScanner. Inferensi API dapat dipanggil secara sinkron atau asinkron.

Sinkron

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

Asinkron

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

Ubah class, metode, dan nama berikut:

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary opsi detektor kode batang 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

Kode ulang inisialisasi seperti yang ditunjukkan dalam contoh ini:

GMV

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector memiliki dua API deteksi yang berbeda. Keduanya adalah operasi sinkron:

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

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

Ganti GMVDetector dengan MLKTextRecognizer. Inferensi API dapat dipanggil secara sinkron atau asinkron.

Sinkron

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

Asinkron

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

Ubah class, metode, dan nama berikut:

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

Mendapatkan bantuan

Jika Anda mengalami masalah, lihat halaman Komunitas kami, tempat kami menjelaskan channel yang tersedia untuk menghubungi kami.