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

安装opencv-contrib后仍遇AttributeError: 'module'无'face'属性问题求助

Fixing "AttributeError: 'module' object has no attribute 'face'" in OpenCV 3.4.0 + Python 2.7

Hey there, let's work through this frustrating issue with the LBPH face recognizer. The error you're seeing means OpenCV can't find the face module, even after installing opencv-contrib—here are the most likely fixes:

1. Match opencv-contrib-python to your OpenCV version

Since you're using OpenCV 3.4.0, you need to install the exact matching version of the contrib package. Newer versions won't play nice with your older OpenCV setup. Here's how to do it:

  • First, uninstall any existing mismatched packages:
    pip uninstall opencv-python opencv-contrib-python -y
    
  • Then install the compatible version:
    pip install opencv-python==3.4.0.12 opencv-contrib-python==3.4.0.12
    
    Note: 3.4.0.12 is the official contrib release that pairs perfectly with OpenCV 3.4.0.

2. Verify your Python environment is using the right OpenCV installation

Sometimes Python picks up an old or system-installed version of OpenCV instead of the one you installed via pip. To check:

  • Run this in your Python shell:
    import cv2
    print(cv2.__file__)
    
  • The output should point to the pip-installed location (something like .../site-packages/cv2/cv2.so or .pyd on Windows). If it's a system path (e.g., /usr/lib/python2.7/dist-packages/), you might need to:
    • Use a virtual environment to isolate your dependencies
    • Adjust your PYTHONPATH to prioritize the pip-installed version

3. If you compiled OpenCV from source: Ensure the face module was built

If you didn't use pip and compiled OpenCV yourself, you must enable the face module during compilation. Here's what to check:

  • When running cmake, make sure the flag BUILD_opencv_face is set to ON (it's off by default in some builds)
  • Recompile OpenCV with this flag enabled, then reinstall it to your Python environment

4. Double-check your code imports

While this is less likely, try explicitly importing the face module at the top of your script:

import cv2
from cv2 import face  # Explicit import to ensure the module loads

Then you can use face.LBPHFaceRecognizer_create() instead of cv2.face.LBPHFaceRecognizer_create()—sometimes this helps resolve import quirks in older Python/OpenCV combinations.

Give these steps a try, and the face module should show up properly for your LBPH implementation!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 07:03:14