Saving OpenCV object in memory in python

Viewed 1630

I am training a face recognition model using Fisher Face algorithm using OpenCV library and Python language.

   fisherFace = cv2.face.FisherFaceRecognizer_create()
   fisherFace.train(imagefaceList, np.array(labelsIndexList))

I want to save this model in file/memory. In other word i want to save 'fisherface' object. I have tried pickle module for saving this object using this. I am not able to pickle and unpickle this object.Code is as below:

class test(object):
    def __init__(self, a):
        self.a = a
def pickle_test(t):
    print('pickling a test instance...')
        return test, (t.a,)
copyreg.pickle(test, pickle_test)
t = test(f)
t1 = copy.copy(t)
t2 = pickle.dumps(t)

Is there any way available that saves trained model for fisher face algorithm and use it at other place by loading same model for face recognition?

2 Answers

To save a face recognizer once it has been trained, you can use the write method and save the state as a yaml or an xml file. To use the saved model in the next iteration of the program, call the read method of the recognizer and pass the location of the file where the state was saved.

Related