当前位置:网站首页>[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)
[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)
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 ObjectDetector
rely on
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mozhimen.TFLiteLoader:objectdetector:1.0.2'
}
Access
- Global declarations
private lateinit var _tfLiteObjectDetector: TFLiteObjectDetector
- stay onCreate Initialize in
_tfLiteObjectDetector = TFLiteObjectDetector.create("efficientdet-lite0.tflite", listener = _objectDetectorListener)
- Asynchronous declaration _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)
}
}
}
}
- Object detection
_tfLiteObjectDetector.detect({ Yours Bitmap}, 0)
- An example of processing the returned data , Sure pull Code reference demo, This is the processing in the callback
override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
runOnUiThread {
results?.let {
vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
}
}
}
- result
Description of the return value
- 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)));
}
// Detect the position information of the object on the screen
public abstract RectF getBoundingBox();
// Category collection
public abstract List<Category> getCategories();
}
complete demo Code
@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()
}
}
}
}
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
边栏推荐
- How does win11 optimize services? Win11 method of optimizing service
- Error reporting: internal error XFS_ WANT_ CORRUPTED_ GOTO at line 1635 of file fs/xfs/libxfs/xfs_ alloc. c.
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- 【MySQL入门】第一话 · 初入“数据库”大陆
- 交易所系统开发如何开发?数字货币交易所系统开发成熟技术案例
- 零样本和少样本学习
- What are database OLAP and OLTP? Same and different? Applicable scenarios
- vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
- AtCoder Beginner Contest 257
- The Three Musketeers: One for All!
猜你喜欢
The Sandbox 正在 Polygon 网络上进行部署
公有云市场迈入深水区,冷静的亚马逊云还坐得住吗?
部门新来了个阿里25K出来的,让我见识到了什么是天花板
Notes [introduction to JUC package and future]
RP prototype resource sharing - shopping app
电脑设备管理器在哪里可以找到
Niubi | the tools I have treasured for many years have made me free to fish with pay
Qsort function and Simulation Implementation of qsort function
Is machine learning suitable for girls?
多線程經典案例
随机推荐
基于kubernetes平台微服务的部署
Golang application ━ installation, configuration and use of Hugo blog system
Domestic database disorder
吴恩达的机器学习适合入门吗?
Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online
Modify the name of the launched applet
腾讯3年,功能测试进阶自动化测试,送给在手工测试中迷茫的你
Starting from pg15 xid64 ticket skipping again
Tencent has been conducting advanced automated functional testing for 3 years. It is a gift to you who are confused in manual testing
Do a scrollbar thinking
Is machine learning suitable for girls?
What if the taskbar is blank after win11 update? Solution to blank and stuck taskbar after win11 update
Uniapp rich text editor
RIDE:获取图片base64
电脑设备管理器在哪里可以找到
Win11如何优化服务?Win11优化服务的方法
Zhoushaojian, rare
分享十万级TPS的IM即时通讯综合消息系统的架构
Niubi | the tools I have treasured for many years have made me free to fish with pay
I want to know who I need to know to open a stock account? In addition, is it safe to open a mobile account?