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 từ Google Mobile Vision (GMV) sang Bộ công cụ học máy 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.
  • Bộ công cụ học máy hỗ trợ iOS phiên bản 15.5 trở lên.
  • Bộ công cụ học máy không hỗ trợ kiến trúc 32 bit (i386 và armv7). Bộ công cụ học máy 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 iOS của Bộ công cụ học máy trong Podfile của ứng dụng:

APINhóm GMVVùng chứa 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 tổng thể về API

Những thay đổi này áp dụng cho tất 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ẽ gói các giá trị này bên trong MLKVisionImage và lấy đó làm dữ liệu đầu vào.
  • GMV sử dụng NSDictionary để truyền nhiều tuỳ chọn trình phát hiện. Bộ công cụ học máy sử dụng các lớp tuỳ 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 trình phát hiện. Bộ công cụ học máy sử dụng các lớp chuyên dụng để tạo các thực thể trình phát hiện, trình quét và trình nhận dạng riêng biệt.
  • 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 theo cách đồng bộ và không đồng bộ.
  • GMV mở rộng AVCaptureVideoDataOutput và cung cấp khung nhiều trình phát hiện để thực hiện nhiều hoạt động phát hiện cùng một lúc. Bộ công cụ học máy 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à Bộ công cụ học máy cho từng API Vision, đồng thời cho biết cách khởi chạy API.

FaceDetector

Hãy viết lại mã khởi tạo như trong ví dụ sau:

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ó hai 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 theo 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 tuỳ 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

Hãy viết lại mã khởi tạo như trong ví dụ sau:

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ó hai 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 MLKBarcodeScanner. Bạn có thể gọi API suy luận theo 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 tuỳ 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

Hãy viết lại mã khởi tạo như trong ví dụ sau:

GMV

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector có hai 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 theo 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 vấn đề, hãy tham khảo trang Cộng đồng của chúng tôi. Tại đây, chúng tôi trình bày các kênh liên hệ với chúng tôi.