Skip to content

Instantly share code, notes, and snippets.

@gb103
Created January 6, 2021 11:01
Show Gist options
  • Save gb103/a24326159e5e4da6f2204d542726e4b4 to your computer and use it in GitHub Desktop.
Save gb103/a24326159e5e4da6f2204d542726e4b4 to your computer and use it in GitHub Desktop.
Train a model from image database and create a .yml file.
import os
import cv2
import numpy as np
from PIL import Image
#recognizer=cv2.createLBPHFaceRecognizer();
recognizer = cv2.face.LBPHFaceRecognizer_create()
path='dataSet'
def getImagesWithID(path):
imagepaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagepath in imagepaths:
if ".DS_Store" in imagepath:
print("Junk!")
else:
faceImg=Image.open(imagepath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagepath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("training",faceNp)
cv2.waitKey(10)
return np.array(IDs),faces
IDs,faces=getImagesWithID(path)
recognizer.train(faces,IDs)
recognizer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment