虽然我已经安装了opencv所需的所有软件包,但为什么会出现此错误?

2024-03-29 00:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在这段代码上工作了很长时间,我在几个ide中运行过,但都显示出相同的错误,我现在有点困惑,我需要一个合适的解决方案,以便代码可以工作

import os
import cv2 as cv
import numpy as np
people=['emily','emma','jim parson']
haar_cas=cv.CascadeClassifier('haar_face.xml')
DIR=r'G:\opencv\faces'
features=[]
labels=[]
def train_face():
    for person in people:
        paths=os.path.join(DIR,person)
        label=people.index(person)
        for img in os.listdir(paths):
            img_path=os.path.join(paths,img)
            img_array=cv.imread(img_path)
            gray=cv.cvtColor(img_array,cv.COLOR_BGR2GRAY)
            faces_rect=haar_cas.detectMultiScale(gray,1.1,2)
            for(x,y,w,h) in faces_rect:
                faces_roi= gray[y:y+h,x:x+w]
                features.append(faces_roi)
                labels.append(label)
train_face()
feature=np.array(features,dtype='object')
label=np.array(labels)

# print(f'length of the feature={len(features)}')
# print(f'lenght of the labels={len(labels)}')
face_reconizer=cv.face.LBPHFaceReconizer_create()
np.save('feature.npy',feature)
np.save('label.py',label)

代码的输出是

File "G:\opencv\face_reconization.py", line 28, in <module>
    face_reconizer=cv.face.LBPHFaceReconizer_create()
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceReconizer_create'

1条回答
网友
1楼 · 发布于 2024-03-29 00:35:28

如果通过运行pip install opencv-python安装opencv,则缺少一些软件包,因此需要运行pip install opencv-contrib-python来安装完整版本

如果不起作用,请尝试重新安装opencv pip3 uninstall opencv-contrib-pythonpython -m pip install opencv-contrib-python

相关问题 更多 >