当前位置:网站首页>【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(图像分类篇)
【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(图像分类篇)
2022-06-30 22:10:00 【mozhimen】
深度学习.Tensorflow.TFLite.移动设备集成深度学习轻模型TFlite.图像分类篇
Why i create it?
为了创建一个易用且易于集成的TFlite加载库, 所以TFLiteLoader应运而生
- 开源Github项目地址 TFLiteLoader
集成 ImageClassifier
依赖
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mozhimen.TFLiteLoader:imageclassifier:1.0.2'
}
接入
- 全局声明
private lateinit var _tFLiteImageClassifier: TFLiteImageClassifier
- 在onCreate中进行初始化
_tFLiteImageClassifier = TFLiteImageClassifier.create("health_model.tflite", resultSize = 3)
- 分类图片
val objList = _tFLiteImageClassifier.classify([你的bitmap], 0)
- 对返回数据的处理示例, 可以pull代码参考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()
}
对于返回值的说明
- List{Recognition}
data class Recognition(
/**
* 已识别事物的唯一标识符。特定于类,而不是实例
* A unique identifier for what has been recognized. Specific to the class, not the instance of
* the object.
*/
val id: String?,
/**
* 显示名称以进行识别
* Display name for the recognition.
*/
val title: String?,
/**
* 这是一个相对于其他识别程度的可分类分数。越高越好
* A sortable score for how good the recognition is relative to others. Higher should be better.
*/
val confidence: Float?,
/**
* 源图像中可选的位置,用于识别对象的位置, 图像分类中不返回obj的位置
* 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 <= ' ' }
}
}
完整demo代码
@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()
}
}
}
}
关于这里的框架代码, 可以参考我另一个开源框架库: SwiftKit ,不过因为还未完成, 没有完整的wiki, 过段时间推出
- 本示例代码所持引用:
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'
综上所述: 集成是不是很简单, 那赶快试试吧
边栏推荐
- 机器学习工作要求研究生吗?
- Domestic database disorder
- About, Qianxin detects code vulnerabilities and XSS series solves them
- WinDbg debugging tool introduction
- Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
- Do a scrollbar thinking
- latex中 & 号什么含义?
- Excuse me, can I open an account for the company? Is it safe? All the answers you want are here
- The programmer's girlfriend gave me a fatigue driving test
- 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
猜你喜欢

机器学习中如何使用数据集?

Introduction and example of template method mode

Go Web 编程入门: 一探优秀测试库 GoConvey

Why does the computer speed slow down after vscode is used for a long time?

dba

京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
![[introduction to MySQL] the first conversation · first time in the](/img/73/cc85eb469384c3df94479318293c6f.png)
[introduction to MySQL] the first conversation · first time in the "database" Mainland

十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?

Mysql:sql overview and database system introduction | dark horse programmer

Windbg调试工具介绍
随机推荐
吴恩达的机器学习适合入门吗?
The Jenkins download Plug-in can't be downloaded. Solution
谈谈数字化转型的几个关键问题
Pytorch quantitative practice (2)
盘点华为云GaussDB(for Redis)六大秒级能力
周少剑,很少见
程序员女友给我做了一个疲劳驾驶检测
Docker installing MySQL
1. Summary of wechat applet page Jump methods; 2. the navigateto stack does not jump to the tenth floor
Document layout analysis: a comprehensive survey 2019 paper learning summary
HDFS centralized cache management
Ten of the most heart piercing tests / programmer jokes, read the vast crowd, how to find?
Look at the top 10 capabilities of alicloud cipu
msf之ms17-010永恒之蓝漏洞
顺祝老吴的聚会
Is machine learning suitable for girls?
latex中 & 号什么含义?
Label Contrastive Coding based Graph Neural Network for Graph Classification
What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!