本教程将在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包,并解压缩到工作目录。
本项目初始代码中包括了若干的TODO项,以导航项目中未完成之处。为了方便起见,首先查看TODO列表视图,View>Tool Windows>TODO
默认情况下了列出项目所有的TODO项,进一步按照模块分组(Group By)
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://m.huajiangbk.com/newsview304800.html
上一篇: Keras花卉分类全流程(预处理 |
下一篇: 软著项目推荐 深度学习花卉识别 |