You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android的Chaquopy环境中为Python解释器添加TensorFlow Select Ops

在Chaquopy环境中运行含Select Ops的TensorFlow Lite模型解决方案

核心问题说明

Chaquopy环境里,Python侧的TensorFlow Lite与Android原生依赖相互独立,直接添加原生org.tensorflow:tensorflow-lite-select-tf-ops依赖无法作用于Python脚本,必须从Python层面配置Flex Delegate。

具体解决步骤

  1. 安装完整TensorFlow包
    在Flutter项目的android/app/build.gradle的Chaquopy配置中,安装完整的tensorflow包(而非精简版tensorflow-lite),该包内置Select Ops支持:

    chaquopy {
        pip {
            install "numpy"
            install "opencv-python"
            install "tensorflow>=2.12.0" // 选择适配模型的稳定版本
        }
    }
    
  2. Python脚本显式启用Flex Delegate
    加载TFLite模型时,必须通过experimental_delegates参数指定Flex Delegate,示例代码如下:

    import tensorflow as tf
    
    # 加载含Select Ops的模型
    interpreter = tf.lite.Interpreter(
        model_path="keras_ocr_float16_ctc.tflite",
        experimental_delegates=[tf.lite.experimental.load_delegate('libtensorflowlite_flex_jni.so')]
    )
    interpreter.allocate_tensors()
    

    注:libtensorflowlite_flex_jni.so会随TensorFlow包自动安装,Chaquopy环境可直接识别该库路径。

  3. 环境兼容性检查

    • 确保Android项目的minSdkVersion不低于21(Flex Delegate要求最低API 21)
    • 模型为float16格式时,需使用支持float16 Flex Ops的TensorFlow版本(建议2.12.0及以上)
  4. 清理缓存重新构建
    执行Gradle清理命令,避免旧依赖干扰:

    ./gradlew clean
    

    之后重新构建Flutter项目,确保依赖完全更新。

内容的提问来源于stack exchange,提问作者Jose Georges

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:12:54