我正在学习使用python进行人脸识别的教程。这就是我使用的代码
import cv2,os
import numpy as np
from PIL import Image
recognizer = cv2.face.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('trainingImage')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainer/trainer.yml')
收到错误信息
回溯(最近一次呼叫): File“/home/pi/pythonpy/videofacedet/craft/codacus/培训师.py“,第32行,英寸 faces,id=getImagesAndLabels('trainingImage') File“/home/pi/pythonpy/videofacedet/craft/codacus/培训师.py,第24行,在getImages和Labels中 面孔=探测器(图像NP) 错误:/home/pi/opencv-3.1.0/modules/objdetect/src/级联检测.cpp:1639:错误:(-215)!函数detectMultiScale中的empty()
我在某处读到说我指向的文件夹(trainingImage)是空的,但它不是。我把我的面部训练图像放在那里,文件名格式与教程作者使用的格式相同。我希望有人能帮我解决这个问题。在
问题解决了。我把haarcascade的xml路径弄错了。修复了路径,它按预期工作。在
相关问题 更多 >
编程相关推荐