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

OpenCV报错:module 'cv2.face'无LBPHFaceRecognizer属性求助

解决AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer'

1. 修正代码拼写错误

你的代码中写的是LBPHFaceRecognizor(末尾缺少一个e),而OpenCV官方API的正确名称是LBPHFaceRecognizer。修正后的代码行应为:

face_recognizer = cv.face.LBPHFaceRecognizer.create()

2. 清理冲突依赖并重装正确包

你提到安装的face、face-recognizer包会与opencv-contrib-python产生冲突,导致cv2.face模块无法正常加载,执行以下操作:

  • 卸载冲突包:
    pip uninstall face face-recognizer -y
    
  • 安装版本匹配的opencv-python和opencv-contrib-python(你的OpenCV版本为4.8.1):
    pip install opencv-python==4.8.1 opencv-contrib-python==4.8.1 --force-reinstall
    

3. 验证PyCharm解释器配置

打开PyCharm的File > Settings > Project: [你的项目名] > Python Interpreter,确认当前使用的解释器中已安装opencv-python和opencv-contrib-python,且版本为4.8.1。若使用虚拟环境,需确保在该环境内执行上述安装命令。

4. 确认模块加载正常

在PyCharm的Python控制台中执行以下代码,验证cv2.face模块是否正常:

import cv2
print(cv2.__version__)
print(dir(cv2.face))

如果输出结果中包含LBPHFaceRecognizer,说明问题已解决。

内容的提问来源于stack exchange,提问作者İbrahim Hakkı Aydın

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 03:32:42