python 版本 3.6.1
numpy 版本 1.13.1
cv2 版本 3.3.0
import numpy as np
import cv2
import os
from PIL import Image
recognizer = cv2.createLBPHFaceRecognizer()
path = 'dataset'
def getImageWithID(path):
imagesPaths = [os.path.join(path,f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath)
faceNp = np.array(faceImg, 'uint8')
ID = int(os.path.split(imagePath)[-1].split(".")[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("training the data ", facesNp)
cv2.waitKey(10)
return IDs, faces
Ids, faces = getImageWithID(path)
recognizer.train(faces, Ids)
recognizer.save("recognizer/trainingData.yml")
cv2.destroyAllWindows()