更新 Gradle 导入项
新的 SDK 只需要每个机器学习套件 API 一个依赖项。您无需指定 firebase-ml-vision
或 firebase-ml-natural-language
等通用库。机器学习套件对依赖于 Google Play 服务的库使用 com.google.android.gms
命名空间。
Vision 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.2.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.6 |
图片标注 | 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.8 |
对象检测 | 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.1 |
瘦模型
API | 旧文物 | 新工件 |
---|---|---|
条形码扫描 | com.google.firebase:firebase-ml-vision:24.0.1 | com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.0 |
人脸检测 | 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.0 |
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.2 |
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.2
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.5 |
智能回复 | 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.3 |
瘦模型
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 类中的 getBitmap() 方法(替换
FirebaseVisionImage
)。请参阅机器学习套件快速入门示例中的BitmapUtils.java
,获取从各种输入转换的位图。 - 移除了 BitmapImageMetadata,您只需将图片元数据(如 width、height、rotgrees、format)传递到 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 的更改
条形码扫描
对于 Barcode Scanning API,现在可以通过两种方式传送模型:
- 通过 Google Play 服务,也称为“瘦身”(推荐)- 这会缩减应用大小,并在应用之间共享模型。但是,开发者需要先确保模型已下载,然后才能首次使用。
- 使用应用的 APK(也称为“捆绑”)- 这会增加应用大小,但意味着模型可以直接使用。
这两种实现略有不同,“捆绑”版本比“瘦”版本有了很多改进。如需详细了解这些差异,请参阅 Barcode Scanning API 指南。
人脸检测
对于 Face Detection API,您可以通过以下两种方式传送模型:
- 通过 Google Play 服务,也称为“瘦身”(推荐)- 这会缩减应用大小,并在应用之间共享模型。但是,开发者需要先确保模型已下载,然后才能首次使用。
- 使用应用的 APK(也称为“捆绑”) - 这会增加应用下载大小,但意味着模型立即可用。
这些实现的行为相同。
翻译
TranslateLanguage
现在为其常量使用可读名称(例如ENGLISH
),而不是语言标记 (EN
)。它们现在也是 @StringDef,而不是 @IntDef,常量的值是匹配的 BCP 47 语言标记。请注意,如果您的应用使用“设备空闲”下载条件选项,此选项已被移除,不能再使用。您仍然可以使用“设备充电”选项。如果您希望实现更复杂的行为,可以延迟调用您自己的逻辑的
RemoteModelManager.download
。
AutoML 图片加标签
请注意,如果您的应用使用“设备空闲”下载条件选项,此选项已被移除,不能再使用。您仍然可以使用“设备充电”选项。
如果您需要更复杂的行为,可以延迟调用您自己的逻辑 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 ML Kit 是您唯一使用的 Firebase 组件。
- 仅使用设备端 API。
- 不使用模型服务。
如果是这种情况,您可以在迁移后移除 Firebase 依赖项。请按照以下步骤操作:
- 移除 Firebase 配置文件,方法是删除应用的模块(应用级)目录中的 google-services.json 配置文件。
- 将模块(应用级)Gradle 文件(通常是 app/build.gradle)中的 Google 服务 Gradle 插件替换为严格版本匹配器插件:
之前
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 插件类路径替换为严格版本匹配器插件的类路径:
之前
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 支持网站上的instructions,在 Firebase 控制台中删除您的 Firebase 应用。
获取帮助
如果您遇到任何问题,请查看我们的社区页面,其中列出了可用于与我们联系的渠道。