• 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)


    深度学习.Tensorflow.TFLite.移动设备集成深度学习轻模型TFlite.图像分类篇

    Why i create it?

    为了创建一个易用且易于集成的TFlite加载库, 所以TFLiteLoader应运而生

    在这里插入图片描述

    集成 ObjectDetector

    依赖

    allprojects {
    	repositories {
    		...
    		maven { url 'https://jitpack.io' }
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    dependencies {
    	implementation 'com.github.mozhimen.TFLiteLoader:objectdetector:1.0.2'
    }
    
    • 1
    • 2
    • 3

    接入

    1. 全局声明
    private lateinit var _tfLiteObjectDetector: TFLiteObjectDetector
    
    • 1
    1. 在onCreate中进行初始化
    _tfLiteObjectDetector = TFLiteObjectDetector.create("efficientdet-lite0.tflite", listener = _objectDetectorListener)
    
    • 1
    1. 异步声明_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)
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 物体检测
    _tfLiteObjectDetector.detect({你的Bitmap}, 0)
    
    • 1
    • 对返回数据的处理示例, 可以pull代码参考demo, 这是回调中的处理
    override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
    	runOnUiThread {
        	results?.let {
            	vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 结果

    在这里插入图片描述

    对于返回值的说明

    • 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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    完整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()
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    关于这里的框架代码, 可以参考我另一个开源框架库: 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'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    综上所述: 集成是不是很简单, 那赶快试试吧

  • 相关阅读:
    C++ 11新特性之std::function类模板与std::bind绑定器介绍
    rar格式转换zip格式,如何做?
    请问签过版权转让 协议后,接下来是不是要在 author gateway 等待?
    【爬虫系列】Python 爬虫入门(2)
    Java控制台输入输出问题
    Leetcode 37. 解数独
    LeetCode:第302场周赛【总结】
    基于时空注意力融合网络的城市轨道交通假期短时客流预测
    Windows工业三防平板全功能NFC近距离感应一维/二维扫描
    HTMl案例二:注册页面
  • 原文地址:https://blog.csdn.net/weixin_42473228/article/details/125538970