基于TensorFlow Lite实现的Android花卉识别应用
介绍
本教程将在Android设备上使用TensorFlow Lite运行图像识别模型,具体包括:
使用TensorFlow Lite Model Maker训练自定义的图像分类器利用Android Studio导入训练后的模型,并结合CameraX使用利用手机GPU加速模型运行最终的应用将呈现如下的界面:
预备工作
安装Android Studio 4.1以上版本
下载初始代码
创建工作目录,使用
git clone https://github.com/hoitab/TFLClassify.git 1
拷贝代码;或者直接访问github链接下载代码的ZIP包,并解压缩到工作目录。
运行初始代码
打开Android Studio,选择“Open an Existing Project”



向应用中添加TensorFlow Lite
选择"start"模块


点击“Finish”完成模型导入,系统将自动下载模型的依赖包并将依赖项添加至模块的build.gradle文件。最终TensorFlow Lite模型被成功导入,并生成摘要信息

检查代码中的TODO项
本项目初始代码中包括了若干的TODO项,以导航项目中未完成之处。为了方便起见,首先查看TODO列表视图,View>Tool Windows>TODO
默认情况下了列出项目所有的TODO项,进一步按照模块分组(Group By)
添加代码重新运行APP
定位“start”模块MainActivity.kt文件的TODO 1,添加初始化训练模型的代码private class ImageAnalyzer(ctx: Context, private val listener: RecognitionListener) : ImageAnalysis.Analyzer { ... // TODO 1: Add class variable TensorFlow Lite Model private val flowerModel = FlowerModel.newInstance(ctx) ... } 123456789 在CameraX的analyze方法内部,需要将摄像头的输入ImageProxy转化为Bitmap对象,并进一步转化为TensorImage 对象
override fun analyze(imageProxy: ImageProxy) { ... // TODO 2: Convert Image to Bitmap then to TensorImage val tfImage = TensorImage.fromBitmap(toBitmap(imageProxy)) ... } 123456 对图像进行处理并生成结果,主要包含下述操作: 按照属性score对识别结果按照概率从高到低排序列出最高k种可能的结果,k的结果由常量MAX_RESULT_DISPLAY定义
override fun analyze(imageProxy: ImageProxy) { ... // TODO 3: Process the image using the trained model, sort and pick out the top results val outputs = flowerModel.process(tfImage) .probabilityAsCategoryList.apply { sortByDescending { it.score } // Sort with highest confidence first }.take(MAX_RESULT_DISPLAY) // take the top results ... } 12345678910 将识别的结果加入数据对象Recognition 中,包含label和score两个元素。后续将用于RecyclerView的数据显示
override fun analyze(imageProxy: ImageProxy) { ... // TODO 4: Converting the top probability items into a list of recognitions for (output in outputs) { items.add(Recognition(output.label, output.score)) } ... } 12345678 将原先用于虚拟显示识别结果的代码注释掉或者删除
// START - Placeholder code at the start of the codelab. Comment this block of code out. for (i in 0..MAX_RESULT_DISPLAY-1){ items.add(Recognition("Fake label $i", Random.nextFloat())) } // END - Placeholder code at the start of the codelab. Comment this block of code out. 12345 以物理设备重新运行start模块最终运行效果
相关知识
基于TensorFlow Lite实现的Android花卉识别应用
基于Android系统的花卉识别APP界面设计与实现
34 花卉识别
基于tensorflow的花卉识别
花卉识别(tensorflow)
深度学习之基于Tensorflow卷积神经网络花卉识别系统
基于深度学习的花卉识别研究
基于深度卷积神经网络的移动端花卉识别系统
TensorFlow学习记录(八)
colab cnn实现花卉图片分类识别
网址: 基于TensorFlow Lite实现的Android花卉识别应用 https://www.huajiangbk.com/newsview304800.html
上一篇: Keras花卉分类全流程(预处理 |
下一篇: 软著项目推荐 深度学习花卉识别 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039