更新 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)中 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
com.google.mlkit:linkfirebase:17.0.0 |
Natural Language API
捆绑模型作为应用的一部分提供。细模型必须下载:
| API | 捆绑 | 细 |
|---|---|---|
| 语言 ID | x | x |
| 智能回复 | x | x(Beta 版) |
根据下表,更新模块(应用级)Gradle 文件(通常为 app/build.gradle)中 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.CustomRemoteModel |
| 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 已移除,您只需将图片元数据(例如宽度、高度、rotationDegrees、格式)传递到 InputImages 的构造方法中即可。
以下是一些新旧 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 life cycle 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 life cycle 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 语言标记。如果您的应用使用“设备空闲”下载条件选项,请注意,此选项已移除,无法再使用。您仍然可以使用“设备充电”选项。如果您需要更复杂的行为,可以将调用
RemoteModelManager.download延迟到您自己的逻辑之后。
AutoML Image Labeling
如果您的应用使用“设备空闲”下载条件选项,请注意,此选项已移除,无法再使用。您仍然可以使用“设备充电”选项。
如果您需要更复杂的行为,可以将调用 RemoteModelManager.download 延迟到您自己的逻辑之后。
对象检测和跟踪
如果您的应用使用粗略分类进行对象检测,请注意,新 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) { ... }
“未知”类别已移除。当对象分类的置信度较低时,我们不会返回任何标签。
移除 Firebase 依赖项(可选)
只有在满足以下条件时,此步骤才适用:
- Firebase 机器学习套件是您使用的唯一 Firebase 组件。
- 您仅使用设备端 API。
- 您不使用模型部署。
如果是这种情况,您可以在迁移后移除 Firebase 依赖项。请按照以下步骤操作:
- 移除 Firebase 配置文件:删除应用模块(应用级)目录中的 google-services.json 配置文件。
- 将模块(应用级)Gradle 文件(通常为 app/build.gradle)中的 Google 服务 Gradle 插件替换为 Strict Version Matcher 插件:
之前
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services' // Google Services plugin
android {
// …
}之后
apply plugin: 'com.android.application'
apply plugin: 'com.google.android.gms.strict-version-matcher-plugin'
android {
// …
}- 将项目(根级)Gradle 文件 (build.gradle) 中的 Google 服务 Gradle 插件类路径替换为 Strict Version Matcher 插件的类路径:
之前
buildscript {
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.3' // Google Services plugin
}
}
之后
buildscript {
dependencies {
// ...
classpath 'com.google.android.gms:strict-version-matcher-plugin:1.2.1'
}
}
按照 Firebase 支持网站上的 说明,在 Firebase 控制台中删除您的 Firebase 应用。
获取帮助
如果您遇到任何问题,请查看我们的社区页面,其中列出了与我们联系的渠道。