使用 AutoML Vision Edge 训练您自己的模型后,您可以在您的应用程序中使用它来检测图像中的对象。
有两种方法可以集成从 AutoML Vision Edge 训练的模型:您可以通过将模型放入应用程序的资产文件夹来捆绑模型,或者您可以从 Firebase 动态下载它。
模型捆绑选项 | |
---|---|
捆绑在您的应用程序中 |
|
使用 Firebase 托管 |
|
在你开始之前
如果您想下载模型,请确保将 Firebase 添加到您的 Android 项目(如果您尚未这样做)。捆绑模型时不需要这样做。
将 TensorFlow Lite Task 库的依赖项添加到模块的应用程序级 gradle 文件,通常是
app/build.gradle
:要将模型与您的应用程序捆绑在一起:
dependencies { // ... // Object detection with a bundled Auto ML model implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT' }
要从 Firebase 动态下载模型,还要添加 Firebase ML 依赖项:
dependencies { // ... // Object detection with an Auto ML model deployed to Firebase implementation platform('com.google.firebase:firebase-bom:26.1.1') implementation 'com.google.firebase:firebase-ml-model-interpreter' implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly' }
1.加载模型
配置本地模型源
将模型与您的应用程序捆绑在一起:
- 从您从 Google Cloud Console 下载的 zip 存档中提取模型。
- 在您的应用程序包中包含您的模型:
- 如果您的项目中没有 assets 文件夹,请通过右键单击
app/
文件夹,然后单击New > Folder > Assets Folder来创建一个。 - 将带有嵌入式元数据的
tflite
模型文件复制到资产文件夹。
- 如果您的项目中没有 assets 文件夹,请通过右键单击
将以下内容添加到应用程序的
build.gradle
文件中,以确保 Gradle 在构建应用程序时不会压缩模型文件:android { // ... aaptOptions { noCompress "tflite" } }
模型文件将包含在应用程序包中并作为原始资产提供。
配置 Firebase 托管的模型源
要使用远程托管模型,请创建一个RemoteModel
对象,并指定您在发布模型时为其分配的名称:
爪哇
// Specify the name you assigned when you deployed the model.
FirebaseCustomRemoteModel remoteModel =
new FirebaseCustomRemoteModel.Builder("your_model").build();
科特林
// Specify the name you assigned when you deployed the model.
val remoteModel =
FirebaseCustomRemoteModel.Builder("your_model_name").build()
然后,启动模型下载任务,指定允许下载的条件。如果模型不在设备上,或者有更新版本的模型可用,任务将从 Firebase 异步下载模型:
爪哇
DownloadConditions downloadConditions = new DownloadConditions.Builder()
.requireWifi()
.build();
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(@NonNull Task<Void> task) {
// Success.
}
});
科特林
val downloadConditions = DownloadConditions.Builder()
.requireWifi()
.build()
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener {
// Success.
}
许多应用程序在其初始化代码中开始下载任务,但您可以在需要使用该模型之前的任何时候执行此操作。
从您的模型创建对象检测器
配置模型源后,从其中之一创建一个ObjectDetector
对象。
如果您只有一个本地绑定的模型,只需从您的模型文件创建一个对象检测器并配置您想要的置信度分数阈值(请参阅评估您的模型):
爪哇
// Initialization
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build();
ObjectDetector objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options);
科特林
// Initialization
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build()
val objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options)
如果您有一个远程托管的模型,则必须在运行之前检查它是否已下载。您可以使用模型管理器的isModelDownloaded()
方法检查模型下载任务的状态。
尽管您只需在运行对象检测器之前确认这一点,但如果您同时拥有远程托管模型和本地绑定模型,则在实例化对象检测器时执行此检查可能有意义:从远程创建对象检测器如果已下载则为模型,否则为本地模型。
爪哇
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean isDownloaded) {
}
});
科特林
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener { success ->
}
如果您只有一个远程托管的模型,您应该禁用与模型相关的功能——例如,灰显或隐藏部分 UI——直到您确认模型已下载。您可以通过将侦听器附加到模型管理器的download()
方法来实现。
知道模型已下载后,从模型文件创建对象检测器:
爪哇
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnCompleteListener(new OnCompleteListener<File>() {
@Override
public void onComplete(@NonNull Task<File> task) {
File modelFile = task.getResult();
if (modelFile != null) {
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0)
.build();
objectDetector = ObjectDetector.createFromFileAndOptions(
getApplicationContext(), modelFile.getPath(), options);
}
}
});
科特林
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnSuccessListener { modelFile ->
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0f)
.build()
objectDetector = ObjectDetector.createFromFileAndOptions(
applicationContext, modelFile.path, options)
}
2.准备输入图像
然后,对于要标记的每个图像,从图像创建一个TensorImage
对象。您可以使用fromBitmap
方法从Bitmap
创建TensorImage
对象:
爪哇
TensorImage image = TensorImage.fromBitmap(bitmap);
科特林
val image = TensorImage.fromBitmap(bitmap)
如果您的图像数据不在Bitmap
中,您可以加载像素数组,如TensorFlow Lite 文档中所示。
3.运行物体检测器
要检测图像中的对象,请将TensorImage
对象传递给ObjectDetector
的detect()
方法。
爪哇
List<Detection> results = objectDetector.detect(image);
科特林
val results = objectDetector.detect(image)
4.获取有关标记对象的信息
如果对象检测操作成功,它会返回一个Detection
对象列表。每个Detection
对象代表在图像中检测到的东西。您可以获得每个对象的边界框及其标签。
例如:
爪哇
for (Detection result : results) {
RectF bounds = result.getBoundingBox();
List<Category> labels = result.getCategories();
}
科特林
for (result in results) {
val bounds = result.getBoundingBox()
val labels = result.getCategories()
}
提高实时性能的技巧
如果您想在实时应用程序中标记图像,请遵循以下准则以获得最佳帧率:
- 限制对图像标签器的调用。如果在图像标签器运行时有新的视频帧可用,则丢弃该帧。有关示例,请参阅快速入门示例应用程序中的
VisionProcessorBase
类。 - 如果您使用图像标签器的输出在输入图像上叠加图形,请首先获取结果,然后渲染图像并在一个步骤中进行叠加。通过这样做,您只需为每个输入帧渲染到显示表面一次。有关示例,请参阅快速入门示例应用中的
CameraSourcePreview
和GraphicOverlay
类。 如果您使用 Camera2 API,请以
ImageFormat.YUV_420_888
格式捕获图像。如果您使用较旧的 Camera API,请以
ImageFormat.NV21
格式捕获图像。
使用 AutoML Vision Edge 训练您自己的模型后,您可以在您的应用程序中使用它来检测图像中的对象。
有两种方法可以集成从 AutoML Vision Edge 训练的模型:您可以通过将模型放入应用程序的资产文件夹来捆绑模型,或者您可以从 Firebase 动态下载它。
模型捆绑选项 | |
---|---|
捆绑在您的应用程序中 |
|
使用 Firebase 托管 |
|
在你开始之前
如果您想下载模型,请确保将 Firebase 添加到您的 Android 项目(如果您尚未这样做)。捆绑模型时不需要这样做。
将 TensorFlow Lite Task 库的依赖项添加到模块的应用程序级 gradle 文件,通常是
app/build.gradle
:要将模型与您的应用程序捆绑在一起:
dependencies { // ... // Object detection with a bundled Auto ML model implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT' }
要从 Firebase 动态下载模型,还要添加 Firebase ML 依赖项:
dependencies { // ... // Object detection with an Auto ML model deployed to Firebase implementation platform('com.google.firebase:firebase-bom:26.1.1') implementation 'com.google.firebase:firebase-ml-model-interpreter' implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly' }
1.加载模型
配置本地模型源
将模型与您的应用程序捆绑在一起:
- 从您从 Google Cloud Console 下载的 zip 存档中提取模型。
- 在您的应用程序包中包含您的模型:
- 如果您的项目中没有 assets 文件夹,请通过右键单击
app/
文件夹,然后单击New > Folder > Assets Folder来创建一个。 - 将带有嵌入式元数据的
tflite
模型文件复制到资产文件夹。
- 如果您的项目中没有 assets 文件夹,请通过右键单击
将以下内容添加到应用程序的
build.gradle
文件中,以确保 Gradle 在构建应用程序时不会压缩模型文件:android { // ... aaptOptions { noCompress "tflite" } }
模型文件将包含在应用程序包中并作为原始资产提供。
配置 Firebase 托管的模型源
要使用远程托管模型,请创建一个RemoteModel
对象,并指定您在发布模型时为其分配的名称:
爪哇
// Specify the name you assigned when you deployed the model.
FirebaseCustomRemoteModel remoteModel =
new FirebaseCustomRemoteModel.Builder("your_model").build();
科特林
// Specify the name you assigned when you deployed the model.
val remoteModel =
FirebaseCustomRemoteModel.Builder("your_model_name").build()
然后,启动模型下载任务,指定允许下载的条件。如果模型不在设备上,或者有更新版本的模型可用,任务将从 Firebase 异步下载模型:
爪哇
DownloadConditions downloadConditions = new DownloadConditions.Builder()
.requireWifi()
.build();
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(@NonNull Task<Void> task) {
// Success.
}
});
科特林
val downloadConditions = DownloadConditions.Builder()
.requireWifi()
.build()
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener {
// Success.
}
许多应用程序在其初始化代码中开始下载任务,但您可以在需要使用该模型之前的任何时候执行此操作。
从您的模型创建对象检测器
配置模型源后,从其中之一创建一个ObjectDetector
对象。
如果您只有一个本地绑定的模型,只需从您的模型文件创建一个对象检测器并配置您想要的置信度分数阈值(请参阅评估您的模型):
爪哇
// Initialization
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build();
ObjectDetector objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options);
科特林
// Initialization
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build()
val objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options)
如果您有一个远程托管的模型,则必须在运行之前检查它是否已下载。您可以使用模型管理器的isModelDownloaded()
方法检查模型下载任务的状态。
尽管您只需在运行对象检测器之前确认这一点,但如果您同时拥有远程托管模型和本地绑定模型,则在实例化对象检测器时执行此检查可能有意义:从远程创建对象检测器如果已下载则为模型,否则为本地模型。
爪哇
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean isDownloaded) {
}
});
科特林
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener { success ->
}
如果您只有一个远程托管的模型,您应该禁用与模型相关的功能——例如,灰显或隐藏部分 UI——直到您确认模型已下载。您可以通过将侦听器附加到模型管理器的download()
方法来实现。
知道模型已下载后,从模型文件创建对象检测器:
爪哇
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnCompleteListener(new OnCompleteListener<File>() {
@Override
public void onComplete(@NonNull Task<File> task) {
File modelFile = task.getResult();
if (modelFile != null) {
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0)
.build();
objectDetector = ObjectDetector.createFromFileAndOptions(
getApplicationContext(), modelFile.getPath(), options);
}
}
});
科特林
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnSuccessListener { modelFile ->
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0f)
.build()
objectDetector = ObjectDetector.createFromFileAndOptions(
applicationContext, modelFile.path, options)
}
2.准备输入图像
然后,对于要标记的每个图像,从图像创建一个TensorImage
对象。您可以使用fromBitmap
方法从Bitmap
创建TensorImage
对象:
爪哇
TensorImage image = TensorImage.fromBitmap(bitmap);
科特林
val image = TensorImage.fromBitmap(bitmap)
如果您的图像数据不在Bitmap
中,您可以加载像素数组,如TensorFlow Lite 文档中所示。
3.运行物体检测器
要检测图像中的对象,请将TensorImage
对象传递给ObjectDetector
的detect()
方法。
爪哇
List<Detection> results = objectDetector.detect(image);
科特林
val results = objectDetector.detect(image)
4.获取有关标记对象的信息
如果对象检测操作成功,它会返回一个Detection
对象列表。每个Detection
对象代表在图像中检测到的东西。您可以获得每个对象的边界框及其标签。
例如:
爪哇
for (Detection result : results) {
RectF bounds = result.getBoundingBox();
List<Category> labels = result.getCategories();
}
科特林
for (result in results) {
val bounds = result.getBoundingBox()
val labels = result.getCategories()
}
提高实时性能的技巧
如果您想在实时应用程序中标记图像,请遵循以下准则以获得最佳帧率:
- 限制对图像标签器的调用。如果在图像标签器运行时有新的视频帧可用,则丢弃该帧。有关示例,请参阅快速入门示例应用程序中的
VisionProcessorBase
类。 - 如果您使用图像标签器的输出在输入图像上叠加图形,请首先获取结果,然后渲染图像并在一个步骤中进行叠加。通过这样做,您只需为每个输入帧渲染到显示表面一次。有关示例,请参阅快速入门示例应用中的
CameraSourcePreview
和GraphicOverlay
类。 如果您使用 Camera2 API,请以
ImageFormat.YUV_420_888
格式捕获图像。如果您使用较旧的 Camera API,请以
ImageFormat.NV21
格式捕获图像。
使用 AutoML Vision Edge 训练您自己的模型后,您可以在您的应用程序中使用它来检测图像中的对象。
有两种方法可以集成从 AutoML Vision Edge 训练的模型:您可以通过将模型放入应用程序的资产文件夹来捆绑模型,或者您可以从 Firebase 动态下载它。
模型捆绑选项 | |
---|---|
捆绑在您的应用程序中 |
|
使用 Firebase 托管 |
|
在你开始之前
如果您想下载模型,请确保将 Firebase 添加到您的 Android 项目(如果您尚未这样做)。捆绑模型时不需要这样做。
将 TensorFlow Lite Task 库的依赖项添加到模块的应用程序级 gradle 文件,通常是
app/build.gradle
:要将模型与您的应用程序捆绑在一起:
dependencies { // ... // Object detection with a bundled Auto ML model implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT' }
要从 Firebase 动态下载模型,还要添加 Firebase ML 依赖项:
dependencies { // ... // Object detection with an Auto ML model deployed to Firebase implementation platform('com.google.firebase:firebase-bom:26.1.1') implementation 'com.google.firebase:firebase-ml-model-interpreter' implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly' }
1.加载模型
配置本地模型源
将模型与您的应用程序捆绑在一起:
- 从您从 Google Cloud Console 下载的 zip 存档中提取模型。
- 在您的应用程序包中包含您的模型:
- 如果您的项目中没有 assets 文件夹,请通过右键单击
app/
文件夹,然后单击New > Folder > Assets Folder来创建一个。 - 将带有嵌入式元数据的
tflite
模型文件复制到资产文件夹。
- 如果您的项目中没有 assets 文件夹,请通过右键单击
将以下内容添加到应用程序的
build.gradle
文件中,以确保 Gradle 在构建应用程序时不会压缩模型文件:android { // ... aaptOptions { noCompress "tflite" } }
模型文件将包含在应用程序包中并作为原始资产提供。
配置 Firebase 托管的模型源
要使用远程托管模型,请创建一个RemoteModel
对象,并指定您在发布模型时为其分配的名称:
爪哇
// Specify the name you assigned when you deployed the model.
FirebaseCustomRemoteModel remoteModel =
new FirebaseCustomRemoteModel.Builder("your_model").build();
科特林
// Specify the name you assigned when you deployed the model.
val remoteModel =
FirebaseCustomRemoteModel.Builder("your_model_name").build()
然后,启动模型下载任务,指定允许下载的条件。如果模型不在设备上,或者有更新版本的模型可用,任务将从 Firebase 异步下载模型:
爪哇
DownloadConditions downloadConditions = new DownloadConditions.Builder()
.requireWifi()
.build();
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(@NonNull Task<Void> task) {
// Success.
}
});
科特林
val downloadConditions = DownloadConditions.Builder()
.requireWifi()
.build()
RemoteModelManager.getInstance().download(remoteModel, downloadConditions)
.addOnSuccessListener {
// Success.
}
许多应用程序在其初始化代码中开始下载任务,但您可以在需要使用该模型之前的任何时候执行此操作。
从您的模型创建对象检测器
配置模型源后,从其中之一创建一个ObjectDetector
对象。
如果您只有一个本地绑定的模型,只需从您的模型文件创建一个对象检测器并配置您想要的置信度分数阈值(请参阅评估您的模型):
爪哇
// Initialization
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build();
ObjectDetector objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options);
科特林
// Initialization
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0) // Evaluate your model in the Google Cloud Console
// to determine an appropriate value.
.build()
val objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options)
如果您有一个远程托管的模型,则必须在运行之前检查它是否已下载。您可以使用模型管理器的isModelDownloaded()
方法检查模型下载任务的状态。
尽管您只需在运行对象检测器之前确认这一点,但如果您同时拥有远程托管模型和本地绑定模型,则在实例化对象检测器时执行此检查可能有意义:从远程创建对象检测器如果已下载则为模型,否则为本地模型。
爪哇
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean isDownloaded) {
}
});
科特林
FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
.addOnSuccessListener { success ->
}
如果您只有一个远程托管的模型,您应该禁用与模型相关的功能——例如,灰显或隐藏部分 UI——直到您确认模型已下载。您可以通过将侦听器附加到模型管理器的download()
方法来实现。
知道模型已下载后,从模型文件创建对象检测器:
爪哇
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnCompleteListener(new OnCompleteListener<File>() {
@Override
public void onComplete(@NonNull Task<File> task) {
File modelFile = task.getResult();
if (modelFile != null) {
ObjectDetectorOptions options = ObjectDetectorOptions.builder()
.setScoreThreshold(0)
.build();
objectDetector = ObjectDetector.createFromFileAndOptions(
getApplicationContext(), modelFile.getPath(), options);
}
}
});
科特林
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
.addOnSuccessListener { modelFile ->
val options = ObjectDetectorOptions.builder()
.setScoreThreshold(0f)
.build()
objectDetector = ObjectDetector.createFromFileAndOptions(
applicationContext, modelFile.path, options)
}
2.准备输入图像
然后,对于要标记的每个图像,从图像创建一个TensorImage
对象。您可以使用fromBitmap
方法从Bitmap
创建TensorImage
对象:
爪哇
TensorImage image = TensorImage.fromBitmap(bitmap);
科特林
val image = TensorImage.fromBitmap(bitmap)
如果您的图像数据不在Bitmap
中,您可以加载像素数组,如TensorFlow Lite 文档中所示。
3.运行物体检测器
要检测图像中的对象,请将TensorImage
对象传递给ObjectDetector
的detect()
方法。
爪哇
List<Detection> results = objectDetector.detect(image);
科特林
val results = objectDetector.detect(image)
4.获取有关标记对象的信息
如果对象检测操作成功,它会返回一个Detection
对象列表。每个Detection
对象代表在图像中检测到的东西。您可以获得每个对象的边界框及其标签。
例如:
爪哇
for (Detection result : results) {
RectF bounds = result.getBoundingBox();
List<Category> labels = result.getCategories();
}
科特林
for (result in results) {
val bounds = result.getBoundingBox()
val labels = result.getCategories()
}
提高实时性能的技巧
如果您想在实时应用程序中标记图像,请遵循以下准则以获得最佳帧率:
- 限制对图像标签器的调用。如果在图像标签器运行时有新的视频帧可用,则丢弃该帧。有关示例,请参阅快速入门示例应用程序中的
VisionProcessorBase
类。 - 如果您使用图像标签器的输出在输入图像上叠加图形,请首先获取结果,然后渲染图像并在一个步骤中进行叠加。通过这样做,您只需为每个输入帧渲染到显示表面一次。有关示例,请参阅快速入门示例应用中的
CameraSourcePreview
和GraphicOverlay
类。 如果您使用 Camera2 API,请以
ImageFormat.YUV_420_888
格式捕获图像。如果您使用较旧的 Camera API,请以
ImageFormat.NV21
格式捕获图像。