Saving OpenCV object in memory in python

你说的曾经没有我的故事 提交于 2019-12-22 09:48:39

问题


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?


回答1:


FaceRecognizer class has a save method, that stores a xml/yml file into the disk that can be loaded with the load method.

Here is the class method list.

So, you should be able to do

fisherFace.save("model.xml")

To save model to file. https://docs.opencv.org/3.0-last-rst/modules/face/doc/facerec_api.html




回答2:


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.



来源:https://stackoverflow.com/questions/47202316/saving-opencv-object-in-memory-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!