0

I am trying to integrate .tflite with kotlin mobile application but this is giving me the following error. I have tried multiple suggestions on internet but didnot work. here is my error:

2022-06-09 20:01:26.489 23351-26611/com.example.miniproject E/ModelUtils: Failed to open model file
java.io.FileNotFoundException: detect.tflite: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:492)
    at java.io.FileInputStream.<init>(FileInputStream.java:160)
    at com.google.mlkit.common.internal.model.ModelUtils.getModelLoggingInfo(com.google.mlkit:common@@17.1.0:24)
    at com.google.mlkit.vision.objects.custom.internal.zzh.zzf(Unknown Source:10)
    at com.google.mlkit.vision.objects.custom.internal.zzg.tryLoad(com.google.mlkit:object-detection-custom@@16.3.0:1)
    at com.google.mlkit.common.sdkinternal.model.CustomModelLoader.load(com.google.mlkit:common@@17.1.0:3)
    at com.google.mlkit.vision.objects.custom.internal.zzh.load(com.google.mlkit:object-detection-custom@@16.3.0:2)
    at com.google.mlkit.common.sdkinternal.ModelResource.zza(Unknown Source:18)
    at com.google.mlkit.common.sdkinternal.zzn.run(Unknown Source:10)
    at com.google.mlkit.common.sdkinternal.zzp.run(com.google.mlkit:common@@17.1.0:2)
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zze(com.google.mlkit:common@@17.1.0:4)
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzc(Unknown Source:8)
    at com.google.mlkit.common.sdkinternal.zzj.run(Unknown Source:2)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzd(Unknown Source:10)
    at com.google.mlkit.common.sdkinternal.zzk.run(Unknown Source:2)
    at java.lang.Thread.run(Thread.java:923)
 Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8514)
    at libcore.io.IoBridge.open(IoBridge.java:478)
    at java.io.FileInputStream.<init>(FileInputStream.java:160) 
    at com.google.mlkit.common.internal.model.ModelUtils.getModelLoggingInfo(com.google.mlkit:common@@17.1.0:24) 
    at com.google.mlkit.vision.objects.custom.internal.zzh.zzf(Unknown Source:10) 
    at com.google.mlkit.vision.objects.custom.internal.zzg.tryLoad(com.google.mlkit:object-detection-custom@@16.3.0:1) 
    at com.google.mlkit.common.sdkinternal.model.CustomModelLoader.load(com.google.mlkit:common@@17.1.0:3) 
    at com.google.mlkit.vision.objects.custom.internal.zzh.load(com.google.mlkit:object-detection-custom@@16.3.0:2) 
    at com.google.mlkit.common.sdkinternal.ModelResource.zza(Unknown Source:18) 
    at com.google.mlkit.common.sdkinternal.zzn.run(Unknown Source:10) 
    at com.google.mlkit.common.sdkinternal.zzp.run(com.google.mlkit:common@@17.1.0:2) 
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zze(com.google.mlkit:common@@17.1.0:4) 
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzc(Unknown Source:8) 
    at com.google.mlkit.common.sdkinternal.zzj.run(Unknown Source:2) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzd(Unknown Source:10) 
    at com.google.mlkit.common.sdkinternal.zzk.run(Unknown Source:2) 
    at java.lang.Thread.run(Thread.java:923) 

My detect.tflite file is in assets folder in the src/main. Here is my MainActivity.kt

class MainActivity : AppCompatActivity() {

private lateinit var objectDetector: ObjectDetector
private lateinit var binding: ActivityMainBinding
private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
    cameraProviderFuture = ProcessCameraProvider.getInstance(this)

    cameraProviderFuture.addListener({
        val  cameraProvider = cameraProviderFuture.get()
        bindPreview(cameraProvider = cameraProvider)

    }, ContextCompat.getMainExecutor(this))
    val localModel = LocalModel.Builder()
        .setAbsoluteFilePath("detect.tflite")
        .build()

    val customObjectDetectorOptions = CustomObjectDetectorOptions.Builder(localModel)
        .setDetectorMode(CustomObjectDetectorOptions.STREAM_MODE)
        .enableClassification()
        .setClassificationConfidenceThreshold(0.5f)
        .setMaxPerObjectLabelCount(3)
        .build()

    objectDetector = ObjectDetection.getClient(customObjectDetectorOptions)



}

@SuppressLint("UnsafeOptInUsageError")
private fun bindPreview(cameraProvider: ProcessCameraProvider)
{
    val preview = Preview.Builder().build()
    val cameraSelector = CameraSelector.Builder()
        .requireLensFacing(CameraSelector.LENS_FACING_BACK)
        .build()
    preview.setSurfaceProvider(binding.previewView.surfaceProvider)

    val imageAnalysis = ImageAnalysis.Builder()
        .setTargetResolution(Size(1280,720))
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
        .build()

    imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this),{ imageProxy ->
        val rotationDegrees= imageProxy.imageInfo.rotationDegrees
        val image = imageProxy.image
        if(image != null){
            val processImage = InputImage.fromMediaImage(image, rotationDegrees)
            objectDetector
                .process(processImage)
                .addOnSuccessListener { objects ->

                    for(i in objects){
                        if(binding.parentLayout.childCount >1) binding.parentLayout.removeViewAt(1)
                        val element = Draw(context=this,
                            rect =  i.boundingBox,
                            text = i.labels.firstOrNull()?.text ?: "Undefined")
                        binding.parentLayout.addView(element)
                    }
                    imageProxy.close()
                }.addOnFailureListener{
                    Log.v("MainActivity","error")
                    imageProxy.close()
                }
        }
    })
    cameraProvider.bindToLifecycle(this as LifecycleOwner,cameraSelector,imageAnalysis,preview)

}

}

This is my layout page:

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<RelativeLayout
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.camera.view.PreviewView
        android:id="@+id/previewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </androidx.camera.view.PreviewView>
</RelativeLayout>

I have tried to add the following in my code but it still doesn't work.

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:requestLegacyExternalStorage="true">

I am using the following MlKit: implementation 'com.google.mlkit:object-detection-custom:16.3.0'

  • I couldn't find a solution to this hence I did an alternative.https://developers.google.com/codelabs/tflite-object-detection-android#0 – Padala Kavya Jun 10 '22 at 18:39

1 Answers1

0

There is no "absolute path of the assets folder in [your] app". Assets are stored in the APK file.

https://stackoverflow.com/a/5030465/17737901

Since your model is in the asset folder, the

setAbsoluteFilePath("detect.tflite")

in your localModel declaration should be

setAssetFilePath("detect.tflite")

instead.

Addy Luo
  • 11
  • 2