0

所以我有一个 Android 应用程序,用户可以使用相机应用程序给鸟儿拍照,然后它会对鸟儿进行分类。我按照适用于 Android 的自定义模型的标签图像文档进行操作,但它不起作用。我的代码中有这段代码onActivityResult

val source = ImageDecoder.createSource(this.contentResolver, Uri.fromFile(photoFile))
val birdBitmap= ImageDecoder.decodeBitmap(source)

val image = InputImage.fromBitmap(birdBitmap, 0)
val localModel = LocalModel.Builder().setAssetFilePath("model.tflite").build()
val customImageLabelerOptions = CustomImageLabelerOptions.Builder(localModel)
    .setConfidenceThreshold(0.5f)
    .setMaxResultCount(5)
    .build()
val imageLabeler =
    ImageLabeling.getClient(customImageLabelerOptions)
imageLabeler.process(image)
    .addOnSuccessListener { labels ->
        var highConf = -1.0f
        var highText = ""
        for (label in labels) {
            val text = label.text
            val confidence = label.confidence
            val index = label.index

            if(confidence > highConf) {
                highConf = confidence
                highText = text
            }
        }
        Log.d("PREDICTION", "$highText, $highConf")
    }
    .addOnFailureListener { e ->
        Log.d("FAIL", "$e")
    }

当用户拍照时,addOnFailureListener会调用,这是我得到的错误:

com.google.mlkit.common.MlKitException: Internal error has occurred when executing ML Kit tasks
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@16.0.0:28)
at com.google.mlkit.common.sdkinternal.zzn.call(Unknown Source:6)
at com.google.mlkit.common.sdkinternal.zzm.run(com.google.mlkit:common@@16.0.0:5)
at com.google.mlkit.common.sdkinternal.zzq.run(com.google.mlkit:common@@16.0.0:3)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zzd(com.google.mlkit:common@@16.0.0:24)
at com.google.mlkit.common.sdkinternal.MlKitThreadPool.zza(com.google.mlkit:common@@16.0.0:30)
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 java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalArgumentException: Unsupported bitmap config HARDWARE
at com.google.android.libraries.vision.visionkit.pipeline.zzbb.zza(com.google.mlkit:vision-internal-vkp@@17.0.0:56)
at com.google.mlkit.vision.vkp.PipelineManager.process(com.google.mlkit:vision-internal-vkp@@17.0.0:150)
at com.google.mlkit.vision.label.custom.internal.zzd.zza(com.google.mlkit:image-labeling-custom@@16.1.0:19)
at com.google.mlkit.vision.label.custom.internal.zzd.run(com.google.mlkit:image-labeling-custom@@16.1.0:112)
at com.google.mlkit.vision.common.internal.MobileVisionBase.zza(com.google.mlkit:vision-common@@16.0.0:23)
at com.google.mlkit.vision.common.internal.zzb.call(Unknown Source:4)
at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@16.0.0:26)

谁能告诉我如何解决这个问题?

4

1 回答 1

3

查看堆栈跟踪,似乎输入位图是直接从相机获取的,它驻留在内存中(Bitmap.Config.HARDWARE)。ML Kit 仅支持 ARGB_8888 格式的位图,请尝试:

val newBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, myBitmap.isMutable())

当我们开发这个时,我们正在考虑保存在设备上的文件并将其加载为 ARGB_8888 而不是从设备相机加载。我将提交一个增强请求,看看我们是否可以涵盖这个用例。感谢举报!

于 2020-07-21T19:55:58.707 回答