当前位置:网站首页>[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
边栏推荐
- [micro service ~nacos] configuration center of Nacos
- The Sandbox 正在 Polygon 网络上进行部署
- 总结的一些内存问题
- 100 important knowledge points that SQL must master: creating and manipulating tables
- Label Contrastive Coding based Graph Neural Network for Graph Classification
- [BSP video tutorial] BSP video tutorial issue 19: AES encryption practice of single chip bootloader, including all open source codes of upper and lower computers (June 26, 2022)
- When unittest automatically tests multiple use cases, the logging module prints repeatedly to solve the problem
- Develop technology - get time 10 minutes ago
- Strictly minor spanning tree
- 顺祝老吴的聚会
猜你喜欢
![[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises](/img/d3/68c9d40ae6e61efc10aca8bcc1f613.jpg)
[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises

Uniapp rich text editor

latex字母头顶两个点

多线程经典案例

公有云市场迈入深水区,冷静的亚马逊云还坐得住吗?

分享十万级TPS的IM即时通讯综合消息系统的架构

JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could

How to use data sets in machine learning?

从PG15 XID64再次跳票说起
随机推荐
Which direction should college students choose to find jobs after graduation?
[untitled] first time to participate in CSDN activities
msf之ms17-010永恒之蓝漏洞
谈谈数字化转型的几个关键问题
Cas classique multithreadé
Win11如何优化服务?Win11优化服务的方法
latex中 & 号什么含义?
Study summary of dynamic routing between capsules
深入解析 Apache BookKeeper 系列:第四篇—背压
2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main() { ints := make
软件确认测试的内容和流程有哪些?确认测试报告需要多少钱?
吴恩达的机器学习适合入门吗?
How to judge whether the JS object is empty
电脑设备管理器在哪里可以找到
AtCoder Beginner Contest 257
Windbg调试工具介绍
Spark - understand partitioner in one article
牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
严格次小生成树
PostgreSQL存储结构浅析