我正在做一個關於人臉識別的簡單項目,我被卡在一個似乎與opencv相關的錯誤中。我創建了一個名稱爲識別器的文件夾來存儲trainner.yml文件。但該文件沒有被生成。用於人臉識別的Python中的OpenCV
以下是訓練數據集圖像的代碼。
import cv2
import 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:
token = imagePath.split(".")
if(token[1] =='jpeg'):
#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)
cv2.imshow("Adding faces to trainning set. . . ",imageNp[y:y+h,x:x+w])
cv2.waitKey(50)
return faceSamples,Ids
faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')
我對着錯誤是這樣:
Traceback (most recent call last):
File "C:\Users\User\Desktop\python opencv\Face Recog\trainner.py", line 37, in <module>
recognizer.train(faces, np.array(Ids))
error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:917: error: (-210) Empty training data was given. You'll need more than one sample to learn a model. in function cv::LBPH::train
在返回您的數據對於我的錯誤是非常明顯的,可能你沒有正確加載數據集。 –