iOS 向けの移行

前提条件

コードの移行を開始する前に、次の要件を満たしていることを確認してください。

  • ML Kit は Xcode 13.2.1 以降をサポートしています。
  • ML Kit は iOS バージョン 10 以降をサポートしています。
  • ML Kit は 32 ビット アーキテクチャ(i386 と armv7)をサポートしていません。ML Kit は 64 ビット アーキテクチャ(x86_64 と arm64)をサポートしています。
  • ML Kit ライブラリは cocoapods としてのみ提供されています。フレームワークと cocoapods を混在させることはできないため、このライブラリを使用するには、まず cocoapods を使用するように移行する必要があります。

Cocoapods を更新する

アプリの Podfile で、ML Kit の iOS cocoapods の依存関係を更新します。

API古い Pod 名新しい Pod 名
バーコード スキャン Firebase/MLVision
Firebase/MLVisionBarcodeModel
GoogleMLKit/BarcodeScanning
顔検出 Firebase/MLVision
Firebase/MLVisionFaceModel
GoogleMLKit/FaceDetection
画像ラベル付け Firebase/MLVision
Firebase/MLVisionLabelModel
GoogleMLKit/ImageLabeling
オブジェクトの検出とトラッキング Firebase/MLVisionObjectDetection GoogleMLKit/ObjectDetection
テキスト認識 Firebase/MLVision
Firebase/MLVisionTextModel
GoogleMLKit/TextRecognition
AutoML 画像のラベル付け(バンドルモデル) Firebase/MLVisionAutoML GoogleMLKit/ImageLabelingCustom
AutoML 画像のラベル付け(Firebase からのモデルのダウンロード) Firebase/MLVisionAutoML GoogleMLKit/ImageLabelingCustom
GoogleMLKit/LinkFirebase
言語 ID Firebase/MLNaturalLanguage
Firebase/MLNLLanguageID
GoogleMLKit/LanguageID
スマート リプライ Firebase/MLNaturalLanguage
Firebase/MLNLSmartReply
GoogleMLKit/SmartReply
翻訳する Firebase/MLNaturalLanguage
Firebase/MLNLTranslate
GoogleMLKit/Translate

クラス、列挙型、型の名前を更新する

一般に、クラス、列挙型、型の名前は、次のように変更する必要があります。

  • Swift: クラス名と列挙型から Vision 接頭辞を削除する
  • Objective-C: クラス名と列挙型接頭辞を FIRVisionFIR の両方を MLK に置き換える

一部のクラス名と型には、この一般的なルールは適用されません。

Swift

以前のクラスまたは型新しいクラスまたは型
AutoMLLocalModel LocalModel
AutoMLRemoteModel CustomRemoteModel
VisionBarcodeDetectionCallback BarcodeScanningCallback
VisionBarcodeDetector BarcodeScanner
VisionBarcodeDetectorOptions BarcodeScannerOptions
VisionImage VisionImage(変更なし)
VisionPoint VisionPoint(変更なし)
VisionOnDeviceAutoMLImageLabelerOptions CustomImageLabelerOptions
VisionOnDeviceImageLabelerOptions ImageLabelerOptions

Objective-C

以前のクラスまたは型新しいクラスまたは型
FIRAutoMLLocalModel MLKLocalModel
FIRAutoMLRemoteModel MLKCustomRemoteModel
FIRVisionBarcodeDetectionCallback MLKBarcodeScanningCallback
FIRVisionBarcodeDetector MLKBarcodeScanner
FIRVisionBarcodeDetectorOptions MLKBarcodeScannerOptions
FIRVisionImage MLKVisionImage
FIRVisionOnDeviceAutoMLImageLabelerOptions MLKCustomImageLabelerOptions
FIRVisionOnDeviceImageLabelerOptions MLKImageLabelerOptions
FIRVisionPoint MLKVisionPoint

Objective-C

メソッド名を更新する

次のルールに従ってメソッド名を更新します。

  • ドメイン エントリ ポイント クラス(VisionNaturalLanguage)が存在しません。これらは、タスク固有のクラスに置き換えられました。 検出機能を取得するための各種のファクトリ メソッドの呼び出しを、各検出機能のファクトリ メソッドの直接呼び出しに置き換えます。

  • VisionImageMetadata クラスと VisionDetectorImageOrientation 列挙型が削除されました。VisionImageorientation プロパティを使用して、画像の表示方向を指定します。

  • 新しい TextRecognizer インスタンスを取得する onDeviceTextRecognizer メソッドの名前が textRecognizer に変更されました。

  • テキスト認識結果クラス(TextElementTextLineTextBlock など)から信頼プロパティが削除されました。

  • 新しい ImageLabeler インスタンスを取得するための onDeviceImageLabeler メソッドと onDeviceImageLabeler(options:) メソッドが統合され、名前が imageLabeler(options:) に変更されました。

  • 新しい ObjectDetector インスタンスを取得する objectDetector メソッドが削除されました。代わりに objectDetector(options:) を使用してください。

  • type プロパティが ImageLabeler から削除され、画像ラベル付け結果クラス ImageLabel から entityID プロパティが削除されました。

  • 他の Vision API に合わせて、バーコード スキャン API detect(in _:, completion:) の名前が process(_:, completion:) に変更されました。

  • Natural Language API では、「言語コード」の代わりに「言語タグ」(BCP-47 標準で定義されている)という用語が使用されるようになりました。

  • TranslateLanguage は、定数に言語タグ(.en など)ではなく、判読可能な名前(.english など)を使用するようになりました。

新旧の Swift メソッドの例を以下に示します。

古い

let options = VisionOnDeviceImageLabelerOptions()
options.confidenceThreshold = 0.75
let labeler = Vision.vision().onDeviceImageLabeler(options: options)

let detector = Vision.vision().faceDetector(options: options)

let localModel = AutoMLLocalModel(manifestPath: "automl/manifest.json")
let options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = 0.75
let labeler = vision.onDeviceAutoMLImageLabeler(options: options)

let detector = Vision.vision().objectDetector()
    

新規

let options = ImageLabelerOptions()
options.confidenceThreshold = NSNumber(value:0.75)
let labeler = ImageLabeler.imageLabeler(options: options)

let detector = FaceDetector.faceDetector(options: options)

let localModel = LocalModel(manifestPath: "automl/manifest.json")
let options = CustomImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value:0.75)
let labeler = ImageLabeler.imageLabeler(options: options)

let detector = ObjectDetector.objectDetector(options: ObjectDetectorOptions())
    

新旧の Objective-C メソッドの例を以下に示します。

古い

FIRVisionOnDeviceImageLabelerOptions *options = 
    [[FIRVisionOnDeviceImageLabelerOptions alloc] init];
options.confidenceThreshold = 0.75;
FIRVisionImageLabeler *labeler = 
    [[FIRVision vision] onDeviceImageLabelerWithOptions:options];

FIRVisionFaceDetector *detector =
    [[FIRVision vision] faceDetectorWithOptions: options];

FIRAutoMLLocalModel *localModel =
    [[FIRAutoMLLocalModel alloc] initWithManifestPath:@"automl/manifest.json"];
FIRVisionOnDeviceAutoMLImageLabelerOptions *options =
    [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc]
        initWithLocalModel: localModel];
options.confidenceThreshold = 0.75
FIRVisionImageLabeler *labeler =
    [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];

FIRVisionObjectDetector *detector =
    [[FIRVision vision] objectDetector];
    

新規

MLKImageLabelerOptions *options =
    [[MLKImageLabelerOptions alloc] init];
options.confidenceThreshold = @(0.75);
MLKImageLabeler *labeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

MLKFaceDetector *detector =
    [MLKFaceDetector faceDetectorWithOptions:options];

MLKLocalModel *localModel =
    [[MLKLocalModel alloc]
        initWithManifestPath:@"automl/manifest.json"];
MLKCustomImageLabelerOptions *options =
    [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.75)
MLKImageLabeler *labeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

MLKObjectDetectorOptions *options = [[MLKObjectDetectorOptions alloc] init];
MLKObjectDetector *detector = [MLKObjectDetector objectDetectorWithOptions:options];
    

API 固有の変更

オブジェクトの検出とトラッキング

アプリでオブジェクト分類を使用する場合、新しい SDK では検出されたオブジェクトの分類カテゴリを返す方法が変更されています。

VisionObjectVisionObjectCategory は、整数ではなく text として ObjectLabel オブジェクトで返されます。可能なすべての文字列カテゴリが DetectedObjectLabel 列挙型に含まれています。

.unknown カテゴリが削除されたことに注意してください。オブジェクトの分類の信頼度が低い場合、分類器はラベルをまったく返しません。

新旧の Swift コードの例を次に示します。

古い

if (object.classificationCategory == .food) {
    ...
}

新規

if let label = object.labels.first {
  if (label.text == DetectedObjectLabel.food.rawValue) {
    ...
  }
}
// or
if let label = object.labels.first {
  if (label.index == DetectedObjectLabelIndex.food.rawValue) {
    ...
  }
}

新旧の Objective-C コードの例を以下に示します。

古い

if (object.classificationCategory == FIRVisionObjectCategoryFood) {
    ...
}

新規

if ([object.labels[0].text isEqualToString:MLKDetectedObjectLabelFood]) {
  ...
}
// or
if ([object.labels[0].index == MLKDetectedObjectLabelIndexFood]) {
  ...
}

Firebase の依存関係を削除する(省略可)

このステップは、次の条件が満たされている場合にのみ適用されます。

  • 使用する Firebase コンポーネントは Firebase ML Kit のみ
  • オンデバイス API のみを使用する
  • モデル サービングは使用しない

その場合は、移行後に Firebase の依存関係を削除できます。以下の手順に沿って登録してください。

  • アプリのディレクトリと Xcode プロジェクトから GoogleService-Info.plist ファイルを削除して、Firebase 構成ファイルを削除します。
  • Podfile から pod 'Firebase/Analytics' などの Firebase cocoapod を削除します。
  • コードから、FirebaseApp の初期化(FirebaseApp.configure() など)をすべて削除します。
  • Firebase サポートサイトのinstructionsに沿って、Firebase コンソールで Firebase アプリを削除します。

ヘルプ

問題が発生した場合は、コミュニティ ページをご確認ください。お問い合わせに使用できるチャンネルの概要が掲載されています。