当前位置:网站首页>【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'
综上所述: 集成是不是很简单, 那赶快试试吧
边栏推荐
- HDFS centralized cache management
- [micro service ~nacos] configuration center of Nacos
- 微服务链路风险分析
- "Trust machine" empowers development
- 电脑设备管理器在哪里可以找到
- Coredns modifying upstream
- I want to know who I need to know to open a stock account? In addition, is it safe to open a mobile account?
- How does win11 optimize services? Win11 method of optimizing service
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- Uniapp rich text editor
猜你喜欢

What if the taskbar is blank after win11 update? Solution to blank and stuck taskbar after win11 update

How to use data sets in machine learning?

win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法

Web APIs comprehensive case -tab column switching - dark horse programmer

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

Modify the name of the launched applet

Starting from pg15 xid64 ticket skipping again

About, Qianxin detects code vulnerabilities and XSS series solves them

Niubi | the tools I have treasured for many years have made me free to fish with pay

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
随机推荐
在启牛开的股票账户安全吗?如何申请低佣金的股票账户?
Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
How does win11 optimize services? Win11 method of optimizing service
Summary of interesting websites
Interesting plug-ins summary
Which direction should college students choose to find jobs after graduation?
Pytorch quantitative practice (2)
Stimulate new kinetic energy to develop digital economy in multiple places
【BSP视频教程】BSP视频教程第19期:单片机BootLoader的AES加密实战,含上位机和下位机代码全开源(2022-06-26)
[introduction to MySQL] the first conversation · first time in the "database" Mainland
100 important knowledge points that SQL must master: creating and manipulating tables
吴恩达的机器学习适合入门吗?
About, Qianxin detects code vulnerabilities and XSS series solves them
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
Anti leakage family photo in attack and defense drill
ML&DL:机器学习和深度学习中超参数优化的简介、评估指标、过拟合现象、常用的调参优化方法之详细攻略
请指教同花顺软件究竟是什么?另外想问,现在在线开户安全么?
Is the stock account opened in qiniu safe? How to apply for a low commission stock account?
《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
Uniapp rich text editor