AttributeError:“ str”对象没有属性“ train”

时间:2019-11-07 01:50:43

标签: python opencv

我目前正在制作人脸识别系统。我在线获得了此代码。有很多问题,无线网络能够解决其他问题,但这次我无法解决。下面是代码。

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

# Path for face image database
path = 'dataset'

recognizer = "help(cv2.face.createLBPHFaceRecognizer)"
detector = cv2.CascadeClassifier("/home/pi/FacialRecognitionProject/haarcascade_frontalface_default.xml");

# function to get the images and label data
def getImagesAndLabels(path):

    imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
    faceSamples=[]
    ids = []

    for imagePath in imagePaths:

        PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
        img_numpy = np.array(PIL_img,'uint8')

        id = int(os.path.split(imagePath)[-1].split(".")[1])
        faces = detector.detectMultiScale(img_numpy)

        for (x,y,w,h) in faces:
            faceSamples.append(img_numpy[y:y+h,x:x+w])
            ids.append(id)

    return faceSamples,ids

print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))

# Save the model into trainer/trainer.yml
recognizer.write('/home/pi/FacialRecognitionProject/trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi

# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))

错误提示 识别器。火车(面孔,np.array(ids)) AttributeError:“ str”对象没有属性“ train”  您的程序尝试调用字符串的方法序列,但是此类型没有这种方法。

1 个答案:

答案 0 :(得分:0)

在行

recognizer = "help(cv2.face.createLBPHFaceRecognizer)"

您正在将识别器设置为字符串。双重qouted初始化在python中被理解为字符串。使用以下内容代替

recognizer = cv2.face.LBPHFaceRecognizer_create()

希望这会有所帮助。

更新:

您应该安装其他软件包才能使用面部模块。您可以使用以下命令进行安装。所以cv2.face.LBPHFaceRecognizer_create()可以正常工作

pip install opencv-contrib-python

注意:如果使用jupyter,则必须在pip安装后重新启动内核