iOS에서 커스텀 분류 모델을 사용하여 객체 감지, 추적, 분류

ML Kit를 사용하면 연속된 동영상 프레임에서 객체를 감지하고 추적할 수 있습니다.

이미지를 ML Kit에 전달하면 이미지에서 최대 5개의 객체를 감지합니다. 이미지의 각 객체의 위치와 함께 표시됩니다. Kubernetes에서 객체를 감지할 때는 각 객체에는 객체를 추적하는 데 사용할 수 있는 고유 ID가 있습니다. 확인할 수 있습니다

커스텀 이미지 분류 모델을 사용하여 있습니다. 자세한 내용은 ML Kit를 사용한 커스텀 모델을 참조하세요. 모델 호환성 요구사항, 선행 학습된 모델을 찾을 수 있는 위치, 자체 모델을 학습시키는 방법을 알아봅니다.

커스텀 모델을 통합하는 방법에는 두 가지가 있습니다. 다음을 기준으로 모델을 번들로 묶을 수 있습니다. 앱의 애셋 폴더에 넣거나 동적으로 다운로드할 수 있습니다. 있습니다. 다음 표에서는 두 가지 옵션을 비교합니다.

번들 모델 호스팅된 모델
모델은 앱의 .ipa 파일에 포함되어 있으며, 크기가 커집니다 모델이 앱의 .ipa 파일에 포함되어 있지 않습니다. 그것은 에 업로드하여 호스팅 Firebase 머신러닝.
Android 기기가 오프라인 상태일 때도 모델을 즉시 사용할 수 있습니다. 모델이 요청 시 다운로드됩니다.
Firebase 프로젝트가 필요하지 않음 Firebase 프로젝트 필요
모델을 업데이트하려면 앱을 다시 게시해야 합니다. 앱을 다시 게시할 필요 없이 모델 업데이트 푸시
A/B 테스트 기본 제공 없음 Firebase 원격 구성으로 간편하게 A/B 테스트 진행

사용해 보기

시작하기 전에

  1. Podfile에 ML Kit 라이브러리를 포함합니다.

    모델을 앱과 번들로 묶는 방법은 다음과 같습니다.

    pod 'GoogleMLKit/ObjectDetectionCustom', '3.2.0'
    

    Firebase에서 모델을 동적으로 다운로드하려면 LinkFirebase를 추가합니다. 종속됩니다.

    pod 'GoogleMLKit/ObjectDetectionCustom', '3.2.0'
    pod 'GoogleMLKit/LinkFirebase', '3.2.0'
    
  2. 프로젝트의 포드를 설치하거나 업데이트한 후 Xcode 프로젝트를 엽니다. .xcworkspace를 사용하여 호출 ML Kit는 Xcode 버전 13.2.1에서 지원됩니다. 또는 그 이상일 수 있습니다.

  3. 모델을 다운로드하려면 iOS 프로젝트에 Firebase 추가 확인하세요. 이 작업은 있습니다.

1. 모델 로드

로컬 모델 소스 구성

모델을 앱과 번들로 묶으려면 다음 안내를 따르세요.

  1. 모델 파일 (일반적으로 .tflite 또는 .lite로 끝남)을 Xcode에 복사합니다. 이 경우 Copy bundle resources를 선택해야 합니다. 이 모델 파일이 App Bundle에 포함되며 ML Kit에서 사용할 수 있게 됩니다.

  2. 모델 파일의 경로를 지정하여 LocalModel 객체를 만듭니다.

    Swift

    let localModel = LocalModel(path: localModelFilePath)

    Objective-C

    MLKLocalModel *localModel =
        [[MLKLocalModel alloc] initWithPath:localModelFilePath];
를 통해 개인정보처리방침을 정의할 수 있습니다.

Firebase 호스팅 모델 소스 구성

원격 호스팅 모델을 사용하려면 CustomRemoteModel 객체를 만듭니다. 모델을 게시할 때 할당한 이름 지정:

Swift

let firebaseModelSource = FirebaseModelSource(
    name: "your_remote_model") // The name you assigned in
                               // the Firebase console.
let remoteModel = CustomRemoteModel(remoteModelSource: firebaseModelSource)

Objective-C

MLKFirebaseModelSource *firebaseModelSource =
    [[MLKFirebaseModelSource alloc]
        initWithName:@"your_remote_model"]; // The name you assigned in
                                            // the Firebase console.
MLKCustomRemoteModel *remoteModel =
    [[MLKCustomRemoteModel alloc]
        initWithRemoteModelSource:firebaseModelSource];

그런 다음 모델을 다운로드할 조건을 지정하여 모델 다운로드 작업을 선택합니다. 모델이 기기에 없거나 최신 사용 가능해지면 태스크는 DAG를 비동기식으로 있습니다.

Swift

let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)

Objective-C

MLKModelDownloadConditions *downloadConditions =
    [[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

NSProgress *downloadProgress =
    [[MLKModelManager modelManager] downloadModel:remoteModel
                                       conditions:downloadConditions];

대부분의 앱은 초기화 코드에서 다운로드 작업을 시작하지만 모델을 사용하기 전에 언제든지 이 작업을 할 수 있습니다.

2. 객체 감지기 구성

모델 소스를 구성한 후 사용 사례를 CustomObjectDetectorOptions 객체로 전달합니다. 언제든지 설정할 수 있습니다.

객체 감지기 설정
감지 모드 STREAM_MODE (기본값) | SINGLE_IMAGE_MODE

STREAM_MODE (기본값)에서는 객체 감지기가 실행됩니다. 지연 시간이 짧지만 불완전한 결과 (예: 지정되지 않은 경계 상자 또는 카테고리 라벨)이 있습니다. 감지기 호출을 나타냅니다. 또한 STREAM_MODE에서 감지기는 객체에 추적 ID를 할당하며, 이 ID를 사용하여 여러 프레임 간에 객체를 추적할 수 없습니다. 이 모드를 사용하면 객체 또는 처리 시간처럼 짧은 지연 시간이 중요한 경우 실시간 동영상 스트리밍도 지원합니다

SINGLE_IMAGE_MODE에서 객체 인식기는 다음을 반환합니다. 결과를 나타냅니다. 만약 분류를 사용 설정할 수도 있습니다. 이는 경계선 이후에 결과를 반환합니다. 상자와 카테고리 라벨을 모두 사용할 수 있습니다. 결과적으로, 감지 지연 시간이 더 길 수 있습니다 또한 SINGLE_IMAGE_MODE, 추적 ID가 할당되지 않았습니다. 사용 이 모드는 지연 시간이 중요하지 않고 확인할 수 있습니다.

여러 객체 감지 및 추적 false (기본값) | true

최대 5개의 객체를 감지하고 추적할지, 아니면 가장 많은 객체만 감지하고 추적할지 여부 가시도 높은 객체 (기본값).

객체 분류 false (기본값) | true

제공된 커스텀 분류 기준 모델을 사용합니다. 맞춤 분류를 사용하려면 다음 단계를 따르세요. true로 설정해야 합니다.

분류 신뢰도 임곗값

감지된 라벨의 최소 신뢰도 점수입니다. 설정하지 않으면 모델의 메타데이터에 지정된 분류 기준 임곗값이 사용됩니다. 모델에 메타데이터가 없거나 메타데이터가 포함되지 않은 경우 분류 기준 임곗값을 지정하면 기본 임곗값인 0.0이 있습니다.

객체당 최대 라벨 수

인식기가 실행하는 객체당 최대 라벨 수 반환합니다. 설정하지 않으면 기본값 10이 사용됩니다.

로컬로 번들된 모델만 있다면 다음 위치에서 객체 감지기를 만듭니다. LocalModel 객체:

Swift

let options = CustomObjectDetectorOptions(localModel: localModel)
options.detectorMode = .singleImage
options.shouldEnableClassification = true
options.shouldEnableMultipleObjects = true
options.classificationConfidenceThreshold = NSNumber(value: 0.5)
options.maxPerObjectLabelCount = 3

Objective-C

MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableClassification = YES;
options.shouldEnableMultipleObjects = YES;
options.classificationConfidenceThreshold = @(0.5);
options.maxPerObjectLabelCount = 3;

원격 호스팅 모델이 있는 경우에는 모델이 다운로드할 수 있습니다. 모델 다운로드 상태를 확인할 수 있습니다. 모델 관리자의 isModelDownloaded(remoteModel:) 메서드를 사용하여 태스크를 수행합니다.

객체 감지기를 실행하기 전에만 이를 확인하면 되지만, 원격 호스팅 모델과 로컬로 번들된 모델이 모두 있는 경우 ObjectDetector를 인스턴스화할 때 이 확인을 실행하는 것이 좋습니다. 다운로드한 경우 원격 모델에서, 로컬 모델에서 감지기를 가져옵니다. 없습니다.

Swift

var options: CustomObjectDetectorOptions!
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = CustomObjectDetectorOptions(remoteModel: remoteModel)
} else {
  options = CustomObjectDetectorOptions(localModel: localModel)
}
options.detectorMode = .singleImage
options.shouldEnableClassification = true
options.shouldEnableMultipleObjects = true
options.classificationConfidenceThreshold = NSNumber(value: 0.5)
options.maxPerObjectLabelCount = 3

Objective-C

MLKCustomObjectDetectorOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[MLKCustomObjectDetectorOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
}
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableClassification = YES;
options.shouldEnableMultipleObjects = YES;
options.classificationConfidenceThreshold = @(0.5);
options.maxPerObjectLabelCount = 3;

원격 호스팅 모델만 있는 경우 모델 관련 사용을 중지해야 합니다. 비활성화되거나 UI의 일부를 숨기는 등의 작업을 할 수 없습니다. 모델이 다운로드되었음을 확인합니다.

기본값에 관찰자를 연결하여 모델 다운로드 상태를 가져올 수 있습니다. 알림 센터를 탭합니다. 관찰자에서 self의 약한 참조를 사용해야 합니다. 다운로드하는 데 시간이 걸릴 수 있고 원래 객체는 다운로드가 완료되면 해제됩니다. 예를 들면 다음과 같습니다.

Swift

NotificationCenter.default.addObserver(
    forName: .mlkitModelDownloadDidSucceed,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel,
        model.name == "your_remote_model"
        else { return }
    // The model was downloaded and is available on the device
}

NotificationCenter.default.addObserver(
    forName: .mlkitModelDownloadDidFail,
    object: nil,
    queue: nil
) { [weak self] notification in
    guard let strongSelf = self,
        let userInfo = notification.userInfo,
        let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
            as? RemoteModel
        else { return }
    let error = userInfo[ModelDownloadUserInfoKey.error.rawValue]
    // ...
}

Objective-C

__weak typeof(self) weakSelf = self;

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidSucceedNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              MLKRemoteModel *model = note.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
              if ([model.name isEqualToString:@"your_remote_model"]) {
                // The model was downloaded and is available on the device
              }
            }];

[NSNotificationCenter.defaultCenter
    addObserverForName:MLKModelDownloadDidFailNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              NSError *error = note.userInfo[MLKModelDownloadUserInfoKeyError];
            }];

객체 감지 및 추적 API는 이 두 가지 핵심 용도에 최적화되어 있습니다. 사례:

  • 카메라에서 가장 눈에 띄는 물체를 실시간으로 감지하고 추적합니다. 뷰파인더를 사용할 수 있습니다.
  • 정적 이미지에서 여러 객체를 감지합니다.

이러한 사용 사례에 맞게 API를 구성하려면 다음 안내를 따르세요.

Swift

// Live detection and tracking
let options = CustomObjectDetectorOptions(localModel: localModel)
options.shouldEnableClassification = true
options.maxPerObjectLabelCount = 3

// Multiple object detection in static images
let options = CustomObjectDetectorOptions(localModel: localModel)
options.detectorMode = .singleImage
options.shouldEnableMultipleObjects = true
options.shouldEnableClassification = true
options.maxPerObjectLabelCount = 3

Objective-C

// Live detection and tracking
MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.shouldEnableClassification = YES;
options.maxPerObjectLabelCount = 3;

// Multiple object detection in static images
MLKCustomObjectDetectorOptions *options =
    [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
options.detectorMode = MLKObjectDetectorModeSingleImage;
options.shouldEnableMultipleObjects = YES;
options.shouldEnableClassification = YES;
options.maxPerObjectLabelCount = 3;

3. 입력 이미지 준비

VisionImage 객체를 UIImage 또는 CMSampleBuffer입니다.

UIImage를 사용하는 경우 다음 단계를 따르세요.

  • UIImageVisionImage 객체를 만듭니다. 올바른 .orientation를 지정해야 합니다.

    Swift

    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation

    Objective-C

    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

CMSampleBuffer를 사용하는 경우 다음 단계를 따르세요.

  • CMSampleBuffer

    이미지 방향을 가져오는 방법은 다음과 같습니다.

    Swift

    func imageOrientation(
      deviceOrientation: UIDeviceOrientation,
      cameraPosition: AVCaptureDevice.Position
    ) -> UIImage.Orientation {
      switch deviceOrientation {
      case .portrait:
        return cameraPosition == .front ? .leftMirrored : .right
      case .landscapeLeft:
        return cameraPosition == .front ? .downMirrored : .up
      case .portraitUpsideDown:
        return cameraPosition == .front ? .rightMirrored : .left
      case .landscapeRight:
        return cameraPosition == .front ? .upMirrored : .down
      case .faceDown, .faceUp, .unknown:
        return .up
      }
    }
          

    Objective-C

    - (UIImageOrientation)
      imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                             cameraPosition:(AVCaptureDevicePosition)cameraPosition {
      switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored
                                                                : UIImageOrientationRight;
    
        case UIDeviceOrientationLandscapeLeft:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored
                                                                : UIImageOrientationUp;
        case UIDeviceOrientationPortraitUpsideDown:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored
                                                                : UIImageOrientationLeft;
        case UIDeviceOrientationLandscapeRight:
          return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored
                                                                : UIImageOrientationDown;
        case UIDeviceOrientationUnknown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
          return UIImageOrientationUp;
      }
    }
          
  • 다음을 사용하여 VisionImage 객체를 만듭니다. CMSampleBuffer 객체 및 방향:

    Swift

    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: cameraPosition)

    Objective-C

     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image.orientation =
       [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition:cameraPosition];

4. 객체 감지기 만들기 및 실행

  1. 새 객체 인식기를 만듭니다.

    Swift

    let objectDetector = ObjectDetector.objectDetector(options: options)

    Objective-C

    MLKObjectDetector *objectDetector = [MLKObjectDetector objectDetectorWithOptions:options];
  2. 그런 다음 다음과 같이 감지기를 사용합니다.

    비동기식으로:

    Swift

    objectDetector.process(image) { objects, error in
        guard error == nil, let objects = objects, !objects.isEmpty else {
            // Handle the error.
            return
        }
        // Show results.
    }

    Objective-C

    [objectDetector
        processImage:image
          completion:^(NSArray *_Nullable objects,
                       NSError *_Nullable error) {
            if (objects.count == 0) {
                // Handle the error.
                return;
            }
            // Show results.
         }];

    동기식으로:

    Swift

    var objects: [Object]
    do {
        objects = try objectDetector.results(in: image)
    } catch let error {
        // Handle the error.
        return
    }
    // Show results.

    Objective-C

    NSError *error;
    NSArray *objects =
        [objectDetector resultsInImage:image error:&error];
    // Show results or handle the error.

5. 라벨이 지정된 객체 정보 가져오기

이미지 프로세서 호출이 성공하면 이미지 프로세서는 Object를 완료 핸들러에 추가하거나 호출했는지 여부에 따라 달라집니다.

Object에는 다음 속성이 포함됩니다.

frame 객체의 위치를 나타내는 CGRect: 이미지
trackingID 여러 이미지에서 객체를 식별하는 정수 또는 다음에서 `nil` 입니다. SINGLE_IMAGE_MODE여야 합니다.
labels
label.text 라벨의 텍스트 설명입니다. TensorFlow가 라이트 모델의 메타데이터에 라벨 설명이 포함되어 있습니다.
label.index 지원되는 모든 라벨 중 라벨의 색인 분류 기준입니다.
label.confidence 객체 분류의 신뢰도 값입니다.

Swift

// objects contains one item if multiple object detection wasn't enabled.
for object in objects {
  let frame = object.frame
  let trackingID = object.trackingID
  let description = object.labels.enumerated().map { (index, label) in
    "Label \(index): \(label.text), \(label.confidence), \(label.index)"
  }.joined(separator: "\n")
}

Objective-C

// The list of detected objects contains one item if multiple object detection
// wasn't enabled.
for (MLKObject *object in objects) {
  CGRect frame = object.frame;
  NSNumber *trackingID = object.trackingID;
  for (MLKObjectLabel *label in object.labels) {
    NSString *labelString =
        [NSString stringWithFormat:@"%@, %f, %lu",
                                   label.text,
                                   label.confidence,
                                   (unsigned long)label.index];
  }
}

우수한 사용자 경험 보장

최상의 사용자 환경을 위해 앱에서 다음 가이드라인을 따르세요.

  • 객체 감지 성공 여부는 객체의 시각적 복잡성에 따라 달라집니다. 포함 감지하기 위해서는 시각적 특징이 적은 물체는 이미지의 더 큰 부분을 차지합니다. 사용자에게 감지하려는 객체 종류에서 잘 작동하는 입력을 캡처하는 것입니다.
  • 분류를 사용할 때 떨어지지 않는 물체를 감지하려는 경우 지원되는 카테고리로 깔끔하게 분류하고 알려지지 않은 특수 처리를 구현하세요. 객체입니다.

또한 [ML Kit 머티리얼 디자인 쇼케이스 앱][showcase-link]{: .external } 및 머티리얼 디자인 머신러닝 기반의 특성 수집 패턴

성능 개선

실시간 애플리케이션에서 객체 감지를 사용하려면 다음 안내를 따르세요. 다음 가이드라인을 참조하세요.

  • 실시간 애플리케이션에서 스트리밍 모드를 사용할 때는 객체 감지가 작동하지 않습니다.

  • 동영상 프레임을 처리하려면 감지기의 results(in:) 동기 API를 사용하세요. 전화걸기 AVCaptureVideoDataOutputSampleBufferDelegate님의 <ph type="x-smartling-placeholder"></ph> captureOutput(_, didOutput:from:) 함수를 사용하여 특정 동영상에서 동기식으로 결과를 가져옵니다. 있습니다. 유지 AVCaptureVideoDataOutput님의 alwaysDiscardsLateVideoFramestrue로 설정하여 감지기 호출을 제한합니다. 새 동영상 프레임은 감지기가 실행되는 동안 사용할 수 있게 되면 삭제됩니다.
  • 감지기 출력을 사용하여 그래픽 이미지를 먼저 ML Kit에서 결과를 가져온 후 이미지를 하나의 단계로 오버레이할 수 있습니다. 이렇게 하면 처리되어야 합니다 자세한 내용은 updatePreviewOverlayViewWithLastFrame을 를 참조하세요.