Mobile Vision에서 iOS의 ML Kit로 마이그레이션

이 문서에서는 프로젝트를 Google 모바일 비전 (GMV)에서 iOS의 ML Kit로 이전하는 데 필요한 단계를 설명합니다.

기본 요건

코드 이전을 시작하기 전에 다음 요구사항을 충족하는지 확인하세요.

  • ML Kit는 Xcode 13.2.1 이상을 지원합니다.
  • ML Kit는 iOS 버전 15.5 이상을 지원합니다.
  • ML Kit는 32비트 아키텍처 (i386 및 armv7)를 지원하지 않습니다. ML Kit는 64비트 아키텍처 (x86_64 및 arm64)를 지원합니다.

cocoapods 업데이트

앱의 Podfile에서 ML Kit iOS CocoaPods의 종속 항목을 업데이트합니다.

APIGMV 포드ML Kit 포드
바코드 스캔 GoogleMobileVision/BarcodeDetector GoogleMLKit/BarcodeScanning
얼굴 인식 GoogleMobileVision/FaceDetector GoogleMLKit/FaceDetection
텍스트 인식 GoogleMobileVision/TextDetector GoogleMLKit/TextRecognition

전반적인 API 변경사항

이 변경사항은 모든 API에 적용됩니다.

  • GMV의 추론 API는 UIImage 또는 CMSampleBufferRef를 입력으로 사용합니다. ML Kit는 이를 MLKVisionImage로 래핑하고 이를 입력으로 사용합니다.
  • GMV는 NSDictionary를 사용하여 다양한 감지기 옵션을 전달합니다. ML Kit는 이 목적으로 전용 옵션 클래스를 사용합니다.
  • GMV는 감지기를 만들 때 감지기 유형을 단일 GMVDetector 클래스에 전달합니다. ML Kit는 전용 클래스를 사용하여 별도의 감지기, 스캐너, 인식기 인스턴스를 만듭니다.
  • GMV의 API는 동기식 감지만 지원합니다. ML Kit의 추론 API는 동기식 및 비동기식으로 호출할 수 있습니다.
  • GMV는 AVCaptureVideoDataOutput를 확장하고 동시에 여러 감지를 실행하기 위한 다중 감지기 프레임워크를 제공합니다. ML Kit는 이러한 메커니즘을 제공하지 않지만 원하는 경우 개발자가 동일한 기능을 구현할 수 있습니다.

API별 변경사항

이 섹션에서는 각 Vision API에 해당하는 GMV 및 ML Kit 클래스와 메서드를 설명하고 API를 초기화하는 방법을 보여줍니다.

FaceDetector

다음 예와 같이 초기화를 다시 코딩합니다.

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에는 두 가지 감지 API가 있습니다. 둘 다 동기 작업입니다.

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

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

GMVDetectorMLKFaceDetector로 바꿉니다. 추론 API는 동기식 또는 비동기식으로 호출할 수 있습니다.

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

다음 클래스, 메서드, 이름을 변경합니다.

BarcodeDetector

다음 예와 같이 초기화를 다시 코딩합니다.

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에는 두 가지 감지 API가 있습니다. 둘 다 동기 작업입니다.

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

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

GMVDetectorMLKBarcodeScanner로 바꿉니다. 추론 API는 동기식 또는 비동기식으로 호출할 수 있습니다.

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

다음 클래스, 메서드, 이름을 변경합니다.

TextRecognition

다음 예와 같이 초기화를 다시 코딩합니다.

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

GMVDetector에는 두 가지 감지 API가 있습니다. 둘 다 동기 작업입니다.

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

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

GMVDetectorMLKTextRecognizer로 바꿉니다. 추론 API는 동기식 또는 비동기식으로 호출할 수 있습니다.

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

다음 클래스, 메서드, 이름을 변경합니다.

도움 받기

문제가 발생하면 YouTube에 문의할 수 있는 채널이 안내된 커뮤니티 페이지를 확인하세요.