Android でカスタムモデルを使用して画像にラベルを付ける

ML Kit を使用すると、画像内のエンティティを認識してラベルを付けることができます。 この API は、幅広いカスタム画像分類モデルをサポートしています。 モデルの互換性要件、事前トレーニング済みモデルの入手先、独自のモデルのトレーニング方法については、ML Kit でのカスタムモデルをご覧ください。

画像ラベリングをカスタムモデルと統合するには、パイプラインをアプリの一部としてバンドルする方法と、Google Play 開発者サービスに依存するバンドルされていないパイプラインを使用する方法の 2 つがあります。バンドルされていないパイプラインを選択すると、アプリのサイズが小さくなります。詳しくは、次の表をご覧ください。

バンドルバンドルされていない
ライブラリ名com.google.mlkit:image-labeling-customcom.google.android.gms:play-services-mlkit-image-labeling-custom

実装
パイプラインはビルド時にアプリに静的にリンクされます。パイプラインは Google Play 開発者サービスを使用して動的にダウンロードされます。
アプリのサイズ約 3.8 MB サイズが増加します。約 200 KB サイズが増加します。
初期化時間パイプラインはすぐに使用できます。初回使用時にパイプラインのダウンロードを待つ必要がある場合があります。
API ライフサイクルのステージ一般提供(GA)ベータ版

カスタムモデルを統合するには、アプリのアセット フォルダにモデルを配置してバンドルする方法と、Firebase から動的にダウンロードする方法の 2 つがあります。次の表は、これら 2 つのオプションを比較したものです。

バンドルモデル ホストされているモデル
モデルはアプリの APK の一部であるため、サイズが大きくなります。 モデルは APK の一部ではありません。Cloud Storage にアップロードすることでホストされます。Cloud Storage for Firebase を使用することをおすすめします。
Android デバイスがオフラインの場合でも、モデルはすぐに使用できます。 アプリには、必要に応じてモデルをダウンロードするコードを含める必要があります。
Firebase プロジェクトは不要 Firebase プロジェクトが必要です(Cloud Storage for Firebase を使用する場合)。
モデルを更新するには、アプリを再公開する必要があります。 アプリを再公開することなくモデルの更新を push できる
組み込みの A/B テストはありません。 Firebase Remote Config を使用した A/B テスト

試してみる

始める前に

  1. プロジェクト レベルの build.gradle.kts ファイルの buildscript セクションと allprojects セクションの両方に Google の Maven リポジトリを組み込みます。

  2. ML Kit Android ライブラリの依存関係をモジュールのアプリレベルの Gradle ファイル(通常は app/build.gradle.kts)に追加します。ニーズに応じて、次のいずれかの依存関係を選択します。

    パイプラインをアプリにバンドルする場合:

    dependencies {
      // ...
      // Use this dependency to bundle the pipeline with your app
      implementation("com.google.mlkit:image-labeling-custom:17.0.3")
    }
    

    Google Play 開発者サービスでパイプラインを使用する場合:

    dependencies {
      // ...
      // Use this dependency to use the dynamically downloaded pipeline in Google Play services
      implementation("com.google.android.gms:play-services-mlkit-image-labeling-custom:16.0.0-beta5")
    }
    
  3. Google Play 開発者サービスでパイプラインを使用する場合、Google Play ストアからアプリがインストールされた後、パイプラインをデバイスに自動的にダウンロードするようにアプリを構成できます。そのためには、アプリの AndroidManifest.xml ファイルに次の宣言を追加します。

    <application ...>
        ...
        <meta-data
            android:name="com.google.mlkit.vision.DEPENDENCIES"
            android:value="custom_ica" />
        <!-- To use multiple downloads: android:value="custom_ica,download2,download3" -->
    </application>
    

    Google Play 開発者サービス ModuleInstallClient API を使用して、パイプラインの可用性を明示的に確認し、ダウンロードをリクエストすることもできます。

    インストール時のパイプラインのダウンロードを有効にしない場合や、明示的なダウンロードをリクエストしない場合は、ラベラーの初回実行時にパイプラインがダウンロードされます。 ダウンロードが完了する前にリクエストしても結果は生成されません。

  4. **Cloud Storage for Firebase を使用してモデルをダウンロードする場合** は、 Firebase を Android プロジェクトに追加します( まだ行っていない場合)。これは、モデルをバンドルする場合には必要ありません。

1. モデルを読み込む

モデルは、ローカル バンドルソースまたはリモートでホストされるソースから読み込むことができます。

ローカル モデルソースを構成する

モデルをアプリにバンドルするには:

  1. モデルファイル(拡張子は通常 .tflite または .lite)をアプリの assets/ フォルダにコピーします (先にこのフォルダを作成する必要がある場合があります。作成するには app/ フォルダを右クリックし、[新規] > [フォルダ] > Assets フォルダ をクリックします)。

  2. モデルファイルへのパスを指定して LocalModel オブジェクトを作成します。

    Kotlin

    val localModel = LocalModel.Builder()
            .setAssetFilePath("model.tflite")
            // or .setAbsoluteFilePath(absolute path to model file)
            // or .setUri(URI to model file)
            .build()

    Java

    LocalModel localModel =
        new LocalModel.Builder()
            .setAssetFilePath("model.tflite")
            // or .setAbsoluteFilePath(absolute path to model file)
            // or .setUri(URI to model file)
            .build();

リモートでホストされるモデルソースを構成する

リモートでホストされるモデルを使用するには、独自のアプリロジックを使用してモデルファイルをデバイスのローカル ストレージにダウンロードし、ローカル モデルとして読み込む必要があります。モデルをホストするには、Cloud Storage for Firebase を使用することをおすすめします。実装の詳細については、 Firebase ML から Cloud Storage への移行ガイドをご覧ください。

画像ラベラーを構成する

モデルソースを構成したら、そのソースのいずれか 1 つから ImageLabeler オブジェクトを作成します。

以下のオプションを使用できます。

オプション
confidenceThreshold

検出されたラベルの最小信頼スコア。設定しない場合、モデルのメタデータで指定された分類子のしきい値が使用されます。モデルにメタデータが含まれていない場合や、メタデータで分類子のしきい値が指定されていない場合は、デフォルトのしきい値 0.0 が使用されます。

maxResultCount

返されるラベルの最大数。設定しない場合、デフォルト値の 10 が使用されます。

ローカル バンドルモデルのみがある場合は、LocalModel オブジェクトからラベラーを作成するだけで済みます。

Kotlin

val customImageLabelerOptions = CustomImageLabelerOptions.Builder(localModel)
    .setConfidenceThreshold(0.5f)
    .setMaxResultCount(5)
    .build()
val labeler = ImageLabeling.getClient(customImageLabelerOptions)

Java

CustomImageLabelerOptions customImageLabelerOptions =
        new CustomImageLabelerOptions.Builder(localModel)
            .setConfidenceThreshold(0.5f)
            .setMaxResultCount(5)
            .build();
ImageLabeler labeler = ImageLabeling.getClient(customImageLabelerOptions);

リモートでホストされるモデルがある場合は、そのモデルを実行する前にダウンロード済みであることを確認する必要があります。

ダウンロードのステータスはラベラーを実行する前に確認するだけで済みますが、リモートでホストされるモデルとローカル バンドルモデルの両方がある場合は、画像ラベラーをインスタンス化する、つまりラベラーを作成するときに確認すると良いかもしれません(リモートモデルをダウンロード済みの場合はリモートモデルから作成、ダウンロードされていない場合はローカルモデルから作成)。

Kotlin

val modelFile = File(context.cacheDir, "my_downloaded_model.tflite")
val model = if (modelFile.exists()) {
    // Use the downloaded model if available
    LocalModel.Builder().setAbsoluteFilePath(modelFile.absolutePath).build()
} else {
    // Fall back to the bundled model
    LocalModel.Builder().setAssetFilePath("model.tflite").build()
}
val options = CustomImageLabelerOptions.Builder(model)
    .setConfidenceThreshold(0.5f)
    .setMaxResultCount(5)
    .build()
val labeler = ImageLabeling.getClient(options)

Java

File modelFile = new File(context.getCacheDir(), "my_downloaded_model.tflite");
LocalModel model;
if (modelFile.exists()) {
    // Use the downloaded model if available
    model = new LocalModel.Builder().setAbsoluteFilePath(modelFile.getAbsolutePath()).build();
} else {
    // Fall back to the bundled model
    model = new LocalModel.Builder().setAssetFilePath("model.tflite").build();
}
CustomImageLabelerOptions options = new CustomImageLabelerOptions.Builder(model)
    .setConfidenceThreshold(0.5f)
    .setMaxResultCount(5)
    .build();
ImageLabeler labeler = ImageLabeling.getClient(options);

リモートでホストされるモデルのみがある場合は、モデルがダウンロード済みであることを確認するまで、モデルに関連する機能を無効にする必要があります(UI の一部をグレー表示または非表示にするなど)。

Kotlin

val localFile = File(context.cacheDir, "my_remote_model.tflite")
if (localFile.exists()) {
    initializeLabeler(localFile)
} else {
    showLoadingUI()
    val storage = Firebase.storage
    val modelRef = storage.getReferenceFromUrl("gs://YOUR_BUCKET/path/to/model.tflite")
    modelRef.getFile(localFile)
        .addOnSuccessListener {
            hideLoadingUI()
            initializeLabeler(localFile)
        }
        .addOnFailureListener {
            showErrorUI()
        }
}

private fun initializeLabeler(modelFile: File) {
    val localModel = LocalModel.Builder().setAbsoluteFilePath(modelFile.absolutePath).build()
    val options = CustomImageLabelerOptions.Builder(localModel).build()
    val labeler = ImageLabeling.getClient(options)
    enableMLFeatures(labeler)
}

Java

File localFile = new File(context.getCacheDir(), "my_remote_model.tflite");
if (localFile.exists()) {
    initializeLabeler(localFile);
} else {
    showLoadingUI();
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference modelRef = storage.getReferenceFromUrl("gs://YOUR_BUCKET/path/to/model.tflite");
    modelRef.getFile(localFile)
        .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                hideLoadingUI();
                initializeLabeler(localFile);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                showErrorUI();
            }
        });
}

private void initializeLabeler(File modelFile) {
    LocalModel localModel = new LocalModel.Builder().setAbsoluteFilePath(modelFile.getAbsolutePath()).build();
    CustomImageLabelerOptions options = new CustomImageLabelerOptions.Builder(localModel).build();
    ImageLabeler labeler = ImageLabeling.getClient(options);
    enableMLFeatures(labeler);
}

2. 入力画像を準備する

次に、ラベルを付ける画像ごとに、画像から InputImage オブジェクトを作成します。Bitmap を使用するか、Camera2 API(YUV_420_888 media.Image)を使用すると、画像ラベラーの処理が速くなります。可能であれば、このフォーマットの使用をおすすめします。

さまざまなソースから InputImage オブジェクトを作成できます。各ソースは次のとおりです。

media.Image の使用

InputImage オブジェクトを media.Image オブジェクトから作成するには(デバイスのカメラから画像をキャプチャする場合など)、media.Image オブジェクトと画像の回転を InputImage.fromMediaImage() に渡します。

CameraX ライブラリを使用する場合は、OnImageCapturedListenerImageAnalysis.Analyzer クラスによって回転値が計算されます。

Kotlin

private class YourImageAnalyzer : ImageAnalysis.Analyzer {

    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy.image
        if (mediaImage != null) {
            val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
            // Pass image to an ML Kit Vision API
            // ...
        }
    }
}

Java

private class YourAnalyzer implements ImageAnalysis.Analyzer {

    @Override
    public void analyze(ImageProxy imageProxy) {
        Image mediaImage = imageProxy.getImage();
        if (mediaImage != null) {
          InputImage image =
                InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
          // Pass image to an ML Kit Vision API
          // ...
        }
    }
}

画像の回転角度を取得するカメラ ライブラリを使用しない場合は、デバイスの回転角度とデバイス内のカメラセンサーの向きから計算できます。

Kotlin

private val ORIENTATIONS = SparseIntArray()

init {
    ORIENTATIONS.append(Surface.ROTATION_0, 0)
    ORIENTATIONS.append(Surface.ROTATION_90, 90)
    ORIENTATIONS.append(Surface.ROTATION_180, 180)
    ORIENTATIONS.append(Surface.ROTATION_270, 270)
}

/**
 * Get the angle by which an image must be rotated given the device's current
 * orientation.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Throws(CameraAccessException::class)
private fun getRotationCompensation(cameraId: String, activity: Activity, isFrontFacing: Boolean): Int {
    // Get the device's current rotation relative to its "native" orientation.
    // Then, from the ORIENTATIONS table, look up the angle the image must be
    // rotated to compensate for the device's rotation.
    val deviceRotation = activity.windowManager.defaultDisplay.rotation
    var rotationCompensation = ORIENTATIONS.get(deviceRotation)

    // Get the device's sensor orientation.
    val cameraManager = activity.getSystemService(CAMERA_SERVICE) as CameraManager
    val sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION)!!

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360
    }
    return rotationCompensation
}

Java

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
    ORIENTATIONS.append(Surface.ROTATION_0, 0);
    ORIENTATIONS.append(Surface.ROTATION_90, 90);
    ORIENTATIONS.append(Surface.ROTATION_180, 180);
    ORIENTATIONS.append(Surface.ROTATION_270, 270);
}

/**
 * Get the angle by which an image must be rotated given the device's current
 * orientation.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private int getRotationCompensation(String cameraId, Activity activity, boolean isFrontFacing)
        throws CameraAccessException {
    // Get the device's current rotation relative to its "native" orientation.
    // Then, from the ORIENTATIONS table, look up the angle the image must be
    // rotated to compensate for the device's rotation.
    int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int rotationCompensation = ORIENTATIONS.get(deviceRotation);

    // Get the device's sensor orientation.
    CameraManager cameraManager = (CameraManager) activity.getSystemService(CAMERA_SERVICE);
    int sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION);

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360;
    }
    return rotationCompensation;
}

次に、media.Image オブジェクトと 回転角度値を InputImage.fromMediaImage() に渡します。

Kotlin

val image = InputImage.fromMediaImage(mediaImage, rotation)

Java

InputImage image = InputImage.fromMediaImage(mediaImage, rotation);

ファイル URI の使用

InputImage オブジェクトをファイルの URI から作成するには、アプリ コンテキストとファイルの URI を InputImage.fromFilePath() に渡します。これは、 ACTION_GET_CONTENT インテントを使用して、写真アプリから画像を選択するようにユーザーに促すときに便利です。

Kotlin

val image: InputImage
try {
    image = InputImage.fromFilePath(context, uri)
} catch (e: IOException) {
    e.printStackTrace()
}

Java

InputImage image;
try {
    image = InputImage.fromFilePath(context, uri);
} catch (IOException e) {
    e.printStackTrace();
}

ByteBuffer または ByteArray の使用

InputImage オブジェクトを ByteBuffer または ByteArray から作成するには、media.Image 入力について上記のように、まず画像の 回転角度を計算します。 次に、画像の高さ、幅、カラー エンコード形式、回転角度とともに、バッファまたは配列を含む InputImage オブジェクトを作成します。

Kotlin

val image = InputImage.fromByteBuffer(
        byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)
// Or:
val image = InputImage.fromByteArray(
        byteArray,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)

Java

InputImage image = InputImage.fromByteBuffer(byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);
// Or:
InputImage image = InputImage.fromByteArray(
        byteArray,
        /* image width */480,
        /* image height */360,
        rotation,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);

Bitmap の使用

InputImage オブジェクトを Bitmap オブジェクトから作成するには、次の宣言を行います。

Kotlin

val image = InputImage.fromBitmap(bitmap, 0)

Java

InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);

画像は Bitmap オブジェクトと回転角度で表されます。

3. 画像ラベラーを実行する

画像内のオブジェクトにラベルを付けるには、image オブジェクトを ImageLabelerprocess() メソッドに渡します。

Kotlin

labeler.process(image)
        .addOnSuccessListener { labels ->
            // Task completed successfully
            // ...
        }
        .addOnFailureListener { e ->
            // Task failed with an exception
            // ...
        }

Java

labeler.process(image)
        .addOnSuccessListener(new OnSuccessListener<List<ImageLabel>>() {
            @Override
            public void onSuccess(List<ImageLabel> labels) {
                // Task completed successfully
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Task failed with an exception
                // ...
            }
        });

4. ラベル付きエンティティに関する情報を取得する

画像のラベル付けオペレーションが成功すると、ImageLabel オブジェクトのリストが成功リスナーに渡されます。各 ImageLabel オブジェクトは画像内でラベル付けされたものを表します。各ラベルのテキストの説明(LiteRT モデルファイルのメタデータで使用できる場合)、信頼スコア、インデックスを取得できます。次に例を示します。

Kotlin

for (label in labels) {
    val text = label.text
    val confidence = label.confidence
    val index = label.index
}

Java

for (ImageLabel label : labels) {
    String text = label.getText();
    float confidence = label.getConfidence();
    int index = label.getIndex();
}

リアルタイムのパフォーマンスを改善するためのヒント

リアルタイムのアプリケーションで画像にラベルを付ける場合は、適切なフレームレートを得るために次の ガイドラインに従ってください。

  • Camera API または camera2 API を使用する場合は、 画像ラベラーの呼び出しのスロットル調整を行います。画像ラベラーの実行中に新しい動画 フレームが使用可能になった場合は、そのフレームをドロップします。例については、クイックスタート サンプルアプリの VisionProcessorBase クラスをご覧ください。
  • CameraX API を使用する場合は、バックプレッシャー戦略がデフォルト値 ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST に設定されていることを確認してください。これにより、分析のために一度に 1 つの画像のみが配信されます。アナライザーがビジー状態のときに複数の画像が 生成された場合、それらの画像は自動的にドロップされ、配信のために キューに登録されません。分析中の画像が ImageProxy.close() を呼び出して閉じられると、次の最新の画像が配信されます。
  • イメージ ラベラーの出力を使用して入力画像の上にグラフィックスをオーバーレイする場合は、まず ML Kit から検出結果を取得し、画像とオーバーレイを 1 つのステップでレンダリングします。これにより、ディスプレイ サーフェスへのレンダリングは入力フレームごとに 1 回で済みます。例については、クイックスタート サンプルアプリの CameraSourcePreview クラスと GraphicOverlay クラスをご覧ください。
  • Camera2 API を使用する場合は、画像を ImageFormat.YUV_420_888 形式でキャプチャします。古い Camera API を使用する場合は、 ImageFormat.NV21 形式で画像をキャプチャします。