Chuyển từ Tầm nhìn di động sang Bộ công cụ học máy trên iOS

Tài liệu này trình bày các bước bạn cần thực hiện để di chuyển dự án của mình từ Google Mobile Vision (GMV) sang ML Kit trên iOS.

Điều kiện tiên quyết

Trước khi bắt đầu di chuyển mã, hãy đảm bảo bạn đáp ứng các yêu cầu sau:

  • Bộ công cụ học máy hỗ trợ Xcode 13.2.1 trở lên.
  • ML Kit hỗ trợ iOS phiên bản 15.5 trở lên.
  • Bộ công cụ học máy không hỗ trợ các kiến trúc 32 bit (i386 và armv7). ML Kit có hỗ trợ kiến trúc 64 bit (x86_64 và arm64).

Cập nhật cocoapods

Cập nhật các phần phụ thuộc cho cocoapods ML Kit iOS trong Podfile của ứng dụng:

APIGMV PodPod Bộ công cụ học máy
Quét mã vạch GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
Phát hiện khuôn mặt GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
Nhận dạng văn bản GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

Các thay đổi chung về API

Những thay đổi này áp dụng cho tất cả các API:

  • API suy luận của GMV lấy UIImage hoặc CMSampleBufferRef làm dữ liệu đầu vào. Bộ công cụ học máy sẽ bao bọc các đối tượng này trong một MLKVisionImage và lấy đối tượng đó làm dữ liệu đầu vào.
  • GMV sử dụng NSDictionary để truyền nhiều lựa chọn về bộ phát hiện. Bộ công cụ học máy sử dụng các lớp lựa chọn chuyên dụng cho mục đích đó.
  • GMV truyền loại trình phát hiện đến lớp GMVDetector duy nhất khi tạo một trình phát hiện. ML Kit sử dụng các lớp chuyên dụng để tạo các thực thể riêng biệt của trình phát hiện, trình quét và trình nhận dạng.
  • Các API của GMV chỉ hỗ trợ tính năng phát hiện đồng bộ. Bạn có thể gọi các API suy luận của Bộ công cụ học máy một cách đồng bộ và không đồng bộ.
  • GMV mở rộng AVCaptureVideoDataOutput và cung cấp một khung nhiều bộ nhận diện để thực hiện nhiều lượt phát hiện cùng một lúc. ML Kit không cung cấp các cơ chế như vậy, nhưng nhà phát triển có thể triển khai chức năng tương tự nếu muốn.

Các thay đổi dành riêng cho API

Phần này mô tả các lớp và phương thức tương ứng của GMV và ML Kit cho từng Vision API, đồng thời cho biết cách khởi tạo API.

FaceDetector

Mã hoá lại quá trình khởi chạy như trong ví dụ này:

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 có 2 API phát hiện khác nhau. Cả hai đều là các thao tác đồng bộ:

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

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

Thay thế GMVDetector bằng MLKFaceDetector. Bạn có thể gọi API suy luận một cách đồng bộ hoặc không đồng bộ.

Đồng bộ

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

Không đồng bộ

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

Thay đổi các lớp, phương thức và tên sau:

GMV ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary các lựa chọn phát hiện khuôn mặt 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

Mã hoá lại quá trình khởi chạy như trong ví dụ này:

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 có 2 API phát hiện riêng biệt. Cả hai đều là các thao tác đồng bộ:

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

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

Thay thế GMVDetector bằng MLKBarcodeScanner. Bạn có thể gọi API suy luận một cách đồng bộ hoặc không đồng bộ.

Đồng bộ

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

Không đồng bộ

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

Thay đổi các lớp, phương thức và tên sau:

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary các lựa chọn của trình phát hiện mã vạch 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

Mã hoá lại quá trình khởi chạy như trong ví dụ này:

GMV

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector có 2 API phát hiện khác nhau. Cả hai đều là các thao tác đồng bộ:

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

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

Thay thế GMVDetector bằng MLKTextRecognizer. Bạn có thể gọi API suy luận một cách đồng bộ hoặc không đồng bộ.

Đồng bộ

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

Không đồng bộ

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

Thay đổi các lớp, phương thức và tên sau:

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

Nhận trợ giúp

Nếu bạn gặp phải vấn đề, hãy truy cập trang Cộng đồng của chúng tôi để xem các kênh liên hệ với chúng tôi.