当前位置:网站首页>[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 it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
- Anfulai embedded weekly report no. 271: June 20, 2022 to June 26, 2022
- 实现多方数据安全共享,解决普惠金融信息不对称难题
- vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(图像分类篇)
- Apache server OpenSSL upgrade
- Alibaba Kube eventer MySQL sink simple usage record
- Go language learning notes - Gorm usage - database configuration, table addition | web framework gin (VII)
- HDFS集中式缓存管理(Centralized Cache Management)
- Cas classique multithreadé
猜你喜欢

Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online

Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes

《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26

Development techniques - import files using easyexcel (simple example)

About, Qianxin detects code vulnerabilities and XSS series solves them

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

牛逼|珍藏多年的工具让我实现了带薪摸鱼自由

机器学习适合女生学吗?

【MySQL入门】第一话 · 初入“数据库”大陆

B_ QuRT_ User_ Guide(31)
随机推荐
How to change the win11 computer name? Win11 method of changing computer name
Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3
I want to know who I need to know to open a stock account? In addition, is it safe to open a mobile account?
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
latex字母头顶两个点
吴恩达的机器学习适合入门吗?
B_ QuRT_ User_ Guide(31)
The programmer's girlfriend gave me a fatigue driving test
Error reporting: internal error XFS_ WANT_ CORRUPTED_ GOTO at line 1635 of file fs/xfs/libxfs/xfs_ alloc. c.
Where can I find the computer version of wechat files
Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
多线程经典案例
Spark - understand partitioner in one article
将Nagios监控信息存入MySQL
Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
部门新来了个阿里25K出来的,让我见识到了什么是天花板
B_ QuRT_ User_ Guide(32)
机器学习工作要求研究生吗?
How to design test cases