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

face_recognition库运行报错:AttributeError及PIL模块ValueError求助

解决face_recognition库运行报错的问题

问题分析

运行测试代码时出现关联的两类错误:

  1. 表面报错:AttributeError: module 'face_recognition' has no attribute 'load_image_file'
  2. 实际根源:导入face_recognition时,PIL模块的__init__.py文件第15行触发ValueError: source code string cannot contain null bytes

本质是PIL(Pillow)库的文件损坏,导致face_recognition依赖的图像处理功能无法正常加载,进而引发属性找不到的连锁错误。另外测试代码中还存在路径问题:load_image_file传入的是文件夹路径,而非具体图片文件路径,这也会导致后续读取错误。

修复步骤

  • 完全卸载损坏的Pillow库:
    pip uninstall -y pillow
    
  • 清理残留文件:
    找到Python安装目录下的Lib\site-packages文件夹,删除所有以PIL或Pillow开头的文件夹与文件。
  • 重新安装最新版Pillow:
    pip install --upgrade pillow
    
  • 修正测试代码的图片路径:
    将load_image_file的参数改为具体图片文件路径,比如:
    image = face_recognition.load_image_file(r'C:\Users\WAJIH\Desktop\code\face\test.jpg')
    
  • 若上述操作后问题仍存在,重新安装face_recognition相关库:
    pip uninstall -y face_recognition face_recognition_models
    pip install face_recognition
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 14:30:16