使用深度学习和 Tensorflow 在 Android 上进行闪电检测:使用 Android Studio 构建 UI





5.00/5 (2投票s)
在本文中,我们将介绍在 Android 环境中基于模型的应用程序的基本设置。
引言
在上一篇文章中,我们介绍了使用 Teachable Machine 训练 TF 模型,并以 FTLite 格式导出训练好的模型。
使用的工具
请查看我们使用的工具及其版本。
工具 | 版本 |
IDE:Android Studio | · Android Studio 4.0.1 · Build #AI-193.6911.18.40.6626763,构建于 2020 年 6 月 24 日 · 运行时版本:1.8.0_242-release-1644-b01 amd64 · VM:JetBrains s.r.o 的 OpenJDK 64 位服务器 VM |
在 Android 设备上进行测试 | · Samsung SM-A710FD- Android 版本 7.0 · Huawei MediaPad T3 10- Android 版本 7.0 |
Java | 8 Update 231 |
Java SE | 8 Update 131 |
以上规格并非此任务的唯一选择。可以随意尝试其他工具/版本,例如 IntelliJ IDEA 作为 IDE。
Android Studio 设置
首先,让我们创建一个新的 Android 项目。选择基本活动,如下所示。
然后,选择“API 23”作为Minimum SDK,并按如下所示填写其他相应的字段。
项目层级结构会出现在 Studio UI 中。我们将从 Android Studio 在生成项目时创建的 gradle
文件开始。
build.gradle
(项目级别)文件可以正常使用 – 只需确保已更新并同步 Gradle 依赖项
classpath "com.android.tools.build:gradle:4.0.1"
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
在 build.gradle
(应用程序级别)文件中,在 android
标签内输入此代码
aaptOptions {
noCompress "tflite" // Your model's file extension: "tflite", "lite", etc.
}
然后,在同一标签中,添加此代码片段
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
现在需要将实现库和框架(在默认基础上)所需的依赖项添加到 build.gradle
(应用程序级别)文件中
// Object detection feature with bundled default classifier
implementation 'com.google.mlkit:object-detection:16.2.1'
// Object detection feature with custom classifier support
implementation 'com.google.mlkit:object-detection-custom:16.2.1'
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-livedata:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
//noinspection GradleDependency
implementation 'com.google.guava:guava:17.0'
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
添加上述依赖项后,当 Android Studio 询问您是否同步时,请同步该文件。
现在转到 AndroidManifest.xml 文件并输入权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
现在让我们深入了解基本设置和前端问题。您的 activity_main.xml 文件应如下所示
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml 文件应如下所示
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.ruturaj.detectlightning.mlkit.CameraSourcePreview
android:id="@+id/preview_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"/>
<com.ruturaj.detectlightning.mlkit.GraphicOverlay
android:id="@+id/graphic_overlay"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="@id/preview_view"
app:layout_constraintRight_toRightOf="@id/preview_view"
app:layout_constraintTop_toTopOf="@id/preview_view"
app:layout_constraintBottom_toBottomOf="@id/preview_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>
创建一个名为 assets
的新文件夹,用于存储我们的 TFLite 模型和标签文件。解压缩从 Teachable Machine 下载的文件。
我们需要将以下内容添加到 strings.xml 文件中预生成的字符串中
<string name="permission_camera_rationale">Access to the camera is needed for detection</string>
<string name="no_camera_permission">This application cannot run because it has nocamera permission. The application will now exit.</string>
<string name="desc_camera_source_activity">Vision detection demo with live camera preview</string>
<!-- Strings for camera settings -->
<string name="pref_category_key_camera" translatable="false">pckc</string>
<string name="pref_category_title_camera">Camera</string>
<string name="pref_key_rear_camera_preview_size" translatable="false">rcpvs</string>
<string name="pref_key_rear_camera_picture_size" translatable="false">rcpts</string>
<string name="pref_key_front_camera_preview_size" translatable="false">fcpvs</string>
<string name="pref_key_front_camera_picture_size" translatable="false">fcpts</string>
<string name="pref_key_camerax_target_resolution" translatable="false">ctas</string>
<string name="pref_key_camera_live_viewport" translatable="false">clv</string>
<string name="pref_title_rear_camera_preview_size">Rear camera preview size</string>
<string name="pref_title_front_camera_preview_size">Front camera preview size</string>
<string name="pref_title_camerax_target_resolution">CameraX target resolution</string>
<string name="pref_title_camera_live_viewport">Enable live viewport</string>
<string name="pref_summary_camera_live_viewport">Do not block camera preview drawing on detection</string>
<!-- Strings for enabling multiple object detection -->
<string name="pref_title_object_detector_enable_multiple_objects">Enable multiple objects</string>
<string name="pref_key_live_preview_object_detector_enable_multiple_objects" translatable="false">lpodemo</string>
<string name="pref_key_still_image_object_detector_enable_multiple_objects" translatable="false">siodemo</string>
<!-- Strings for enabling classification -->
<string name="pref_title_object_detector_enable_classification">Enable classification</string>
<string name="pref_key_live_preview_object_detector_enable_classification" translatable="false">lpodec</string>
<string name="pref_key_still_image_object_detector_enable_classification" translatable="false">siodec</string>
您知道吗…?
您是否知道可以通过多种方式开发此项目?是的,它可以!让我们简要讨论这些方法。
对于此项目,我选择了 Google ML 套件,因为它感觉应该专注于构建自己的演示。这是我使用的依赖项
// Object detection feature with custom classifier support
implementation 'com.google.mlkit:object-detection-custom:16.2.1'
基于 ML 的类文件受到了 ML-kit 的巨大启发,我为这个项目精心选择和组织了类文件。
MainActivity.java 文件将在下一篇文章中进行解释。
您可以下载整个项目并尝试构建。免责声明:我提供的解决方案远非“理想” - 它只是权宜之计且易于解释。有多种方法可以将训练好的模型引入 Android 环境。例如,一些开发人员更喜欢使用 Bazel 来生成 .apk 文件以构建 Android 项目。
后续步骤
在下一篇文章中,我们将在 Android 环境中设置 TFLite 模型,并创建一个可用的演示应用程序。敬请关注!