当前位置:网站首页>[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
2022-06-30 22:20:00 【mozhimen】
Deep learning .Tensorflow.TFLite. Mobile device integration deep learning light model TFlite. Image classification
Why i create it?
To create an easy to use and easy to integrate TFlite Load the library , therefore TFLiteLoader emerge as the times require
- Open source Github Project address TFLiteLoader
Integrate ImageClassifier
rely on
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mozhimen.TFLiteLoader:imageclassifier:1.0.2'
}
Access
- Global declarations
private lateinit var _tFLiteImageClassifier: TFLiteImageClassifier
- stay onCreate Initialize in
_tFLiteImageClassifier = TFLiteImageClassifier.create("health_model.tflite", resultSize = 3)
- Sort of pictures
val objList = _tFLiteImageClassifier.classify([ Yours bitmap], 0)
- An example of processing the returned data , Sure pull Code reference demo
val objList = _tFLiteImageClassifier.classify(rotateBitmap, 0)
Log.d(TAG, "analyze: $objList")
runOnUiThread {
if (objList.isEmpty()) [email protected]
objList.forEachIndexed { index, _ ->
_stringBuilder.append("${objList[index].title}: ${objList[index].confidence}").append(" ")
}
vb.imageClassifierRes.text = _stringBuilder.toString()
_stringBuilder.clear()
}
Description of the return value
- List{Recognition}
data class Recognition(
/**
* Unique identifier of the identified thing . Class specific , Not an instance
* A unique identifier for what has been recognized. Specific to the class, not the instance of
* the object.
*/
val id: String?,
/**
* Display the name for identification
* Display name for the recognition.
*/
val title: String?,
/**
* This is a classifiable score relative to other recognition levels . The higher, the better
* A sortable score for how good the recognition is relative to others. Higher should be better.
*/
val confidence: Float?,
/**
* Optional location in the source image , Used to identify the position of the object , Image classification does not return obj The location of
* Optional location within the source image for the location of the recognized object.
*/
private var location: RectF?
) {
fun getLocation(): RectF {
return RectF(location)
}
fun setLocation(location: RectF?) {
this.location = location
}
override fun toString(): String {
var resultString = ""
if (id != null) {
resultString += "[$id] "
}
if (title != null) {
resultString += "$title "
}
if (confidence != null) {
resultString += String.format("(%.1f%%) ", confidence * 100.0f)
}
if (location != null) {
resultString += location.toString() + " "
}
return resultString.trim { it <= ' ' }
}
}
complete demo Code
@PermissionKAnnor(permissions = [Manifest.permission.CAMERA])
class ImageClassifierActivity : BaseKActivity<ActivityImageClassifierBinding, BaseKViewModel>(R.layout.activity_image_classifier) {
private lateinit var _tFLiteImageClassifier: TFLiteImageClassifier
// private lateinit var _tFLiteLabelImageClassifier: TFLiteLabelImageClassifier
// private lateinit var _tFImageClassifier: TFImageClassifier
override fun initData(savedInstanceState: Bundle?) {
PermissionK.initPermissions(this) {
if (it) {
initView(savedInstanceState)
} else {
PermissionK.applySetting(this)
}
}
}
override fun initView(savedInstanceState: Bundle?) {
initLiteLoader()
initCamera()
}
private fun initLiteLoader() {
_tFLiteImageClassifier = TFLiteImageClassifier.create("health_model.tflite", resultSize = 3)
// _tFLiteLabelImageClassifier = TFLiteLabelImageClassifier.create("?", "labels.txt", modelType = ModelType.QUANTIZED_EFFICIENTNET)
// _tFImageClassifier = TFImageClassifier.create("output_graph.pb", "output_labels.txt", "input", 299, "output", 128f, 128f, 0.1f, 1)
}
private fun initCamera() {
vb.imageClassifierPreview.initCamera(this, CameraSelector.DEFAULT_BACK_CAMERA)
vb.imageClassifierPreview.setImageAnalyzer(_frameAnalyzer)
vb.imageClassifierPreview.startCamera()
}
private val _frameAnalyzer: ImageAnalysis.Analyzer by lazy {
object : ImageAnalysis.Analyzer {
private val _reentrantLock = ReentrantLock()
private val _stringBuilder = StringBuilder()
@SuppressLint("UnsafeOptInUsageError", "SetTextI18n")
override fun analyze(image: ImageProxy) {
try {
_reentrantLock.lock()
val bitmap: Bitmap = if (image.format == ImageFormat.YUV_420_888) {
ImageConverter.yuv2Bitmap(image)!!
} else {
ImageConverter.jpeg2Bitmap(image)
}
val rotateBitmap = UtilKBitmap.rotateBitmap(bitmap, 90)
val objList = _tFLiteImageClassifier.classify(rotateBitmap, 0)
Log.d(TAG, "analyze: $objList")
runOnUiThread {
if (objList.isEmpty()) [email protected]
objList.forEachIndexed { index, _ ->
_stringBuilder.append("${objList[index].title}: ${objList[index].confidence}").append(" ")
}
vb.imageClassifierRes.text = _stringBuilder.toString()
_stringBuilder.clear()
}
} finally {
_reentrantLock.unlock()
}
image.close()
}
}
}
}
About the framework code here , You can refer to my other open source framework library : SwiftKit , But because it hasn't been finished yet , There is no complete wiki, It will be launched later
- This sample code holds references :
implementation 'com.github.mozhimen.SwiftKit:basick:1.1.1'
implementation('com.github.mozhimen.SwiftKit:abilityk:1.1.1') {
exclude group: 'com.mozhimen.abilityk.scank'
exclude group: 'com.huawei.hms'
}
implementation 'com.github.mozhimen.SwiftKit:componentk:1.1.1'
in summary : Integration is not very simple , Then try it quickly
边栏推荐
- Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
- Niubi | the tools I have treasured for many years have made me free to fish with pay
- Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
- The sandbox is being deployed on the polygon network
- [career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises
- Spark - understand partitioner in one article
- Domestic database disorder
- 软件测试报告包含哪些内容?如何获取高质量软件测试报告?
- Windbg调试工具介绍
猜你喜欢

Zhoushaojian, rare

《安富莱嵌入式周报》第270期:2022.06.13--2022.06.19

Anfulai embedded weekly report no. 270: June 13, 2022 to June 19, 2022

win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法

MFC interface library bcgcontrolbar v33.0 - desktop alarm window, grid control upgrade, etc

Starting from pg15 xid64 ticket skipping again

深入解析 Apache BookKeeper 系列:第四篇—背压

【BSP视频教程】BSP视频教程第19期:单片机BootLoader的AES加密实战,含上位机和下位机代码全开源(2022-06-26)
![[450. delete nodes in binary search tree]](/img/fd/bab2f92edeadd16263f15de6cc4420.png)
[450. delete nodes in binary search tree]

dba
随机推荐
Qsort function and Simulation Implementation of qsort function
Modify the name of the launched applet
Neo4j load CSV configuration and use
电脑设备管理器在哪里可以找到
Zhoushaojian, rare
Strictly minor spanning tree
Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
Yolo target detection
What if the taskbar is blank after win11 update? Solution to blank and stuck taskbar after win11 update
从PG15 XID64再次跳票说起
[backtracking] full arrangement leetcode46
牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
WinDbg debugging tool introduction
How to judge whether the JS object is empty
The programmer's girlfriend gave me a fatigue driving test
基于kubernetes平台微服务的部署
latex中 & 号什么含义?
总结的一些内存问题
Uniapp routing uni simple router