2

我在树莓派 3 上使用 opencv 3.1.0,python 进行面部识别,我使用了下面的代码,但我收到一个错误,请问我该如何解决这个问题我关注了许多关于如何修复它的网站,但没有一个有效

错误

recognizer= cv2.createLBPHFaceRecognizer()

AttributeError: module ‘cv2’ has no attribute

代码

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

recognizer = cv2.createLBPHFaceRecognizer()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
        #loading the image and converting it to gray scale
        pilImage=Image.open(imagePath).convert('L')
        #Now we are converting the PIL image into numpy array
        imageNp=np.array(pilImage,'uint8')
        #getting the Id from the image
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        # extract the face from the training image sample
        faces=detector.detectMultiScale(imageNp)
        #If a face is there then append that in the list as well as Id of it
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)
    return faceSamples,Ids

faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')
4

1 回答 1

0

从 开始OpenCV 3cv2.createLBPHFaceRecognizer()已移至cv2.face模块下为cv2.face.createLBPHFaceRecognizer()AttributeError上面只是说opencv找不到模块。

cv2.face模块现在移动到opencv_contrib模块。如果找不到合适的或基于二进制文件,则需要从源代码构建opencv[1]opencv_contrib[2] 。raspbiandebian

于 2017-05-14T07:20:05.730 回答