Android ML 套件无法标记图像
Android ML Kit Not Able to Label Image
所以我有一个 Android 应用程序,用户可以在其中使用相机应用程序拍摄一只鸟的照片,然后它会对这只鸟进行分类。我按照 label images with a custom model for 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)
谁能告诉我如何解决这个问题?
查看堆栈跟踪,似乎输入位图是直接从相机获取的,并且驻留在内存中 (Bitmap.Config.HARDWARE
)。 ML Kit 只支持 ARGB_8888 格式的位图,请尝试:
val newBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, myBitmap.isMutable())
当我们开发这个时,我们正在考虑将文件保存在设备上并将其作为 ARGB_8888 而不是从设备相机加载。我将提交一个增强请求,看看我们是否可以涵盖这个用例。感谢举报!
所以我有一个 Android 应用程序,用户可以在其中使用相机应用程序拍摄一只鸟的照片,然后它会对这只鸟进行分类。我按照 label images with a custom model for 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)
谁能告诉我如何解决这个问题?
查看堆栈跟踪,似乎输入位图是直接从相机获取的,并且驻留在内存中 (Bitmap.Config.HARDWARE
)。 ML Kit 只支持 ARGB_8888 格式的位图,请尝试:
val newBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, myBitmap.isMutable())
当我们开发这个时,我们正在考虑将文件保存在设备上并将其作为 ARGB_8888 而不是从设备相机加载。我将提交一个增强请求,看看我们是否可以涵盖这个用例。感谢举报!