attributeerror: module 'cv2.face' has no a

2020-03-04 07:33发布

So i'm doing a little personal project but i keep getting this error when I try to create the recognizer. i have opencv-contrib and everything. Does anyone know whats going on? code posted below

import cv2, os
import numpy as np
from PIL import Image

cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

recognizer = cv2.face.createLBPHFaceRecognizer()

it gets caught on that last line. I've tried reinstalling all modules already. Not really sure what else to do. The weird thing is it works on my laptop but not my desktop. They both have the same modules, same python release and running the exact same code.

12条回答
看我几分像从前
2楼-- · 2020-03-04 07:37

Try to update your opencv by "python -m pip install opencv-contrib-python" ps: you have to delete the CV2 repository from the Python rep and then run this command (in the CMD windows) if it doesn't work

查看更多
欢心
3楼-- · 2020-03-04 07:37

I had this problem running opencv Version 3.4.1. Here is what I did.

SPECS: Raspberry pi 3B, OS: Raspbian, Version: 9 (Stretch), Python 3, opencv Version 3.4.1

Check opencv version in python

import cv2

cv2.__version__

1) sudo pip install opencv-contrib-python

*After this I could not import cv2 in python until I installed the following.

2) sudo apt-get update

3) sudo apt-get install libhdf5-dev

4) sudo apt-get update

5) sudo apt-get install libhdf5-serial-dev libqtgui4 libqt4-test

查看更多
三岁会撩人
4楼-- · 2020-03-04 07:39
  • Uninstall this package (opencv-python) by command :-
  • pip uninstall opencv-python
  • Install the library opencv-contrib python using command :-
  • pip install opencv-contrib-python
  • then add or check :-
  • recognizer = cv2.face_LBPHFaceRecognizer.create()
  • It will work fine
  • The problem was in opencv-python library cv2.face is not present so it shows attribute missing problem, so install new lib by uninstalling previous one if installed..
查看更多
神经病院院长
5楼-- · 2020-03-04 07:40

You are using Opencv 3.x, in the new version few modules has been removed. You have two options: 1. Add opencv_contrib module to your existing opencv 3.x version. Here's the link https://github.com/opencv/opencv_contrib 2.you can use older versions of Opencv. Like opencv 2.4.x

查看更多
够拽才男人
6楼-- · 2020-03-04 07:45

There are some missing modules for contributed libraries in the default pip install opencv-python so you need pip install opencv-contrib-python

查看更多
Evening l夕情丶
7楼-- · 2020-03-04 07:45

Try to use this:

import cv2
import os
import numpy as np
from PIL import Image

# Path for face image database
path = 'dataset'
recognizer = cv2.face_LBPHFaceRecognizer.create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
查看更多
登录 后发表回答