当前位置:网站首页>【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

集成 ObjectDetector
依赖
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mozhimen.TFLiteLoader:objectdetector:1.0.2'
}
接入
- 全局声明
private lateinit var _tfLiteObjectDetector: TFLiteObjectDetector
- 在onCreate中进行初始化
_tfLiteObjectDetector = TFLiteObjectDetector.create("efficientdet-lite0.tflite", listener = _objectDetectorListener)
- 异步声明_objectDetectorListener
private val _objectDetectorListener: IObjectDetectorListener = object : IObjectDetectorListener {
override fun onError(error: String) {
runOnUiThread {
error.showToast()
}
}
override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
runOnUiThread {
results?.let {
vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
}
}
}
}
- 物体检测
_tfLiteObjectDetector.detect({你的Bitmap}, 0)
- 对返回数据的处理示例, 可以pull代码参考demo, 这是回调中的处理
override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
runOnUiThread {
results?.let {
vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
}
}
}
- 结果

对于返回值的说明
- MutableList{Detection}
@AutoValue
@UsedByReflection("object_detection_jni.cc")
public abstract class Detection {
public Detection() {
}
@UsedByReflection("object_detection_jni.cc")
public static Detection create(RectF boundingBox, List<Category> categories) {
return new AutoValue_Detection(new RectF(boundingBox), Collections.unmodifiableList(new ArrayList(categories)));
}
//检测物体在画面的位置信息
public abstract RectF getBoundingBox();
//类别集合
public abstract List<Category> getCategories();
}
完整demo代码
@PermissionKAnnor(permissions = [Manifest.permission.CAMERA])
class ObjectDetectionActivity : BaseKActivity<ActivityObjectDetectionBinding, BaseKViewModel>(R.layout.activity_object_detection) {
private lateinit var _tfLiteObjectDetector: TFLiteObjectDetector
private val _objectDetectorListener: IObjectDetectorListener = object : IObjectDetectorListener {
override fun onError(error: String) {
runOnUiThread {
error.showToast()
}
}
override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
runOnUiThread {
results?.let {
vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
}
}
}
}
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() {
_tfLiteObjectDetector = TFLiteObjectDetector.create("efficientdet-lite0.tflite", listener = _objectDetectorListener)
// _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.objectDetectionPreview.initCamera(this, CameraSelector.DEFAULT_BACK_CAMERA)
vb.objectDetectionPreview.setImageAnalyzer(_frameAnalyzer)
vb.objectDetectionPreview.startCamera()
}
private val _frameAnalyzer: ImageAnalysis.Analyzer by lazy {
object : ImageAnalysis.Analyzer {
private val _reentrantLock = ReentrantLock()
@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)
_tfLiteObjectDetector.detect(rotateBitmap, 0)
} 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'
综上所述: 集成是不是很简单, 那赶快试试吧
边栏推荐
- [backtracking] full arrangement II leetcode47
- 顺祝老吴的聚会
- Analysis of PostgreSQL storage structure
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- Analysis of doctor Aifen's incident
- Modify the name of the launched applet
- Summary of errors reported when using YML file to migrate CONDA environment
- ML&DL:機器學習和深度學習中超參數優化的簡介、評估指標、過擬合現象、常用的調參優化方法之詳細攻略
- 部门新来了个阿里25K出来的,让我见识到了什么是天花板
- Excuse me, can I open an account for the company? Is it safe? All the answers you want are here
猜你喜欢
![Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]](/img/a8/6472e2051a295f5e42a88d64199517.png)
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]

机器学习适合女生学吗?

Pytorch quantitative practice (2)

Uniapp routing uni simple router

部门新来了个阿里25K出来的,让我见识到了什么是天花板

Nansen复盘加密巨头自救:如何阻止百亿多米诺倾塌

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

Introduce an online platform for multi omics integration and network visual analysis

机器学习工作要求研究生吗?

【BSP视频教程】BSP视频教程第19期:单片机BootLoader的AES加密实战,含上位机和下位机代码全开源(2022-06-26)
随机推荐
Do a scrollbar thinking
程序员女友给我做了一个疲劳驾驶检测
Apache服务器OpenSSL升级
Stinky tofu made by Grandma
Microservice link risk analysis
[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)
Go Web 编程入门: 一探优秀测试库 GoConvey
Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online
Analysis of PostgreSQL storage structure
How to change the win11 computer name? Win11 method of changing computer name
Pytorch quantitative practice (1)
Excuse me, can I open an account for the company? Is it safe? All the answers you want are here
对于产业互联网的粗浅认识,最终将产业互联网的发展带入到了消费互联网的怪圈之中
电脑版微信文件存储在哪个文件夹可以找到
Anfulai embedded weekly report no. 270: June 13, 2022 to June 19, 2022
When unittest automatically tests multiple use cases, the logging module prints repeatedly to solve the problem
Qsort function and Simulation Implementation of qsort function
Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
微服务链路风险分析
Bloom filter