Migrating from Mobile Vision to ML Kit on iOS

This document covers the steps you need to take to migrate your projects from Google Mobile Vision (GMV) to ML Kit on iOS.

Prerequisites

Before you start to migrate your code, be sure you meet these requirements:

  • ML Kit supports Xcode 13.2.1 or later.
  • ML Kit supports iOS version 15.5 or greater.
  • ML Kit doesn't support 32-bit architectures (i386 and armv7). ML Kit does support 64-bit architectures (x86_64 and arm64).

Update cocoapods

Update the dependencies for the ML Kit iOS cocoapods in your app’s Podfile:

APIGMV PodML Kit Pod
Barcode scanning GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
Face detection GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
Text recognition GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

Overall API changes

These changes apply to all APIs:

  • GMV’s inference APIs take UIImage or CMSampleBufferRef as input. ML Kit wraps them inside an MLKVisionImage and takes that as input.
  • GMV uses NSDictionary to pass various detector options. ML Kit uses dedicated options classes for that purpose.
  • GMV passes the detector type to the single GMVDetector class when it creats a detector. ML Kit uses dedicated classes to create separate detector, scanner, and recognizer instances.
  • GMV's APIs support synchronous detection only. ML Kit's inference APIs can be called synchronously and asynchronously.
  • GMV extends AVCaptureVideoDataOutput and provides a multi-detector framework for performing multiple detections at the same time. ML Kit doesn't provide such mechanisms, but the same functionality can be implemented by the developer if desired.

API-specific changes

This section describes corresponding GMV and ML Kit classes and methods for each Vision API, and shows how to initialize the API.

FaceDetector

Recode the initialization as shown in this example:

GMVML Kit
NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];
MLKFaceDetectorOptions *options = [[MLKFaceDetectorOptions alloc] init];
options.performanceMode = MLKFaceDetectorPerformanceModeAccurate;
options.classificationMode = MLKFaceDetectorClassificationModeAll;
options.landmarkMode = MLKFaceDetectorLandmarkModeAll;
MLKFaceDetector *faceDetector = [MLKFaceDetector faceDetectorWithOptions:options];

GMVDetector has two different detection APIs. Both are synchronous operations:

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

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

Replace GMVDetector with MLKFaceDetector. The inference API can be called synchronously or asynchronously.

- (nullable NSArray<MLKFace *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKFaceDetectionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Change the following classes, methods, and names:

BarcodeDetector

Recode the initialization as shown in this example:

GMVML Kit
NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];
MLKBarcodeScannerOptions *options = [[MLKBarcodeScannerOptions alloc] init];
options.formats = MLKBarcodeFormatCode128 | MLKBarcodeFormatQRCode;
MLKBarcodeScanner *barcodeScanner =
    [MLKBarcodeScanner barcodeScannerWithOptions:options];

GMVDetector has two different detection APIs. Both are synchronous operations:

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

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

Replace GMVDetector with MLKBarcodeScanner. The inference API can be called synchronously or asynchronously.

- (nullable NSArray<MLKBarcode *> *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKBarcodeScanningCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Change the following classes, methods, and names:

TextRecognition

Recode the initialization as shown in this example:

GMVML Kit
GMVDetector *textDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];
MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector has two different detection APIs. Both are synchronous operations:

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

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

Replace GMVDetector with MLKTextRecognizer. The inference API can be called synchronously or asynchronously.

- (nullable MLKText *)
    resultsInImage:(MLKVisionImage *)image
             error:(NSError **)error;
- (void)processImage:(MLKVisionImage *)image
    Completion:
        (MLKTextRecognitionCallback)completion
    NS_SWIFT_NAME(process(_:completion:));

Change the following classes, methods, and names:

Getting help

If you run into any issues, check out our Community page where we outline the channels available for getting in touch with us.