ML Kit を使用すると、画像内にあるよく知られたランドマークを認識できます。
始める前に
- まだアプリに Firebase を追加していない場合は、スタートガイドの手順に沿って追加してください。
- Podfile に ML Kit ライブラリを含めます。pod 'Firebase/MLVision', '6.25.0' .xcworkspaceを使用して Xcode プロジェクトを開くようにしてください。
- アプリに Firebase をインポートします。
  Swiftimport Firebase Objective-C@import Firebase; 
- 
  プロジェクトで Cloud ベースの API をまだ有効にしていない場合は、ここで有効にします。 - Firebase コンソールの ML Kit API ページを開きます。
- 
      まだプロジェクトを Blaze 料金プランにアップグレードしていない場合は、[アップグレード] をクリックしてアップグレードします(プロジェクトをアップグレードするよう求められるのは、プロジェクトが Blaze プランでない場合のみです)。 Blaze レベルのプロジェクトだけが Cloud ベースの API を使用できます。 
- Cloud ベースの API がまだ有効になっていない場合は、[Cloud ベースの API を有効化] をクリックします。
 
ランドマーク検出ツールを構成する
デフォルトでは、Cloud 検出ツールは安定バージョンのモデルを使用して、最大 10 件の結果を返します。これらの設定のいずれかを変更する場合は、次の例のように VisionCloudDetectorOptions オブジェクトを使用して指定します。
Swift
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
Objective-C
FIRVisionCloudDetectorOptions *options = [[FIRVisionCloudDetectorOptions alloc] init]; options.modelType = FIRVisionCloudModelTypeLatest; options.maxResults = 20;
次のステップでは、Cloud 検出ツール オブジェクトを作成するときに VisionCloudDetectorOptions オブジェクトを渡します。
ランドマーク検出ツールを実行する
画像内のランドマークを認識するには、画像をUIImage または CMSampleBufferRef として VisionCloudLandmarkDetector の detect(in:) メソッドに渡します。
- VisionCloudLandmarkDetectorのインスタンスを取得します。- Swift- lazy var vision = Vision.vision() let cloudDetector = vision.cloudLandmarkDetector(options: options) // Or, to use the default settings: // let cloudDetector = vision.cloudLandmarkDetector() - Objective-C- FIRVision *vision = [FIRVision vision]; FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector]; // Or, to change the default settings: // FIRVisionCloudLandmarkDetector *landmarkDetector = // [vision cloudLandmarkDetectorWithOptions:options]; 
- 
    UIImageまたはCMSampleBufferRefを使用してVisionImageオブジェクトを作成します。UIImageを使用するには:- 必要に応じて、imageOrientationプロパティが.upになるように画像を回転させます。
- 適切に回転させた UIImageを使用してVisionImageオブジェクトを作成します。回転メタデータにはデフォルト値の.topLeftを使用する必要があるため、値を指定しないでください。Swiftlet image = VisionImage(image: uiImage) Objective-CFIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage]; 
 CMSampleBufferRefを使用するには:- 
    VisionImageMetadataオブジェクトを作成し、CMSampleBufferRefバッファに格納されている画像データの向きを指定します。画像の向きは次のように取得します。 Swiftfunc imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> VisionDetectorImageOrientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftTop : .rightTop case .landscapeLeft: return cameraPosition == .front ? .bottomLeft : .topLeft case .portraitUpsideDown: return cameraPosition == .front ? .rightBottom : .leftBottom case .landscapeRight: return cameraPosition == .front ? .topRight : .bottomRight case .faceDown, .faceUp, .unknown: return .leftTop } } Objective-C- (FIRVisionDetectorImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationLeftTop; } else { return FIRVisionDetectorImageOrientationRightTop; } case UIDeviceOrientationLandscapeLeft: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationBottomLeft; } else { return FIRVisionDetectorImageOrientationTopLeft; } case UIDeviceOrientationPortraitUpsideDown: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationRightBottom; } else { return FIRVisionDetectorImageOrientationLeftBottom; } case UIDeviceOrientationLandscapeRight: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationTopRight; } else { return FIRVisionDetectorImageOrientationBottomRight; } default: return FIRVisionDetectorImageOrientationTopLeft; } } 次に、メタデータ オブジェクトを作成します。 Swiftlet cameraPosition = AVCaptureDevice.Position.back // Set to the capture device you used. let metadata = VisionImageMetadata() metadata.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition ) Objective-CFIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init]; AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionBack; // Set to the capture device you used. metadata.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition]; 
- VisionImageオブジェクトと回転メタデータを使用して- CMSampleBufferRefオブジェクトを作成します。- Swift- let image = VisionImage(buffer: sampleBuffer) image.metadata = metadata - Objective-C- FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer]; image.metadata = metadata; 
 
- 必要に応じて、
- 
    次に、画像を detect(in:)メソッドに渡します。SwiftcloudDetector.detect(in: visionImage) { landmarks, error in guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else { // ... return } // Recognized landmarks // ... } Objective-C[landmarkDetector detectInImage:image completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks, NSError *error) { if (error != nil) { return; } else if (landmarks != nil) { // Got landmarks } }]; 
認識されたランドマークに関する情報を取得する
ランドマークの認識に成功すると、VisionCloudLandmark オブジェクトの配列が完了ハンドラに渡されます。各オブジェクトから、画像内で認識されたランドマークに関する情報を取得できます。
次に例を示します。
Swift
for landmark in landmarks { let landmarkDesc = landmark.landmark let boundingPoly = landmark.frame let entityId = landmark.entityId // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for location in landmark.locations { let latitude = location.latitude let longitude = location.longitude } let confidence = landmark.confidence }
Objective-C
for (FIRVisionCloudLandmark *landmark in landmarks) { NSString *landmarkDesc = landmark.landmark; CGRect frame = landmark.frame; NSString *entityId = landmark.entityId; // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for (FIRVisionLatitudeLongitude *location in landmark.locations) { double latitude = [location.latitude doubleValue]; double longitude = [location.longitude doubleValue]; } float confidence = [landmark.confidence floatValue]; }
次のステップ
- Cloud APIs を使用するアプリを本番環境にデプロイする前に、不正な API アクセスを防いでその影響を軽減するため、いくつかの追加手順が必要になります。