Why i create it?
为了创建一个易用且易于集成的TFlite加载库, 所以TFLiteLoader应运而生
集成 ObjectDetector
依赖
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mozhimen.TFLiteLoader:objectdetector:1.0.2'
}
接入
private lateinit var _tfLiteObjectDetector: TFLiteObjectDetector
_tfLiteObjectDetector = TFLiteObjectDetector.create("efficientdet-lite0.tflite", listener = _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)
override fun onResults(imageWidth: Int, imageHeight: Int, inferenceTime: Long, results: MutableList<Detection>?) {
runOnUiThread {
results?.let {
vb.objectDetectionOverlay.setObjectRect(imageWidth, imageHeight, results)
}
}
}
对于返回值的说明
@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'
综上所述: 集成是不是很简单, 那赶快试试吧