Tensorflow Lite和安卓Kotlin无法打开.tflite

ee7vknir  于 2022-11-30  发布在  Kotlin
关注(0)|答案(1)|浏览(303)

我试图整合. tflite与kotlin移动应用程序,但这是给我以下错误。我已经尝试了互联网上的多个建议,但没有工作。这里是我的错误:

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) 

我的detect.tflite文件在src/main的assets文件夹中。

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)

}

}
这是我的版面配置页面:

<?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>

我已经尝试在我的代码中添加以下内容,但仍然不起作用。

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

我使用以下MlKit:实现'com.google.mlkit:对象检测-自定义:16.3.0'

f0brbegy

f0brbegy1#

setAbsoluteFilePath("detect.tflite")

localModel声明中的值应该是

setAssetFilePath("detect.tflite")

得双曲余切值.

相关问题