使用FireBase机器学习套件的新功能autoML搭建训练发布模型,并在Android上使用进行图片分类

115 阅读9分钟

将 Firebase 添加到应用,需要在 Firebase 控制台中和打开的 Android 项目中执行若干任务(例如,从控制台下载 Firebase 配置文件,然后将配置文件移动到 Android 项目中)。

第一步:创建 Firebase 项目

您必须先创建一个 Firebase 项目,并将其关联到您的 Android 应用,然后才能将 Firebase 添加到您的 Android 应用。

第二步:在 Firebase 中注册应用

拥有 Firebase 项目后,就可以向其中添加 Android 应用了。

在Firebase刚刚建好的项目中添加应用,点击添加中间的添加安卓应用按钮就行。

  1. Android 软件包名称字段中输入您的AppId。

确保输入应用实际使用的 ID。在向 Firebase 项目注册应用后,将无法添加或修改此值。

可以在项目的app/build中找到:

  1. (可选)根据设置工作流的提示输入其他应用信息。

别名是方便内部使用的标识符,只有您能在 Firebase 控制台中看到。

  1. 点击注册应用

第三步:添加 Firebase 配置文件

  1. 将 Firebase Android 配置文件添加到您的应用:

  2. 点击下载 google-services.json 以获取 Firebase Android 配置文件 (google-services.json)。

  • 我们可以随时再次下载 Firebase Android 配置文件。

  • 请确保该配置文件名未附加其他字符,如 (2)

  1. 接着将配置文件移动到应用的模块(应用级)目录中。

注意:该 Firebase 配置文件包含项目的唯一、非机密标识符。

  1. 要在 Android 应用中启用 Firebase 产品,请将 Google 服务插件添加到 Gradle 文件中。

  2. 在根级(项目级)Gradle 文件 (build.gradle) 中添加相应规则,以包含 Google 服务插件。此外,请确认您是否拥有 Google 的 Maven 代码库。

buildscript {

repositories {

// // 添加下面一行:

google() // Google's Maven repository

}

dependencies {

// ...

// 添加下面一行:

classpath 'com.google.gms:google-services:4.2.0' // Google Services plugin

}

}

allprojects {

// ...

repositories {

// 添加下面一行:

google() // Google's Maven repository

// ...

}

}

  1. 在Gradle 文件(通常是 app/build.gradle)中,在文件末尾添加一行内容。

apply plugin: 'com.android.application'

android {

// ...

}

// 添加以下内容:

apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin

第四步:将 Firebase SDK 添加到APP

在(应用级)Gradle 文件(通常是 app/build.gradle)中,添加核心 Firebase SDK 的依赖项:

buildscript {

repositories {

// Check that you have the following line (if not, add it):

google() // Google's Maven repository

}

dependencies {

...

// Add this line

classpath 'com.google.gms:google-services:4.3.3'

}

}

allprojects {

...

repositories {

// Check that you have the following line (if not, add it):

google() // Google's Maven repository

...

}

}

应用级 build.gradle(<项目>/<应用模块>/build.gradle):

apply plugin: 'com.android.application'

// Add this line

apply plugin: 'com.google.gms.google-services'

dependencies {

// add the Firebase SDK for Google Analytics

implementation 'com.google.firebase:firebase-analytics:17.2.2'

// add SDKs for any other desired Firebase products

// firebase.google.com/docs/androi…

}

最后,按 IDE 中显示的栏中的“立即同步”:

  1. 同步您的应用以确保所有依赖项都具有必要的版本。

  2. 运行应用,向 Firebase 发送已成功集成 Firebase 的验证信息。

设备日志将显示说明初始化已完成的 Firebase 验证信息。如果我们是在具有网络访问权限的模拟器上运行应用,则 Firebase 控制台会通知说应用连接已完成。

使用 “ML KIT” 的 autoML 搭建训练发布模型


第一步:下载数据集:

我这里使用的是使用 TensorFlow 的 flower-image 数据集,创建图像分类或标签模型,在训练了该模型后,将其用于应用程序中的设备上图像标签。

这个数据集又5种标签的雏菊,蒲公英,玫瑰,向日葵和郁金香花,这样后面的 app 就可以使用模型标识图像的标签之一。

下载花卉图像数据集。

第二步:上传和训练数据集:

转到 Firebase 控制台-> Machine Learning,然后单击 “AutoML” 。

现在,单击添加数据集

给数据集命名并选择第二个选项,然后单击继续。

现在,单击浏览文件,然后为花朵图像数据集选择一个.zip文件,该文件已在前面的第一步中下载。

现在,等待所有三个步骤都已完成。

之后,点击训练模型

现在选择第三个选项(高精度),然后单击开始训练。

现在,等待模型训练完成。

第三步:使用训练完成的模型:

在模型列表中单击第一个模型,并在模型名称上记下我们以后需要的名称

现在,就可以使用模型了

先查看一下刚刚训练完的模型的评估结果:

在 Android Studio 上编写 app 使用该模型


我们可以通过2种方式使用此模型

(1)远程(发布到firebase并在运行时从您的应用程序远程加载)

(2)本地(与应用程序下载并捆绑)

首先,通过单击“发布”按钮来发布模型,然后单击“下载”并将zip文件保存在所需的位置。

之后,解压缩下载的zip文件,然后复制所有三个文件。

现在转到Android Studio,然后

创建 Assets 文件夹绑定本地模型

在应用程序上单击鼠标右键,选择“NEW”->“Folder”->“Assets”

然后单击完成。

现在,通过右键单击assss-> New-> Directory在assets文件夹中创建目录

给出一个名字,例如model,然后点击回车。

现在,将所有三个复制的文件粘贴到此文件夹中。

修改 build.gradle 以使用模型

将以下内容添加到应用的build.gradle文件中,以确保Gradle在构建应用时不会压缩模型文件:

android {

// ...

aaptOptions {

noCompress“ tflite”

}

}

如下所示:

现在,在应用程序级别build.gradle中为ML Kit Android库添加以下两个依赖项:

implementation 'com.google.firebase:firebase-ml-vision:24.0.1'

implementation 'com.google.firebase:firebase-ml-vision-automl:18.0.3'

现在添加一个Button,ImageView和TextView来选择图像,显示图像并在活动布局中显示带有谓词百分比的标签。

<RelativeLayout

xmlns:android="schemas.android.com/apk/res/and…"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/timg">

<ImageView

android:id="@+id/image"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_above="@+id/selectImage"

android:layout_marginBottom="50dp" />

<Button

android:id="@+id/selectImage"

android:layout_width="136dp"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:background="@color/orange_normal"

android:text="Select Image !"

android:textColor="@color/colorWite" />

<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/selectImage"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"

android:textColor="@android:color/black"

android:textSize="16sp" />

从手机上获取图片

要想判断图片的标签,首先肯定是要在手机上获取图片

打开android studio并将此依赖关系粘贴到应用程序级别的build.gradle文件中,如下所示:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'

通过添加CropImageActivity来修改AndroidMainfest.xml:

<activity

android:name="com.theartofdev.edmodo.cropper.CropImageActivity"

android:screenOrientation="portrait"

android:theme="@style/Base.Theme.AppCompat"/>

打开android studio并将此依赖关系粘贴到应用程序级别的build.gradle文件中,如下所示:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'

通过添加CropImageActivity来修改AndroidMainfest.xml:

<activity

android:name="com.theartofdev.edmodo.cropper.CropImageActivity"

android:screenOrientation="portrait"

android:theme="@style/Base.Theme.AppCompat"/>

点击按钮即可打开CropImageActivity:

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

CropImage.activity().start(MainActivity.this);

// fromRemoteModel();

}

});

最后,覆盖活动结果并更新ImageView:

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {

CropImage.ActivityResult result = CropImage.getActivityResult(data);

if (resultCode == RESULT_OK) {

if (result != null) {

Uri uri = result.getUri(); // 图片在手机上的路径

imageView.setImageURI(uri); // 设置图片到 imageview

textView.setText(""); // 为了之前的文本不会被添加到新中

setLabelerFromLocalModel(uri);

// setLabelerFromRemoteLabel(uri);

} else

progressDialog.cancel();

} else

progressDialog.cancel();

}

}

获得输入图像标签的方式:

主要有以下三步:

1.加载模型

2.准备输入图像

3.运行图像标签器

1.对于远程模型:

1. 声明所有变量

FirebaseAutoMLRemoteModel remoteModel; // 用来加载远端仓库模型

FirebaseVisionImageLabeler labeler; // 用于运行图像标签器

FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder optionsBuilder; // 使用哪个选项在本地或远程运行标签器

ProgressDialog progressDialog; // 显示进度对话框时,模型正在下载…

FirebaseModelDownloadConditions conditions; // 下载模型的conditions

FirebaseVisionImage image; // 准备输入图片

TextView textView; // 用于显示输入图片的标签

Button button; // 从设备中选择图像

ImageView imageView; // 用于显示选定的图像

private FirebaseAutoMLLocalModel localModel;

2. 加载模型

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2020最新上万页的大厂面试真题

七大模块学习资料:如NDK模块开发、Android框架体系架构...

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群: 第一,学习知识比较碎片化,没有合理的学习路线与进阶方向。 第二,开发几年,不知道如何进阶更进一步,比较迷茫。 第三,到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢! 由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。如有需要获取完整的资料文档的朋友点击我的GitHub免费获取。