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

树莓派4运行DeepFace代码报错ImportError:无法从tensorflow.python.eager.context导入get_config

树莓派4运行DeepFace导入错误解决方案

问题根因

该导入错误是独立Keras包与树莓派上安装的TensorFlow版本不匹配导致的,DeepFace默认依赖独立Keras,而树莓派arm架构下的TensorFlow适配包自带keras模块,二者调用路径冲突。

修复步骤

  • 先卸载现有冲突包
pip3 uninstall -y keras tensorflow deepface
  • 安装树莓派arm架构适配的TensorFlow 2.3.0版本,不要安装x86通用架构的TensorFlow包
  • 安装DeepFace时跳过依赖安装,避免自动拉取独立Keras包
pip3 install --no-deps deepface
  • 批量修改DeepFace源码中的Keras导入路径,找到/home/pi/.local/lib/python3.7/site-packages/deepface/目录下所有文件,将所有from keras.xxx import xxx的导入语句替换为from tensorflow.keras.xxx import xxx
  • 修正本地代码的两个笔误:
    1. 图片路径simg.jpb后缀修正为simg.jpg
    2. DeepFace.verify方法中img1_path=img1m修正为img1_path=img1

修正后的运行代码:

import cv2
from deepface import DeepFace
img1 = "simg.jpg"
img2 = "simg2.jpg"
print ("before")
resp = DeepFace.verify(img1_path=img1, img2_path=img2)
print ("after")
if resp['verified']:
    print("access granted")
else:
    print("Access denied")

额外注意

  • 若运行时提示缺少依赖(如opencv、numpy、pandas等),单独使用pip3 install安装对应arm版本即可
  • 树莓派4运行内存有限,首次运行DeepFace会下载预训练模型,建议提前开启2G以上虚拟内存,避免进程被系统强制终止

内容的提问来源于stack exchange,提问作者Sa'ad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 13:09:03