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

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

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

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

- (nullable NSAr<ray__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:));

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

GMV ML Kit
GMVFaceFeature MLKFace
GMVFaceContour MLKFaceContour
GMVDetectorImageOrientation MLKVisionImage.orientation
얼굴 감지 옵션 NSDictionary 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

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

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

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

- (nullable NSAr<ray__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:));

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

GMV ML Kit
GMVDetectorImageOrientation MLKVisionImage.orientation
바코드 감지기 옵션 NSDictionary 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

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

GMV

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

ML Kit

MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];

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

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

- (nullable NSAr<ray__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:));

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

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

도움 받기

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