本指南介绍了如何从适用于 Android 的 Firebase 机器学习套件迁移。
更新 Gradle 导入
机器学习套件 SDK 仅需要每个机器学习套件 API 的一个依赖项。您无需指定 firebase-ml-vision 或 firebase-ml-natural-language 等常用库。机器学习套件对依赖于 Google Play 服务的库使用 com.google.android.gms 命名空间。
Vision API
捆绑模型作为应用的一部分提供。细模型必须下载。某些 API 以捆绑和细两种形式提供,而其他 API 仅以其中一种形式提供:
| API | 捆绑 | 细 |
|---|---|---|
| 文本识别 | x(Beta 版) | x |
| 人脸检测 | x | x |
| 条形码扫描 | x | x |
| 图片标注 | x | x |
| 对象检测和跟踪 | x | - |
根据下表,更新模块(应用级)Gradle 文件(通常为 app/build.gradle.kts)中 Android 版机器学习套件库的依赖项:
捆绑模型
| API | 旧工件 | 新工件 |
|---|---|---|
| 条形码扫描 | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-barcode-model:16.0.1 |
com.google.mlkit:barcode-scanning:17.3.0 |
| 人脸轮廓 | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-face-model:19.0.0 |
com.google.mlkit:face-detection:16.1.7 |
| 图片标注 | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-image-label-model:19.0.0 |
com.google.mlkit:image-labeling:17.0.9 |
| 对象检测 | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-object-detection-model:19.0.3 |
com.google.mlkit:object-detection:17.0.2 |
细模型
| API | 旧工件 | 新工件 |
|---|---|---|
| 条形码扫描 | com.google.firebase:firebase-ml-vision:24.0.1 | com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.1 |
| 人脸检测 | com.google.firebase:firebase-ml-vision:24.0.1 | com.google.android.gms:play-services-mlkit-face-detection:17.1.0 |
| 文本识别 | com.google.firebase:firebase-ml-vision:24.0.1 | com.google.android.gms:play-services-mlkit-text-recognition:19.0.1 |
AutoMLVision Edge
| API | 旧工件 | 新工件 |
|---|---|---|
| 无需下载的 AutoML | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-automl:18.0.3 |
com.google.mlkit:image-labeling-custom:17.0.3 |
| 需要下载的 AutoML | com.google.firebase:firebase-ml-vision:24.0.1 com.google.firebase:firebase-ml-vision-automl:18.0.3 |
com.google.mlkit:image-labeling-custom:17.0.3
如需托管和下载自定义模型,请将模型移至 Cloud Storage,并在应用中添加 下载逻辑以使用 LocalModel 加载模型。如需了解详情,
请参阅
Firebase ML 到 Cloud Storage 迁移指南。 |
Natural Language API
捆绑模型作为应用的一部分提供。细模型必须下载:
| API | 捆绑 | 细 |
|---|---|---|
| 语言 ID | x | x |
| 智能回复 | x | x(Beta 版) |
根据下表,更新模块(应用级)Gradle 文件(通常为 app/build.gradle.kts)中 Android 版机器学习套件库的依赖项:
捆绑模型
| API | 旧工件 | 新工件 |
|---|---|---|
| 语言 ID | com.google.firebase:firebase-ml-natural-language:22.0.0 com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7 |
com.google.mlkit:language-id:17.0.6 |
| 智能回复 | com.google.firebase:firebase-ml-natural-language:22.0.0 com.google.firebase:firebase-ml-natural-language-smart-reply-model:20.0.7 |
com.google.mlkit:smart-reply:17.0.4 |
细模型
| API | 旧工件 | 新工件 |
|---|---|---|
| 语言 ID | com.google.firebase:firebase-ml-natural-language:22.0.0 com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7 |
com.google.android.gms:play-services-mlkit-language-id:17.0.0 |
| 智能回复 | com.google.firebase:firebase-ml-natural-language:22.0.0 com.google.firebase:firebase-ml-natural-language-smart-reply-model:20.0.7 |
com.google.android.gms:play-services-mlkit-smart-reply:16.0.0-beta1 |
更新类名称
如果您的类出现在此表中,请进行指示的更改:
| 旧类 | 新类 |
|---|---|
| com.google.firebase.ml.common.FirebaseMLException | com.google.mlkit.common.MlKitException |
| com.google.firebase.ml.vision.common.FirebaseVisionImage | com.google.mlkit.vision.common.InputImage |
| com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector | com.google.mlkit.vision.barcode.BarcodeScanner |
| com.google.firebase.ml.vision.labeler.FirebaseVisionImageLabel | com.google.mlkit.vision.label.ImageLabeler |
| com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector | com.google.mlkit.vision.barcode.BarcodeScanner |
| com.google.firebase.ml.vision.automl.FirebaseAutoMLLocalModel | com.google.mlkit.common.model.LocalModel |
| com.google.firebase.ml.vision.automl.FirebaseAutoMLRemoteModel | com.google.mlkit.common.model.LocalModel 需要手动下载。Firebase 托管的远程模型已弃用。如需了解 详情,请参阅 Firebase ML 到 Cloud Storage 迁移指南。 |
| com.google.firebase.ml.vision.label.FirebaseVisionOnDeviceImageLabelerOptions | com.google.mlkit.vision.label.defaults.ImageLabelerOptions |
| com.google.firebase.ml.vision.label.FirebaseVisionImageLabel | com.google.mlkit.vision.label.ImageLabel |
| com.google.firebase.ml.vision.label.FirebaseVisionOnDeviceAutoMLImageLabelerOptions | com.google.mlkit.vision.label.custom.CustomImageLabelerOptions |
| com.google.firebase.ml.vision.objects.FirebaseVisionObjectDetectorOptions | com.google.mlkit.vision.objects.defaults.ObjectDetectorOptions |
对于其他类,请遵循以下规则:
- 从类名称中移除
FirebaseVision前缀。 - 从类名称中移除以
Firebase前缀开头的其他前缀。
此外,在软件包名称中,将 com.google.firebase.ml 前缀替换为 com.google.mlkit。
更新方法名称
只需修改极少代码:
- 检测器/扫描器/标注器/翻译器…实例化已更改。每个功能现在都有自己的入口点。例如:
BarcodeScanning、TextRecognition、ImageLabeling、Translation… 对 Firebase 服务getInstance()的调用将替换为对功能入口点的getClient()方法的调用。 - 由于我们引入了用于识别其他脚本(如中文和韩文)的其他库,因此移除了
TextRecognizer的默认实例化。如需将默认选项与拉丁文脚本文本识别模型搭配使用,请声明对com.google.android.gms:play-services-mlkit-text-recognition的依赖项,并使用TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)。 - 由于我们为
ImageLabeler和ObjectDetector这两个功能引入了自定义模型支持,因此移除了它们的默认实例化。 例如,如需将默认选项与ImageLabeling中的基本模型搭配使用, 请声明对com.google.mlkit:image-labeling的依赖项,并在 Java 中使用ImageLabeling.getClient(ImageLabelerOptions.DEFAULT_OPTIONS)。 - 所有句柄(检测器/扫描器/标注器/翻译器…)都是可关闭的。确保
在不再使用这些对象时调用
close()方法。如果您在Fragment或AppCompatActivity中使用它们,一种方法是在Fragment或AppCompatActivity上调用LifecycleOwner.getLifecycle(),然后调用Lifecycle.addObserver。 - 为了保持一致性,Vision API 中的
processImage()和detectInImage()已重命名为process()。 - Natural Language API 现在使用术语“语言标记”(由 BCP 47 标准定义),而不是“语言代码”。
- 移除了
xxxOptions类中的 getter 方法。 - 作为公共接口的一部分,不再支持
InputImage类(替换FirebaseVisionImage)中的getBitmap()方法。如需获取从各种输入转换而来的位图,请参阅BitmapUtils.java在 机器学习套件快速入门示例中。 - 移除了
FirebaseVisionImageMetadata,您只需将图片元数据(例如width、height、rotationDegrees、format)传递到InputImage的构造方法中即可。
以下是一些新旧 Kotlin 方法的示例:
旧方法
// Construct image labeler with base model and default options. val imageLabeler = FirebaseVision.getInstance().onDeviceImageLabeler // Construct object detector with base model and default options. val objectDetector = FirebaseVision.getInstance().onDeviceObjectDetector // Construct face detector with given options val faceDetector = FirebaseVision.getInstance().getVisionFaceDetector(options) // Construct image labeler with local AutoML model val localModel = FirebaseAutoMLLocalModel.Builder() .setAssetFilePath("automl/manifest.json") .build() val autoMLImageLabeler = FirebaseVision.getInstance() .getOnDeviceAutoMLImageLabeler( FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.3F) .build() )
新方法
// Construct image labeler with base model and default options. val imageLabeler = ImageLabeling.getClient(ImageLabelerOptions.DEFAULT_OPTIONS) // Optional: add lifecycle observer lifecycle.addObserver(imageLabeler) // Construct object detector with base model and default options. val objectDetector = ObjectDetection.getClient( ObjectDetectorOptions.DEFAULT_OPTIONS ) // Construct face detector with given options val faceDetector = FaceDetection.getClient(options) // Construct image labeler with local AutoML model val localModel = LocalModel.Builder() .setAssetManifestFilePath("automl/manifest.json") .build() val autoMLImageLabeler = ImageLabeling.getClient( CustomImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.3F).build())
以下是一些新旧 Java 方法的示例:
旧方法
// Construct image labeler with base model and default options. FirebaseVisionImageLabeler imagelLabeler = FirebaseVision.getInstance().getOnDeviceImageLabeler(); // Construct object detector with base model and default options. FirebaseVisionObjectDetector objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(); // Construct face detector with given options FirebaseVisionFaceDetector faceDetector = FirebaseVision.getInstance().getVisionFaceDetector(options); // Construct image labeler with local AutoML model FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder() .setAssetFilePath("automl/manifest.json") .build(); FirebaseVisionImageLabeler autoMLImageLabeler = FirebaseVision.getInstance() .getOnDeviceAutoMLImageLabeler( FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.3F) .build());
新方法
// Construct image labeler with base model and default options. ImageLabeler imageLabeler = ImageLabeling.getClient( ImageLabelerOptions.DEFAULT_OPTIONS ); // Optional: add lifecycle observer getLifecycle().addObserver(imageLabeler); // Construct object detector with base model and default options. ObjectDetector objectDetector = ObjectDetection.getClient( ObjectDetectorOptions.DEFAULT_OPTIONS ); // Construct face detector with given options FaceDetector faceDetector = FaceDetection.getClient(options); // Construct image labeler with local AutoML model LocalModel localModel = new LocalModel.Builder() .setAssetManifestFilePath("automl/manifest.json") .build(); ImageLabeler autoMLImageLabeler = ImageLabeling.getClient( new CustomImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.3F).build());
特定于 API 的更改
条形码扫描
对于条形码扫描 API,现在有两种模型交付方式:
- 通过 Google Play 服务(又称“细模型”,推荐) - 这种方式可以减小 应用大小,并且模型在应用之间共享。不过,开发者需要确保在首次使用模型之前下载该模型。
- 通过应用的 APK(又称“捆绑模型”) - 这种方式会增加应用大小,但 意味着模型可以立即使用。
这两种实现方式略有不同,“捆绑模型”版本比“细模型”版本有一些改进。如需详细了解这些 差异,请参阅 条形码扫描 API 指南。
人脸检测
对于人脸检测 API,有两种模型交付方式:
- 通过 Google Play 服务(又称“细模型”,推荐) - 这种方式可以减小 应用大小,并且模型在应用之间共享。不过,开发者需要确保在首次使用模型之前下载该模型。
- 通过应用的 APK(又称“捆绑模型”) - 这种方式会增加应用下载 大小,但意味着模型可以立即使用。
这两种实现方式的行为相同。
翻译
TranslateLanguage现在为其常量使用可读的名称(例如ENGLISH),而不是语言标记 (EN)。它们现在也是@StringDef, 而不是@IntDef,并且常量的值是匹配的 BCP 47 语言标记。
AutoML Image Labeling(已弃用)
使用 AutoML 下载自定义模型以进行图片标注的功能已弃用,并将于 2027 年 6 月 15 日关闭。您应使用 Cloud Storage 托管模型,并在应用中添加下载逻辑以改为下载模型。如需了解 详情,请参阅 Firebase ML 到 Cloud Storage 迁移指南。
对象检测和跟踪
如果您的应用使用粗略分类进行对象检测,请注意,新 SDK 更改了其返回检测到的对象的分类类别的方式。
分类类别将作为 DetectedObject.Label 的实例返回,而不是整数。PredefinedCategory 类中包含粗略分类器的所有可能类别。
以下是新旧 Kotlin 代码的示例:
旧代码
if (object.classificationCategory == FirebaseVisionObject.CATEGORY_FOOD) { ... }
新代码
if (!object.labels.isEmpty() && object.labels[0].text == PredefinedCategory.FOOD) { ... } // or if (!object.labels.isEmpty() && object.labels[0].index == PredefinedCategory.FOOD_INDEX) { ... }
以下是新旧 Java 代码的示例:
旧代码
if (object.getClassificationCategory() == FirebaseVisionObject.CATEGORY_FOOD) { ... }
新代码
if (!object.getLabels().isEmpty() && object.getLabels().get(0).getText().equals(PredefinedCategory.FOOD)) { ... } // or if (!object.getLabels().isEmpty() && object.getLabels().get(0).getIndex() == PredefinedCategory.FOOD_INDEX) { ... }
移除了“unknown”类别。当对象分类的置信度较低时,我们不会返回任何标签。
移除 Firebase 依赖项
迁移后,移除 Firebase 依赖项。请按照以下步骤操作:
- 移除 Firebase 配置文件:删除应用模块(应用级)目录中的
google-services.json配置文件。 - 将模块(应用级)Gradle 文件(通常为
app/build.gradle.kts)中的 Google 服务 Gradle 插件替换为 Strict Version Matcher 插件:
之前
plugins { id("com.android.application") id("com.google.gms.google-services") } android { // … }
之后
plugins { id("com.android.application") id("com.google.android.gms.strict-version-matcher-plugin") } android { // … }
- 将项目(根级)Gradle 文件 (
build.gradle.kts) 中的 Google 服务 Gradle 插件类路径替换为 Strict Version Matcher 插件的类路径:
之前
buildscript { dependencies { // ... classpath("com.google.gms:google-services:4.3.3") } }
之后
buildscript { dependencies { // ... classpath("com.google.android.gms:strict-version-matcher-plugin:1.2.1") } }
按照 Firebase 支持网站上的 说明,在 Firebase 控制台中删除 Firebase 应用。
获取帮助
如果您遇到任何问题,请查看我们的社区页面 ,其中列出了与我们联系的可用渠道。