使用 AutoML 训练的模型给图片加标签 (Android)

在您使用 AutoML Vision Edge 训练自己的模型后,就可以在应用中用它给图片加标签。

准备工作

  1. 将 Firebase 添加到您的 Android 项目(如果尚未添加)。
  2. 将 Android 版机器学习套件库的依赖项添加到您的模块(应用级层)Gradle 文件(通常为 app/build.gradle):
    apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies {  // ...   implementation 'com.google.firebase:firebase-ml-vision:24.0.3'  implementation 'com.google.firebase:firebase-ml-vision-automl:18.0.5' }

1. 加载模型

机器学习套件会在设备上运行 AutoML 生成的模型。不过,您也可以将机器学习套件配置为从 Firebase 远程加载模型和/或从本地存储空间加载模型。

通过在 Firebase 上托管模型,您可以在无需发布新应用版本的情况下更新模型,并使用 Remote ConfigA/B Testing 向不同用户组动态提供不同模型。

如果您选择仅通过在 Firebase 中托管而不是与应用捆绑的方式来提供模型,可以缩小应用的初始下载文件大小。但请注意,如果模型未与您的应用捆绑,那么在应用首次下载模型之前,任何与模型相关的功能都将无法使用。

通过将您的模型与应用捆绑,您可以确保即使 Firebase 托管的模型不可用,应用的机器学习功能仍可正常运行。

配置 Firebase 托管的模型来源

要使用远程托管的模型,请创建一个 FirebaseAutoMLRemoteModel 对象,指定您在发布该模型时分配给模型的名称:

Java

// Specify the name you assigned in the Firebase console. FirebaseAutoMLRemoteModel remoteModel =  new FirebaseAutoMLRemoteModel.Builder("your_remote_model").build(); 

Kotlin

// Specify the name you assigned in the Firebase console. val remoteModel = FirebaseAutoMLRemoteModel.Builder("your_remote_model").build() 

然后,启动模型下载任务,指定要满足的下载条件。如果模型不在设备上,或模型有较新的版本,则任务将从 Firebase 异步下载模型:

Java

FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()  .requireWifi()  .build(); FirebaseModelManager.getInstance().download(remoteModel, conditions)  .addOnCompleteListener(new OnCompleteListener<Void>() {  @Override  public void onComplete(@NonNull Task<Void> task) {  // Success.  }  }); 

Kotlin

val conditions = FirebaseModelDownloadConditions.Builder()  .requireWifi()  .build() FirebaseModelManager.getInstance().download(remoteModel, conditions)  .addOnCompleteListener {  // Success.  } 

许多应用会通过其初始化代码启动下载任务,但您可以在需要使用该模型之前随时启动下载任务。

配置本地模型来源

如需将模型捆绑到您的应用,请执行以下操作:

  1. 自您从 Firebase 控制台下载的 zip 归档文件中解压缩模型及其元数据。我们建议您依所下载文件原样使用这些文件,不要做出修改(包括文件名)。
  2. 将您的模型及其元数据文件包括在自己的应用软件包中:

    1. 如果您的项目中没有资源文件夹,请创建一个,方法是右键点击 app/ 文件夹,然后依次点击 New(新建)> Folder(文件夹)> Assets Folder(资源文件夹)
    2. 在资源文件夹下创建一个子文件夹,以包含模型文件。
    3. 将文件 model.tflitedict.txtmanifest.json 复制到子文件夹(这三个文件必须位于相同的文件夹中)。
  3. 将以下内容添加到应用的 build.gradle 文件中,以确保 Gradle 在构建应用时不会压缩模型文件:
     android { // ... aaptOptions { noCompress "tflite" } } 
    模型文件将包含在应用软件包中,并作为原始资源提供给机器学习套件使用。
  4. 创建一个 FirebaseAutoMLLocalModel 对象,指定模型清单文件的路径:

    Java

    FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder()  .setAssetFilePath("manifest.json")  .build(); 

    Kotlin

    val localModel = FirebaseAutoMLLocalModel.Builder()  .setAssetFilePath("manifest.json")  .build() 

根据模型创建图片标记器

配置模型来源后,根据其中一个模型创建 FirebaseVisionImageLabeler 对象。

如果您只有本地捆绑的模型,只需根据您的 FirebaseAutoMLLocalModel 对象创建一个标记器,然后配置您需要的置信度得分阈值(请参见评估您的模型):

Java

FirebaseVisionImageLabeler labeler; try {  FirebaseVisionOnDeviceAutoMLImageLabelerOptions options =  new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)  .setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console  // to determine an appropriate value.  .build();  labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options); } catch (FirebaseMLException e) {  // ... } 

Kotlin

val options = FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)  .setConfidenceThreshold(0) // Evaluate your model in the Firebase console  // to determine an appropriate value.  .build() val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options) 

如果您使用的是远程托管的模型,则必须在运行之前检查该模型是否已下载。您可以使用模型管理器的 isModelDownloaded() 方法检查模型下载任务的状态。

虽然您只需在运行标记器之前确认这一点,但如果您同时拥有远程托管模型和本地捆绑模型,则可能需要在实例化图片标记器时执行此项检查:如果已下载远程模型,则根据该模型创建标记器,否则根据本地模型进行创建。

Java

FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)  .addOnSuccessListener(new OnSuccessListener<Boolean>() {  @Override  public void onSuccess(Boolean isDownloaded) {  FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder optionsBuilder;  if (isDownloaded) {  optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel);  } else {  optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel);  }  FirebaseVisionOnDeviceAutoMLImageLabelerOptions options = optionsBuilder  .setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console  // to determine an appropriate threshold.  .build();  FirebaseVisionImageLabeler labeler;  try {  labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options);  } catch (FirebaseMLException e) {  // Error.  }  }  }); 

Kotlin

FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)  .addOnSuccessListener { isDownloaded ->   val optionsBuilder =  if (isDownloaded) {  FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel)  } else {  FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)  }  // Evaluate your model in the Firebase console to determine an appropriate threshold.  val options = optionsBuilder.setConfidenceThreshold(0.0f).build()  val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options) } 

如果您只有远程托管的模型,则应停用与模型相关的功能(例如使界面的一部分变灰或将其隐藏),直到您确认模型已下载。这可以通过将监听器附加到模型管理器的 download() 方法来实现:

Java

FirebaseModelManager.getInstance().download(remoteModel, conditions)  .addOnSuccessListener(new OnSuccessListener<Void>() {  @Override  public void onSuccess(Void v) {  // Download complete. Depending on your app, you could enable  // the ML feature, or switch from the local model to the remote  // model, etc.  }  }); 

Kotlin

FirebaseModelManager.getInstance().download(remoteModel, conditions)  .addOnCompleteListener {  // Download complete. Depending on your app, you could enable the ML  // feature, or switch from the local model to the remote model, etc.  } 

2. 准备输入图片

接下来,对于每个您想要加标签的图片,使用本部分介绍的某个选项创建 FirebaseVisionImage 对象,并将其传递给 FirebaseVisionImageLabeler 的一个实例(下一部分将具体介绍)。

您可以基于以下资源创建一个 FirebaseVisionImage 对象:media.Image 对象、设备上的文件、字节数组或 Bitmap 对象:

  • 如需基于 media.Image 对象创建 FirebaseVisionImage 对象(例如从设备的相机捕获图片时),请将 media.Image 对象和图片的旋转角度传递给 FirebaseVisionImage.fromMediaImage()

    如果您使用 CameraX 库,OnImageCapturedListenerImageAnalysis.Analyzer 类会为您计算旋转角度值,因此您只需在调用 FirebaseVisionImage.fromMediaImage() 之前将旋转角度转换为机器学习套件的 ROTATION_ 常量之一:

    Java

    private class YourAnalyzer implements ImageAnalysis.Analyzer {  private int degreesToFirebaseRotation(int degrees) {  switch (degrees) {  case 0:  return FirebaseVisionImageMetadata.ROTATION_0;  case 90:  return FirebaseVisionImageMetadata.ROTATION_90;  case 180:  return FirebaseVisionImageMetadata.ROTATION_180;  case 270:  return FirebaseVisionImageMetadata.ROTATION_270;  default:  throw new IllegalArgumentException(  "Rotation must be 0, 90, 180, or 270.");  }  }  @Override  public void analyze(ImageProxy imageProxy, int degrees) {  if (imageProxy == null || imageProxy.getImage() == null) {  return;  }  Image mediaImage = imageProxy.getImage();  int rotation = degreesToFirebaseRotation(degrees);  FirebaseVisionImage image =  FirebaseVisionImage.fromMediaImage(mediaImage, rotation);  // Pass image to an ML Kit Vision API  // ...  } }

    Kotlin

    private class YourImageAnalyzer : ImageAnalysis.Analyzer {  private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) {  0 -> FirebaseVisionImageMetadata.ROTATION_0  90 -> FirebaseVisionImageMetadata.ROTATION_90  180 -> FirebaseVisionImageMetadata.ROTATION_180  270 -> FirebaseVisionImageMetadata.ROTATION_270  else -> throw Exception("Rotation must be 0, 90, 180, or 270.")  }  override fun analyze(imageProxy: ImageProxy?, degrees: Int) {  val mediaImage = imageProxy?.image  val imageRotation = degreesToFirebaseRotation(degrees)  if (mediaImage != null) {  val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation)  // Pass image to an ML Kit Vision API  // ...  }  } }

    如果您没有使用可提供图片旋转角度的相机库,可以根据设备的旋转角度和设备中相机传感器的朝向来计算旋转角度:

    Java

    private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static {  ORIENTATIONS.append(Surface.ROTATION_0, 90);  ORIENTATIONS.append(Surface.ROTATION_90, 0);  ORIENTATIONS.append(Surface.ROTATION_180, 270);  ORIENTATIONS.append(Surface.ROTATION_270, 180); } /**  * 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, Context context)  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);  // On most devices, the sensor orientation is 90 degrees, but for some  // devices it is 270 degrees. For devices with a sensor orientation of  // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.  CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE);  int sensorOrientation = cameraManager  .getCameraCharacteristics(cameraId)  .get(CameraCharacteristics.SENSOR_ORIENTATION);  rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;  // Return the corresponding FirebaseVisionImageMetadata rotation value.  int result;  switch (rotationCompensation) {  case 0:  result = FirebaseVisionImageMetadata.ROTATION_0;  break;  case 90:  result = FirebaseVisionImageMetadata.ROTATION_90;  break;  case 180:  result = FirebaseVisionImageMetadata.ROTATION_180;  break;  case 270:  result = FirebaseVisionImageMetadata.ROTATION_270;  break;  default:  result = FirebaseVisionImageMetadata.ROTATION_0;  Log.e(TAG, "Bad rotation value: " + rotationCompensation);  }  return result; }

    Kotlin

    private val ORIENTATIONS = SparseIntArray() init {  ORIENTATIONS.append(Surface.ROTATION_0, 90)  ORIENTATIONS.append(Surface.ROTATION_90, 0)  ORIENTATIONS.append(Surface.ROTATION_180, 270)  ORIENTATIONS.append(Surface.ROTATION_270, 180) } /**  * 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, context: Context): 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)  // On most devices, the sensor orientation is 90 degrees, but for some  // devices it is 270 degrees. For devices with a sensor orientation of  // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.  val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager  val sensorOrientation = cameraManager  .getCameraCharacteristics(cameraId)  .get(CameraCharacteristics.SENSOR_ORIENTATION)!!  rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360  // Return the corresponding FirebaseVisionImageMetadata rotation value.  val result: Int  when (rotationCompensation) {  0 -> result = FirebaseVisionImageMetadata.ROTATION_0  90 -> result = FirebaseVisionImageMetadata.ROTATION_90  180 -> result = FirebaseVisionImageMetadata.ROTATION_180  270 -> result = FirebaseVisionImageMetadata.ROTATION_270  else -> {  result = FirebaseVisionImageMetadata.ROTATION_0  Log.e(TAG, "Bad rotation value: $rotationCompensation")  }  }  return result }

    然后,将 media.Image 对象及旋转角度值传递给 FirebaseVisionImage.fromMediaImage()

    Java

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

    Kotlin

    val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
  • 如需基于文件 URI 创建 FirebaseVisionImage 对象,请将应用上下文和文件 URI 传递给 FirebaseVisionImage.fromFilePath()。如果您使用 ACTION_GET_CONTENT Intent 提示用户从图库应用中选择图片,这一操作会非常有用。

    Java

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

    Kotlin

    val image: FirebaseVisionImage try {  image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) {  e.printStackTrace() }
  • 如需基于 ByteBuffer 或字节数组创建 FirebaseVisionImage 对象,请先按上述 media.Image 输入的说明计算图片旋转角度。

    然后,创建一个包含图片的高度、宽度、颜色编码格式和旋转角度的 FirebaseVisionImageMetadata 对象:

    Java

    FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()  .setWidth(480) // 480x360 is typically sufficient for  .setHeight(360) // image recognition  .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)  .setRotation(rotation)  .build();

    Kotlin

    val metadata = FirebaseVisionImageMetadata.Builder()  .setWidth(480) // 480x360 is typically sufficient for  .setHeight(360) // image recognition  .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)  .setRotation(rotation)  .build()

    使用缓冲区或数组以及元数据对象来创建 FirebaseVisionImage 对象:

    Java

    FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);

    Kotlin

    val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
  • 如需基于 Bitmap 对象创建 FirebaseVisionImage 对象,请运行以下代码:

    Java

    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);

    Kotlin

    val image = FirebaseVisionImage.fromBitmap(bitmap)
    Bitmap 对象表示的图片必须保持竖直,不需要额外的旋转。

3. 运行图片标记器

如需给图片中的对象加标签,请将 FirebaseVisionImage 对象传递给 FirebaseVisionImageLabelerprocessImage() 方法。

Java

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

Kotlin

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

如果给图片加标签成功,则系统会向成功监听器传递一组 FirebaseVisionImageLabel 对象。您可以从各个对象获取在该图片中识别出的特征的相关信息。

例如:

Java

for (FirebaseVisionImageLabel label: labels) {  String text = label.getText();  float confidence = label.getConfidence(); } 

Kotlin

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

提高实时性能的相关提示

  • 限制检测器的调用次数。如果在检测器运行时有新的视频帧可用,请丢弃该帧。
  • 如果要将检测器的输出作为图形叠加在输入图片上,请先从机器学习套件获取结果,然后在一个步骤中完成图片的呈现和叠加。采用这一方法,每个输入帧只需在显示表面呈现一次。
  • 如果您使用 Camera2 API,请以 ImageFormat.YUV_420_888 格式捕获图片。

    如果您使用旧版 Camera API,请以 ImageFormat.NV21 格式捕获图片。