1

I want to save a classifier that has been trained on multiple images to avoid the time it takes to re-train it every time I run the program. For sklearn's classifiers I was able to simply pickle them, using pickle.load but when I try doing the same I get following error:

TypeError: can't pickle cv2.face_LBPHFaceRecognizer objects

Heres the classifier itself:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
clf = cv2.face.LBPHFaceRecognizer_create()

img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# detecting face using haarcasade
face = face_cascade.detectMultiScale(img, minNeighbors = 3)
# detecting region of interest and appending it to a separate matrix
for x, y, w, h in face:
     roi = img[y:y+h, x:x+w]
     x_train.append(roi)
     y_train.append(label)
 clf.train(x_train, y_train)

Is there any way to save such classifier?

Ach113
  • 1,775
  • 3
  • 18
  • 40

1 Answers1

4

You can save such classifiers as a .yml file.

For example:

clf.save('trainingData.yml')

You can load the same using:

clf.load('trainingData.yml')
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Saving does work, but when doing clf.load() I get error saying: object has no attribute 'load' – Ach113 Aug 14 '18 at 14:28
  • Apparently, the load() has been replaced with read() in newer versions of openCV. – Ach113 Aug 14 '18 at 14:41
  • What version of OpenCV are you using? I am using 3.4.1 and `load()` works for me – Jeru Luke Aug 14 '18 at 14:48
  • Strange, mine is 3.4.1 as well, but I googled the problem I was having with load() and I came across this https://stackoverflow.com/questions/46873728/cv2-lbphfacerecognizer-has-no-attribute-load-predict. Oddly save() does work when this post is saying that it too had been replaced by write() – Ach113 Aug 14 '18 at 15:26