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 rằng 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 10 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 cocoapod

Cập nhật các phần phụ thuộc cho cocoaPods dành cho iOS của Bộ công cụ học máy trong Podfile của ứng dụng:

APINhóm GMVNhóm 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 bao bọc chúng bên trong một MLKVisionImage và lấy đó làm thông tin đầ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 biệt để phục vụ mục đích này.
  • GMV sẽ chuyển loại trình phát hiện sang 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ể riêng biệt cho trình phát hiện, máy quét và trình nhận dạng.
  • API của GMV chỉ hỗ trợ phát hiện đồng bộ. Các API suy luận của Bộ công cụ học máy có thể được gọi một cách đồng bộ và không đồng bộ.
  • GMV mở rộng AVCaptureVideoDataOutput và cung cấp một khung đa bộ phát hiện để thực hiện nhiều lượt phát hiện cùng 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 cùng một chức năng nếu muốn.

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

Phần này mô tả các lớp cũng như phương thức GMV và Bộ công cụ học máy tương ứng cho từng API Vision, đồng thời trình bày cách khởi chạy API.

FaceDetector

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

GMV (tổng giá trị hàng hoá)

NSDictionary *options = @{
    GMVDetectorFaceMode : @(GMVDetectorFaceAccurateMode),
    GMVDetectorFaceClassificationType : @(GMVDetectorFaceClassificationAll),
    GMVDetectorFaceLandmarkType : @(GMVDetectorFaceLandmarkAll)
};
GMVDetector *faceDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeFace options:options];

Bộ công cụ máy học

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 riêng biệt. Cả hai đều là toán tử đồ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. API dự đoán có thể được gọi đồ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 đây:

GMV (tổng giá trị hàng hoá) Bộ công cụ máy học
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary 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

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

GMV (tổng giá trị hàng hoá)

NSDictionary *options = @{
    GMVDetectorBarcodeFormats : @(GMVDetectorBarcodeFormatCode128 |
                                  GMVDetectorBarcodeFormatQRCode)
};
GMVDetector *barcodeDetector =
    [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];

Bộ công cụ máy học

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à toán tử đồ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. API dự đoán có thể được gọi đồ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 đây:

GMV (tổng giá trị hàng hoá) Bộ công cụ máy học
GMVDetectorImageOrientation MLKVisionImage.orientation
NSDictionary lựa chọn về 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 (tổng giá trị hàng hoá)

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

Bộ công cụ máy học

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

GMVDetector có 2 API phát hiện riêng biệt. Cả hai đều là toán tử đồ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. API dự đoán có thể được gọi đồ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 đây:

GMV (tổng giá trị hàng hoá) Bộ công cụ máy học
GMVDetectorImageOrientation MLKVisionImage.orientation
GMVTextBlockFeature MLKTextBlock
GMVTextElementFeature MLKTextElement
GMVTextLineFeature MLKTextLine

Nhận trợ giúp

Nếu bạn gặp vấn đề, hãy xem trang Cộng đồng. Tại đây, bạn có thể xem danh sách những kênh mà chúng tôi có thể liên hệ.