Android摄像头帧年龄性别识别中GpuDelegate初始化报错求助
Android TensorFlow Lite GPU Delegate初始化错误解决方案
错误信息
E/skurt.genderag: [qarth_debug:] get PatchStore::createDisableExceptionQarthFile method fail. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.esrabaskurt.genderage, PID: 11140 java.lang.NoClassDefFoundError: Failed resolution of: Lorg/tensorflow/lite/gpu/GpuDelegateFactory$Options; at org.tensorflow.lite.gpu.GpuDelegate.<init>(GpuDelegate.java:52) at com.esrabaskurt.genderage.age_gender_recognition.<init>(age_gender_recognition.java:56) at com.esrabaskurt.genderage.CameraActivity.onCreate(CameraActivity.java:79) ...(省略后续栈信息) I/Process: Sending signal. PID: 11140 SIG: 9
相关代码片段
age_gender_recognition.java 第56行
import org.tensorflow.lite.gpu.GpuDelegate; public class age_gender_recognition { ... private GpuDelegate gpuDelegate=null; ... age_gender_recognition(AssetManager assetManager, Context context,String modelPath,int inputSize) throws IOException { ... gpuDelegate =new GpuDelegate(); ... }}
CameraActivity.java 第79行
age_gender_recognition =new age_gender_recognition(getAssets(),CameraActivity.this,"model.tflite",inputSize);
build.gradle 依赖配置
dependencies { implementation 'org.tensorflow:tensorflow-lite-metadata:0.4.3' implementation 'org.tensorflow:tensorflow-lite-gpu:2.11.0' implementation 'org.tensorflow:tensorflow-lite-support:0.4.3' implementation 'org.tensorflow:tensorflow-lite-task-vision:0.4.3' implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.3' ... }
解决建议
统一TensorFlow Lite依赖版本
当前依赖中tensorflow-lite-gpu版本为2.11.0,其他TFLite相关依赖版本为0.4.3,版本不匹配会导致类加载失败。确保所有TensorFlow Lite相关依赖版本对齐,例如保持0.4.3(该版本对应TFLite 2.11.0),若升级则同步所有相关依赖版本。修正GpuDelegate初始化方式
TFLite 2.11.0中GpuDelegate无参构造内部依赖已变更,建议使用带配置项的构造方法:import org.tensorflow.lite.gpu.GpuDelegate; public class age_gender_recognition { private GpuDelegate gpuDelegate = null; age_gender_recognition(AssetManager assetManager, Context context, String modelPath, int inputSize) throws IOException { GpuDelegate.Options options = new GpuDelegate.Options(); // 根据需求配置参数,示例配置如下 options.setPrecisionLossAllowed(true); options.setInferencePreference(GpuDelegate.Options.INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER); gpuDelegate = new GpuDelegate(options); // 其他初始化代码 } }清理重建项目
执行Build > Clean Project,再执行Build > Rebuild Project,清除应用缓存后重新安装。检查多Dex配置
若项目启用多Dex,确保GpuDelegateFactory$Options被包含在主Dex中:android { defaultConfig { multiDexEnabled true } } dependencies { implementation 'androidx.multidex:multidex:2.0.1' }
内容的提问来源于stack exchange,提问作者esra
相关产品推荐
相关产品推荐

