[目前,我正在为面部识别系统训练图像。我正在使用Python,OpenCV。我已经从网络摄像头中收集了样本,但是样本图像的大小有所不同。示例图像大小的示例为376 x 376、412 x 412、836 x 836。
当前工作目录的屏幕截图:
样本图像保存在名为'sampleImgFolder'的主文件夹中,并且在每个样本的主文件夹特定文件夹下。
训练图像的源代码
import os
import cv2
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create()
targetImagesDirectory="sampleImgFolder/"
dataset = cv2.CascadeClassifier('resources/haarcascade_frontalface_default.xml')
def getImageWithID(path):
#empty list to store processed data
sampleFaces = []
sampleFaceId = []
os.chdir(targetImagesDirectory)
for directory in os.listdir():
os.chdir(directory)
for files in os.listdir():
imagePath = '{}/{}'.format(os.getcwd(), files)
imagePil = Image.open(imagePath).convert('L')
imageNumpy = np.array(imagePil, 'uint8') #conversion of normal image to numpy array
#imageNumpy.astype(np.float32)
#detect face
faces = dataset.detectMultiScale(imageNumpy)
#extracting id from file name
id = files.split('_')
id = id[0].split('-')
id = id[2]
for (x, y, w, h) in faces:
sampleFaces.append(imageNumpy[y:y + h, x:x + w])
sampleFaceId.append(id)
os.chdir('../')
os.chdir('../')
return np.array(sampleFaceId), sampleFaces
print("reading images")
Ids,faces=getImageWithID(targetImagesDirectory)
print('reading completed')
recognizer.train(faces,Ids)
print("training")
#train the dataset. Create a file name trainningData.yml
recognizer.write('train/trainningData.yml')
cv2.destroyAllWindows()
运行以上代码时出现以下错误:
recognizer.train(faces, np.array(Ids))