当前位置:网站首页>[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
边栏推荐
- HDFS集中式缓存管理(Centralized Cache Management)
- "Trust machine" empowers development
- Analysis of doctor Aifen's incident
- How to judge whether the JS object is empty
- 公有云市场迈入深水区,冷静的亚马逊云还坐得住吗?
- [untitled] first time to participate in CSDN activities
- HDFS centralized cache management
- [backtracking] full arrangement II leetcode47
- 十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
- 交易所系统开发如何开发?数字货币交易所系统开发成熟技术案例
猜你喜欢

机器学习适合女生学吗?

Uniapp life cycle / route jump

电脑设备管理器在哪里可以找到
![[introduction to MySQL] the first conversation · first time in the](/img/73/cc85eb469384c3df94479318293c6f.png)
[introduction to MySQL] the first conversation · first time in the "database" Mainland

Some memory problems summarized

实现多方数据安全共享,解决普惠金融信息不对称难题

分享十万级TPS的IM即时通讯综合消息系统的架构

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

Analysis of PostgreSQL storage structure

Cas classique multithreadé
随机推荐
Win11电脑名如何更改?Win11更改电脑名的方法
腾讯3年,功能测试进阶自动化测试,送给在手工测试中迷茫的你
About, Qianxin detects code vulnerabilities and XSS series solves them
dba
"Team training competition" Shandong multi university training 3
Cas classique multithreadé
机器学习适合女生学吗?
Strictly minor spanning tree
Analysis of doctor Aifen's incident
B_ QuRT_ User_ Guide(31)
Anfulai embedded weekly report no. 271: June 20, 2022 to June 26, 2022
Development techniques - import files using easyexcel (simple example)
将Nagios监控信息存入MySQL
【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
Femas: cloud native multi runtime microservice framework
[backtracking] full arrangement II leetcode47
Study summary of dynamic routing between capsules
多線程經典案例
The Three Musketeers: One for All!
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]