There is a problem with Python OpenCv face recognition: AttributeError: module 'cv2' has no attribute 'face'

When learning Python OpenCv face recognition, there is a line of code as follows:

##加载识别器
recognizer = cv2.face.LBPHFaceRecognizer_create()

Problem:
The problem after running is: AttributeError: module 'cv2' has no attribute 'face' . This problem occurs because the face module is not actually part of the OpenCv library. More precisely, face is part of the opencv-contrib library . From the readme: This repository [opencv-contrib] is used to develop so-called "extra" modules, providing functionality. New modules often do not have stable APIs, and they are not well tested. Therefore, they should not be released as part of the official OpenCV distribution, as the library maintains binary compatibility and tries to provide good performance and stability.

Solution:
After knowing the problem, we can install the corresponding library. There are many methods on the Internet, such as:
(1) Install opencv-python and opencv-contrib-python to solve it
(2) After uninstalling opencv-contrib-python Reinstall it again

I can solve it by using the first method myself, just use the following command to install the opencv-contrib library :

pip install opencv-contrib-python -i https://mirrors.aliyun.com/pypi/simple/

Some computers may fail to install after using the above command. The problem is as follows:
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied. : 'D:\Anaconda\AnacondaPath\envs\p1\Lib\site-packages\cv2\cv2.pyd'
Consider using the --useroption or check the permissions.
insert image description here
If the above problem occurs, use the following method to solve it, and enter another installation command. Can:

pip install --user opencv-contrib-python -i https://mirrors.aliyun.com/pypi/simple/

The installation is successful as shown in the figure below:
insert image description here
After the opencv-contrib library is installed through the above steps, the error will not be reported when the code is run again. This is my own successful solution, and I hope it can help you solve the problem, thank you!

Guess you like

Origin blog.csdn.net/qq_40280673/article/details/124838213