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

调用cv2.face.LBPHFaceRecognizer.create()报错及安装问题求助

AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer' 问题排查与解决

问题背景

调用以下代码时触发错误:

cv2.face.LBPHFaceRecognizer.create()

错误回溯信息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\gagan\anaconda3\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\gagan\Desktop\FaceRecognition\students_attendence\train.py", line 49, in train_classifier
    clf = cv2.face.LBPHFaceRecognizer.create()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer'

已尝试但无效的操作:

  • 管理员身份执行重装命令:
    pip uninstall opencv-python
    pip uninstall opencv-contrib-python
    pip install opencv-contrib-python
    
    安装时出现权限错误:
    pip install opencv-contrib-python
    Collecting opencv-contrib-python
      Using cached opencv_contrib_python-4.9.0.80-cp37-abi3-win_amd64.whl.metadata (20 kB)
    Requirement already satisfied: numpy>=1.21.2 in c:\users\gagan\anaconda3\lib\site-packages (from opencv-contrib-python) (1.24.3)
    Using cached opencv_contrib_python-4.9.0.80-cp37-abi3-win_amd64.whl (45.3 MB)
    Installing collected packages: opencv-contrib-python
    ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'C:\\Users\\gagan\\anaconda3\\Lib\\site-packages\\cv2\\cv2.pyd'
    Consider using the `--user` option or check the permissions.
    
  • 执行pip install opencv-contrib-python --user
  • 尝试安装opencv及opencv-contrib的3.4.8.29版本,安装失败问题依旧

解决步骤

1. 确认Python环境一致性

  • 检查PyCharm使用的解释器路径:打开File > Settings > Project: [你的项目名] > Python Interpreter,查看当前解释器路径。
  • 在系统终端执行where python(Windows)或which python(Linux/macOS),对比两者路径是否一致。若不一致,在PyCharm中切换到终端使用的解释器。

2. 彻底清理残留并重装

  • 关闭所有Python相关程序(包括PyCharm),避免文件被占用。
  • 以管理员身份打开系统命令提示符,执行强制卸载:
    pip uninstall -y opencv-python opencv-contrib-python
    
  • 手动删除残留文件:找到Python环境的site-packages目录(如C:\Users\gagan\anaconda3\Lib\site-packages),删除cv2文件夹和所有前缀为opencv的.dist-info文件夹。
  • 安装稳定指定版本:
    pip install opencv-contrib-python==4.5.5.62
    

3. 验证安装有效性

  • 打开Python交互环境,执行:
    import cv2
    print(cv2.__version__)
    print(hasattr(cv2.face, 'LBPHFaceRecognizer'))
    
    若输出True,说明LBPHFaceRecognizer已正常可用。

4. 解决PyCharm终端权限问题

  • 若PyCharm终端仍有权限报错,直接在系统管理员命令提示符中完成安装,回到PyCharm后点击Python Interpreter界面的刷新按钮,重新加载解释器。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:47:20