Ach113
Ach113

Reputation: 1825

How to save openCV classifier?

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?

Upvotes: 1

Views: 966

Answers (1)

Jeru Luke
Jeru Luke

Reputation: 21203

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')

Upvotes: 4

Related Questions