我已经在这段代码上工作了很长时间,我在几个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'
如果通过运行
pip install opencv-python
安装opencv,则缺少一些软件包,因此需要运行pip install opencv-contrib-python
来安装完整版本如果不起作用,请尝试重新安装opencv
pip3 uninstall opencv-contrib-python
python -m pip install opencv-contrib-python
相关问题 更多 >
编程相关推荐